Skip to content

Commit bf740ab

Browse files
authored
Merge pull request #224 from kaijietti/master
fix and refactor appstore get_notification_history
2 parents 321f62e + 2f1d4e3 commit bf740ab

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

appstore/api/model.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,15 @@ type MassExtendRenewalDateStatusResponse struct {
185185

186186
// NotificationHistoryRequest https://developer.apple.com/documentation/appstoreserverapi/notificationhistoryrequest
187187
type NotificationHistoryRequest struct {
188-
StartDate int64 `json:"startDate"`
189-
EndDate int64 `json:"endDate"`
190-
OriginalTransactionId string `json:"originalTransactionId,omitempty"`
191-
NotificationType appstore.NotificationTypeV2 `json:"notificationType,omitempty"`
192-
NotificationSubtype appstore.SubtypeV2 `json:"notificationSubtype,omitempty"`
193-
OnlyFailures bool `json:"onlyFailures"`
194-
TransactionId string `json:"transactionId"`
188+
StartDate int64 `json:"startDate"`
189+
EndDate int64 `json:"endDate"`
190+
NotificationType appstore.NotificationTypeV2 `json:"notificationType,omitempty"`
191+
NotificationSubtype appstore.SubtypeV2 `json:"notificationSubtype,omitempty"`
192+
OnlyFailures bool `json:"onlyFailures"`
193+
TransactionId string `json:"transactionId,omitempty"`
194+
// Use transactionId instead.
195+
// Deprecated.
196+
OriginalTransactionId string `json:"originalTransactionId,omitempty"`
195197
}
196198

197199
// NotificationHistoryResponses https://developer.apple.com/documentation/appstoreserverapi/notificationhistoryresponse

appstore/api/store.go

+45-31
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ func (a *StoreClient) GetSubscriptionRenewalDataStatus(ctx context.Context, prod
310310
URL = strings.Replace(URL, "{requestIdentifier}", requestIdentifier, -1)
311311

312312
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
313+
if err != nil {
314+
return statusCode, nil, err
315+
}
316+
313317
if statusCode != http.StatusOK {
314318
return statusCode, nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
315319
}
@@ -322,54 +326,64 @@ func (a *StoreClient) GetSubscriptionRenewalDataStatus(ctx context.Context, prod
322326
return statusCode, rsp, nil
323327
}
324328

329+
// GetAllNotificationHistory returns all the NotificationHistoryResponseItem using the paginationToken on behalf of you.
330+
func (a *StoreClient) GetAllNotificationHistory(ctx context.Context, body NotificationHistoryRequest, duration time.Duration) (responses []NotificationHistoryResponseItem, err error) {
331+
paginationToken := ""
332+
for {
333+
rsp, err := a.GetNotificationHistory(ctx, body, paginationToken)
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
responses = append(responses, rsp.NotificationHistory...)
339+
340+
if rsp.HasMore {
341+
paginationToken = rsp.PaginationToken
342+
} else {
343+
break
344+
}
345+
346+
time.Sleep(duration)
347+
}
348+
349+
return responses, nil
350+
}
351+
325352
// GetNotificationHistory https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
326353
// Note: Notification history is available starting on June 6, 2022. Use a startDate of June 6, 2022 or later in your request.
327-
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest) (responses []NotificationHistoryResponseItem, err error) {
354+
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest, paginationToken string) (rsp *NotificationHistoryResponses, err error) {
328355
baseURL := HostProduction + PathGetNotificationHistory
329356
if a.Token.Sandbox {
330357
baseURL = HostSandBox + PathGetNotificationHistory
331358
}
332359

360+
URL := baseURL
361+
if paginationToken != "" {
362+
query := url.Values{}
363+
query.Set("paginationToken", paginationToken)
364+
URL += "?" + query.Encode()
365+
}
366+
333367
bodyBuf := new(bytes.Buffer)
334368
err = json.NewEncoder(bodyBuf).Encode(body)
335369
if err != nil {
336370
return nil, err
337371
}
338372

339-
URL := baseURL
340-
for {
341-
rsp := NotificationHistoryResponses{}
342-
rsp.NotificationHistory = make([]NotificationHistoryResponseItem, 0)
343-
344-
statusCode, rspBody, errOmit := a.Do(ctx, http.MethodPost, URL, bodyBuf)
345-
if errOmit != nil {
346-
return nil, errOmit
347-
}
348-
349-
if statusCode != http.StatusOK {
350-
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
351-
}
352-
353-
err = json.Unmarshal(rspBody, &rsp)
354-
if err != nil {
355-
return nil, err
356-
}
357-
358-
responses = append(responses, rsp.NotificationHistory...)
359-
if !rsp.HasMore {
360-
break
361-
}
373+
statusCode, rspBody, err := a.Do(ctx, http.MethodPost, URL, bodyBuf)
374+
if err != nil {
375+
return nil, err
376+
}
362377

363-
data := url.Values{}
364-
if rsp.HasMore && rsp.PaginationToken != "" {
365-
data.Set("paginationToken", rsp.PaginationToken)
366-
URL = baseURL + "?" + data.Encode()
367-
}
378+
if statusCode != http.StatusOK {
379+
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
380+
}
368381

369-
time.Sleep(10 * time.Millisecond)
382+
if err = json.Unmarshal(rspBody, &rsp); err != nil {
383+
return nil, err
370384
}
371385

372-
return responses, nil
386+
return rsp, nil
373387
}
374388

375389
// SendRequestTestNotification https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification

0 commit comments

Comments
 (0)