Skip to content

Commit

Permalink
fix: recursive loop in GetTransactionHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
richzw committed Jan 10, 2024
1 parent decd225 commit 2c35c74
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
17 changes: 15 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/textproto"
"strconv"
"time"
)

Expand Down Expand Up @@ -139,17 +140,29 @@ func SetResponseErrorHandler(c HTTPClient, u Unmarshaller, ptr any) DoFunc {
if err != nil {
return resp, err
}

if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized {
return resp, nil
}

b, err := io.ReadAll(resp.Body)
if err != nil {
return resp, err
}
if err = u(b, ptr); err != nil {

var rErr appStoreAPIErrorResp
if err = u(b, &rErr); err != nil {
return resp, err
}
return resp, nil

if rErr.ErrorCode == 4290000 {
retryAfter, err := strconv.ParseInt(req.Header.Get("Retry-After"), 10, 64)
if err == nil {
return resp, &Error{errorCode: rErr.ErrorCode, errorMessage: rErr.ErrorMessage, retryAfter: retryAfter}
}
}

return resp, &Error{errorCode: rErr.ErrorCode, errorMessage: rErr.ErrorMessage}
}
}

Expand Down
71 changes: 48 additions & 23 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ func (c *StoreClient) GetALLSubscriptionStatuses(ctx context.Context, originalTr
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)
client = SetRequest(ctx, client, http.MethodGet, URL)
rsp := &StatusResponse{}
client = SetResponseBodyHandler(client, json.Unmarshal, rsp)

_, err := client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}
if err != nil {
return nil, err
}
Expand All @@ -124,12 +129,17 @@ func (c *StoreClient) GetTransactionInfo(ctx context.Context, transactionId stri
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)
client = SetRequest(ctx, client, http.MethodGet, URL)
rsp := &TransactionInfoResponse{}
client = SetResponseBodyHandler(client, json.Unmarshal, rsp)

_, err := client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}
if err != nil {
return nil, err
}
Expand All @@ -144,12 +154,17 @@ func (c *StoreClient) LookupOrderID(ctx context.Context, orderId string) (*Order
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)
client = SetRequest(ctx, client, http.MethodGet, URL)
rsp := &OrderLookupResponse{}
client = SetResponseBodyHandler(client, json.Unmarshal, rsp)

_, err := client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}
if err != nil {
return nil, err
}
Expand All @@ -165,26 +180,27 @@ func (c *StoreClient) GetTransactionHistory(ctx context.Context, originalTransac
query = &url.Values{}
}

var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
client = RequireResponseStatus(client, http.StatusOK)

for {
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)

rsp := HistoryResponse{}
client = SetRequest(ctx, client, http.MethodGet, URL+"?"+query.Encode())
client = SetResponseBodyHandler(client, json.Unmarshal, &rsp)
_, err = client.Do(nil)
if err != nil {
return nil, err
client = SetRequest(ctx, client, http.MethodGet, URL+"?"+query.Encode())
_, errDo := client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}

responses = append(responses, &rsp)
if !rsp.HasMore {
return
if errDo != nil {
return nil, errDo
}

if rsp.Revision != "" {
responses = append(responses, &rsp)
if rsp.HasMore && rsp.Revision != "" {
query.Set("revision", rsp.Revision)
} else {
return
Expand All @@ -198,18 +214,22 @@ func (c *StoreClient) GetTransactionHistory(ctx context.Context, originalTransac
func (c *StoreClient) GetRefundHistory(ctx context.Context, originalTransactionId string) (responses []*RefundLookupResponse, err error) {
baseURL := c.hostUrl + PathRefundHistory
baseURL = strings.Replace(baseURL, "{originalTransactionId}", originalTransactionId, -1)

URL := baseURL
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
client = RequireResponseStatus(client, http.StatusOK)

for {
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)
rsp := RefundLookupResponse{}
client = SetRequest(ctx, client, http.MethodGet, URL)
client = SetResponseBodyHandler(client, json.Unmarshal, &rsp)
_, err = client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -324,19 +344,24 @@ func (c *StoreClient) GetNotificationHistory(ctx context.Context, body Notificat
}

URL := baseURL
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
client = RequireResponseStatus(client, http.StatusOK)

for {
var client HTTPClient
client = c.httpCli
client = SetInitializer(client, c.initHttpClient)
apiErr := &Error{}
client = SetResponseErrorHandler(client, json.Unmarshal, &apiErr)
client = RequireResponseStatus(client, http.StatusOK)
rsp := NotificationHistoryResponses{}
rsp.NotificationHistory = make([]NotificationHistoryResponseItem, 0)

client = SetRequest(ctx, client, http.MethodPost, URL)
client = SetRequestBodyJSON(client, bodyBuf)
client = SetResponseBodyHandler(client, json.Unmarshal, &rsp)
_, err = client.Do(nil)
if apiErr.errorCode != 0 {
return nil, apiErr
}
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2c35c74

Please sign in to comment.