Skip to content

Commit c81f2e3

Browse files
authored
Merge pull request #305 from lubnin/master
feat(appstore): Add configurable host to store client
2 parents 195f7cf + 6261211 commit c81f2e3

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

appstore/api/store.go

+28-50
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type StoreConfig struct {
4646
Sandbox bool // default is Production
4747
TokenIssuedAtFunc func() int64 // The token’s creation time func. Default is current timestamp.
4848
TokenExpiredAtFunc func() int64 // The token’s expiration time func. Default is one hour later.
49+
50+
// internal variables
51+
HostDebug string // can be used to override the host for testing
4952
}
5053

5154
type (
@@ -98,6 +101,7 @@ type StoreClient struct {
98101
Token *Token
99102
httpCli *http.Client
100103
cert *Cert
104+
host string
101105
}
102106

103107
// NewStoreClient create a appstore server api client
@@ -111,6 +115,7 @@ func NewStoreClient(config *StoreConfig) *StoreClient {
111115
httpCli: &http.Client{
112116
Timeout: 30 * time.Second,
113117
},
118+
host: getHost(config.Sandbox, config.HostDebug),
114119
}
115120
return client
116121
}
@@ -124,16 +129,24 @@ func NewStoreClientWithHTTPClient(config *StoreConfig, httpClient *http.Client)
124129
Token: token,
125130
cert: &Cert{},
126131
httpCli: httpClient,
132+
host: getHost(config.Sandbox, config.HostDebug),
127133
}
128134
return client
129135
}
130136

