Skip to content

Commit

Permalink
feat(appstore): fix the bug of NotificationHistoryRequest.Transaction…
Browse files Browse the repository at this point in the history
…Id json tag and implement two versions of get_notification_history, single page or all items.
  • Loading branch information
kaijietti committed Jul 3, 2023
1 parent 321f62e commit f5b2e10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
16 changes: 9 additions & 7 deletions appstore/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ type MassExtendRenewalDateStatusResponse struct {

// NotificationHistoryRequest https://developer.apple.com/documentation/appstoreserverapi/notificationhistoryrequest
type NotificationHistoryRequest struct {
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
OriginalTransactionId string `json:"originalTransactionId,omitempty"`
NotificationType appstore.NotificationTypeV2 `json:"notificationType,omitempty"`
NotificationSubtype appstore.SubtypeV2 `json:"notificationSubtype,omitempty"`
OnlyFailures bool `json:"onlyFailures"`
TransactionId string `json:"transactionId"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
NotificationType appstore.NotificationTypeV2 `json:"notificationType,omitempty"`
NotificationSubtype appstore.SubtypeV2 `json:"notificationSubtype,omitempty"`
OnlyFailures bool `json:"onlyFailures"`
TransactionId string `json:"transactionId,omitempty"`
// Use transactionId instead.
// Deprecated.
OriginalTransactionId string `json:"originalTransactionId,omitempty"`
}

// NotificationHistoryResponses https://developer.apple.com/documentation/appstoreserverapi/notificationhistoryresponse
Expand Down
72 changes: 41 additions & 31 deletions appstore/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,54 +322,64 @@ func (a *StoreClient) GetSubscriptionRenewalDataStatus(ctx context.Context, prod
return statusCode, rsp, nil
}

// GetAllNotificationHistory returns all the NotificationHistoryResponseItem using the paginationToken on behalf of you.
func (a *StoreClient) GetAllNotificationHistory(ctx context.Context, body NotificationHistoryRequest, duration time.Duration) (responses []NotificationHistoryResponseItem, err error) {
paginationToken := ""
for {
rsp, err := a.GetNotificationHistory(ctx, body, paginationToken)
if err != nil {
return nil, err
}

responses = append(responses, rsp.NotificationHistory...)

if rsp.HasMore {
paginationToken = rsp.PaginationToken
} else {
break
}

time.Sleep(duration)
}

return responses, nil
}

// GetNotificationHistory https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
// Note: Notification history is available starting on June 6, 2022. Use a startDate of June 6, 2022 or later in your request.
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest) (responses []NotificationHistoryResponseItem, err error) {
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest, paginationToken string) (rsp *NotificationHistoryResponses, err error) {
baseURL := HostProduction + PathGetNotificationHistory
if a.Token.Sandbox {
baseURL = HostSandBox + PathGetNotificationHistory
}

URL := baseURL
if paginationToken != "" {
query := url.Values{}
query.Set("paginationToken", paginationToken)
URL += "?" + query.Encode()
}

bodyBuf := new(bytes.Buffer)
err = json.NewEncoder(bodyBuf).Encode(body)
if err != nil {
return nil, err
}

URL := baseURL
for {
rsp := NotificationHistoryResponses{}
rsp.NotificationHistory = make([]NotificationHistoryResponseItem, 0)

statusCode, rspBody, errOmit := a.Do(ctx, http.MethodPost, URL, bodyBuf)
if errOmit != nil {
return nil, errOmit
}

if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}

err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return nil, err
}

responses = append(responses, rsp.NotificationHistory...)
if !rsp.HasMore {
break
}
statusCode, rspBody, err := a.Do(ctx, http.MethodPost, URL, bodyBuf)
if err != nil {
return nil, err
}

data := url.Values{}
if rsp.HasMore && rsp.PaginationToken != "" {
data.Set("paginationToken", rsp.PaginationToken)
URL = baseURL + "?" + data.Encode()
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}

time.Sleep(10 * time.Millisecond)
if err = json.Unmarshal(rspBody, &rsp); err != nil {
return nil, err
}

return responses, nil
return rsp, nil
}

// SendRequestTestNotification https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification
Expand Down

0 comments on commit f5b2e10

Please sign in to comment.