137+
func getHost(sandbox bool, debugHost string) string {
138+
if debugHost != "" {
139+
return debugHost
140+
}
141+
if sandbox {
142+
return HostSandBox
143+
}
144+
return HostProduction
145+
}
146+
131147
// GetALLSubscriptionStatuses https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses
132148
func (a *StoreClient) GetALLSubscriptionStatuses(ctx context.Context, originalTransactionId string, query *url.Values) (rsp *StatusResponse, err error) {
133-
URL := HostProduction + PathGetALLSubscriptionStatus
134-
if a.Token.Sandbox {
135-
URL = HostSandBox + PathGetALLSubscriptionStatus
136-
}
149+
URL := a.host + PathGetALLSubscriptionStatus
137150
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
138151
if query != nil {
139152
URL = URL + "?" + query.Encode()
@@ -157,10 +170,7 @@ func (a *StoreClient) GetALLSubscriptionStatuses(ctx context.Context, originalTr
157170

158171
// LookupOrderID https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id
159172
func (a *StoreClient) LookupOrderID(ctx context.Context, orderId string) (rsp *OrderLookupResponse, err error) {
160-
URL := HostProduction + PathLookUp
161-
if a.Token.Sandbox {
162-
URL = HostSandBox + PathLookUp
163-
}
173+
URL := a.host + PathLookUp
164174
URL = strings.Replace(URL, "{orderId}", orderId, -1)
165175
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
166176
if err != nil {
@@ -181,10 +191,7 @@ func (a *StoreClient) LookupOrderID(ctx context.Context, orderId string) (rsp *O
181191

182192
// GetTransactionHistory https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
183193
func (a *StoreClient) GetTransactionHistory(ctx context.Context, originalTransactionId string, query *url.Values) (responses []*HistoryResponse, err error) {
184-
URL := HostProduction + PathTransactionHistory
185-
if a.Token.Sandbox {
186-
URL = HostSandBox + PathTransactionHistory
187-
}
194+
URL := a.host + PathTransactionHistory
188195
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
189196

190197
if query == nil {
@@ -225,10 +232,7 @@ func (a *StoreClient) GetTransactionHistory(ctx context.Context, originalTransac
225232

226233
// GetTransactionInfo https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info
227234
func (a *StoreClient) GetTransactionInfo(ctx context.Context, transactionId string) (rsp *TransactionInfoResponse, err error) {
228-
URL := HostProduction + PathTransactionInfo
229-
if a.Token.Sandbox {
230-
URL = HostSandBox + PathTransactionInfo
231-
}
235+
URL := a.host + PathTransactionInfo
232236
URL = strings.Replace(URL, "{transactionId}", transactionId, -1)
233237

234238
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
@@ -250,10 +254,7 @@ func (a *StoreClient) GetTransactionInfo(ctx context.Context, transactionId stri
250254

251255
// GetRefundHistory https://developer.apple.com/documentation/appstoreserverapi/get_refund_history
252256
func (a *StoreClient) GetRefundHistory(ctx context.Context, originalTransactionId string) (responses []*RefundLookupResponse, err error) {
253-
baseURL := HostProduction + PathRefundHistory
254-
if a.Token.Sandbox {
255-
baseURL = HostSandBox + PathRefundHistory
256-
}
257+
baseURL := a.host + PathRefundHistory
257258
baseURL = strings.Replace(baseURL, "{originalTransactionId}", originalTransactionId, -1)
258259

259260
URL := baseURL
@@ -292,10 +293,7 @@ func (a *StoreClient) GetRefundHistory(ctx context.Context, originalTransactionI
292293

293294
// SendConsumptionInfo https://developer.apple.com/documentation/appstoreserverapi/send_consumption_information
294295
func (a *StoreClient) SendConsumptionInfo(ctx context.Context, originalTransactionId string, body ConsumptionRequestBody) (statusCode int, err error) {
295-
URL := HostProduction + PathConsumptionInfo
296-
if a.Token.Sandbox {
297-
URL = HostSandBox + PathConsumptionInfo
298-
}
296+
URL := a.host + PathConsumptionInfo
299297
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
300298

301299
bodyBuf := new(bytes.Buffer)
@@ -313,10 +311,7 @@ func (a *StoreClient) SendConsumptionInfo(ctx context.Context, originalTransacti
313311

314312
// ExtendSubscriptionRenewalDate https://developer.apple.com/documentation/appstoreserverapi/extend_a_subscription_renewal_date
315313
func (a *StoreClient) ExtendSubscriptionRenewalDate(ctx context.Context, originalTransactionId string, body ExtendRenewalDateRequest) (statusCode int, err error) {
316-
URL := HostProduction + PathExtendSubscriptionRenewalDate
317-
if a.Token.Sandbox {
318-
URL = HostSandBox + PathExtendSubscriptionRenewalDate
319-
}
314+
URL := a.host + PathExtendSubscriptionRenewalDate
320315
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
321316

322317
bodyBuf := new(bytes.Buffer)
@@ -334,10 +329,7 @@ func (a *StoreClient) ExtendSubscriptionRenewalDate(ctx context.Context, origina
334329

335330
// ExtendSubscriptionRenewalDateForAll https://developer.apple.com/documentation/appstoreserverapi/extend_subscription_renewal_dates_for_all_active_subscribers
336331
func (a *StoreClient) ExtendSubscriptionRenewalDateForAll(ctx context.Context, body MassExtendRenewalDateRequest) (statusCode int, err error) {
337-
URL := HostProduction + PathExtendSubscriptionRenewalDateForAll
338-
if a.Token.Sandbox {
339-
URL = HostSandBox + PathExtendSubscriptionRenewalDateForAll
340-
}
332+
URL := a.host + PathExtendSubscriptionRenewalDateForAll
341333

342334
bodyBuf := new(bytes.Buffer)
343335
err = json.NewEncoder(bodyBuf).Encode(body)
@@ -354,10 +346,7 @@ func (a *StoreClient) ExtendSubscriptionRenewalDateForAll(ctx context.Context, b
354346

355347
// GetSubscriptionRenewalDataStatus https://developer.apple.com/documentation/appstoreserverapi/get_status_of_subscription_renewal_date_extensions
356348
func (a *StoreClient) GetSubscriptionRenewalDataStatus(ctx context.Context, productId, requestIdentifier string) (statusCode int, rsp *MassExtendRenewalDateStatusResponse, err error) {
357-
URL := HostProduction + PathGetStatusOfSubscriptionRenewalDate
358-
if a.Token.Sandbox {
359-
URL = HostSandBox + PathGetStatusOfSubscriptionRenewalDate
360-
}
349+
URL := a.host + PathGetStatusOfSubscriptionRenewalDate
361350
URL = strings.Replace(URL, "{productId}", productId, -1)
362351
URL = strings.Replace(URL, "{requestIdentifier}", requestIdentifier, -1)
363352

@@ -404,12 +393,7 @@ func (a *StoreClient) GetAllNotificationHistory(ctx context.Context, body Notifi
404393
// GetNotificationHistory https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
405394
// Note: Notification history is available starting on June 6, 2022. Use a startDate of June 6, 2022 or later in your request.
406395
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest, paginationToken string) (rsp *NotificationHistoryResponses, err error) {
407-
baseURL := HostProduction + PathGetNotificationHistory
408-
if a.Token.Sandbox {
409-
baseURL = HostSandBox + PathGetNotificationHistory
410-
}
411-
412-
URL := baseURL
396+
URL := a.host + PathGetNotificationHistory
413397
if paginationToken != "" {
414398
query := url.Values{}
415399
query.Set("paginationToken", paginationToken)
@@ -440,20 +424,14 @@ func (a *StoreClient) GetNotificationHistory(ctx context.Context, body Notificat
440424

441425
// SendRequestTestNotification https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification
442426
func (a *StoreClient) SendRequestTestNotification(ctx context.Context) (int, []byte, error) {
443-
URL := HostProduction + PathRequestTestNotification
444-
if a.Token.Sandbox {
445-
URL = HostSandBox + PathRequestTestNotification
446-
}
427+
URL := a.host + PathRequestTestNotification
447428

448429
return a.Do(ctx, http.MethodPost, URL, nil)
449430
}
450431

451432
// GetTestNotificationStatus https://developer.apple.com/documentation/appstoreserverapi/get_test_notification_status
452433
func (a *StoreClient) GetTestNotificationStatus(ctx context.Context, testNotificationToken string) (int, []byte, error) {
453-
URL := HostProduction + PathGetTestNotificationStatus
454-
if a.Token.Sandbox {
455-
URL = HostSandBox + PathGetTestNotificationStatus
456-
}
434+
URL := a.host + PathGetTestNotificationStatus
457435
URL = strings.Replace(URL, "{testNotificationToken}", testNotificationToken, -1)
458436

459437
return a.Do(ctx, http.MethodGet, URL, nil)

0 commit comments

Comments
 (0)