-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathShopifySdk.java
More file actions
1305 lines (1126 loc) · 60.6 KB
/
ShopifySdk.java
File metadata and controls
1305 lines (1126 loc) · 60.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.shopify;
import com.shopify.mappers.ObjectMapperProvider;
import com.shopify.model.*;
import java.net.URI;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.github.rholder.retry.Attempt;
import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.RetryListener;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.shopify.exceptions.ShopifyClientException;
import com.shopify.exceptions.ShopifyEmptyLineItemsException;
import com.shopify.exceptions.ShopifyErrorResponseException;
import com.shopify.exceptions.ShopifyIncompatibleApiException;
import com.shopify.mappers.LegacyToFulfillmentOrderMapping;
import com.shopify.mappers.ResponseEntityToStringMapper;
import com.shopify.mappers.ShopifySdkObjectMapper;
public class ShopifySdk {
static final String API_VERSION_PREFIX = "api";
private static final long TWO_HUNDRED_MILLISECONDS = 200L;
private static final String EQUALS = "=";
private static final String AMPERSAND = "&";
private static final String REL_PREVIOUS_HEADER_KEY = "previous";
private static final String REL_NEXT_HEADER_KEY = "next";
private static final String EMPTY_STRING = "";
static final String RETRY_AFTER_HEADER = "Retry-After";
private static final String MINIMUM_REQUEST_RETRY_DELAY_CANNOT_BE_LARGER_THAN_MAXIMUM_REQUEST_RETRY_DELAY_MESSAGE = "Maximum request retry delay must be larger than minimum request retry delay.";
private static final String INVALID_MINIMUM_REQUEST_RETRY_DELAY_MESSAGE = "Minimum request retry delay cannot be set lower than 200 milliseconds.";
private static final Logger LOGGER = LoggerFactory.getLogger(ShopifySdk.class);
private static final String HTTPS = "https://";
private static final String API_TARGET = ".myshopify.com/admin";
static final String ACCESS_TOKEN_HEADER = "X-Shopify-Access-Token";
static final String DEPRECATED_REASON_HEADER = "X-Shopify-API-Deprecated-Reason";
static final String OAUTH = "oauth";
static final String REVOKE = "revoke";
static final String ACCESS_TOKEN = "access_token";
static final String PRODUCTS = "products";
static final String THEMES = "themes";
static final String ASSETS = "assets";
static final String WEBHOOKS = "webhooks";
static final String VARIANTS = "variants";
static final String CUSTOM_COLLECTIONS = "custom_collections";
static final String RECURRING_APPLICATION_CHARGES = "recurring_application_charges";
static final String ORDERS = "orders";
static final String FULFILLMENTS = "fulfillments";
static final String FULFILLMENT_ORDERS = "fulfillment_orders";
static final String ACTIVATE = "activate";
static final String IMAGES = "images";
static final String SHOP = "shop";
static final String COUNT = "count";
static final String CLOSE = "close";
static final String CANCEL = "cancel";
static final String MOVE = "move";
static final String UPDATE_TRACKING = "update_tracking";
static final String METAFIELDS = "metafields";
static final String RISKS = "risks";
static final String LOCATIONS = "locations";
static final String INVENTORY_LEVELS = "inventory_levels";
static final String JSON = ".json";
static final String LIMIT_QUERY_PARAMETER = "limit";
static final String ASSET_KEY_PARAMETER="asset[key]";
static final String PAGE_INFO_QUERY_PARAMETER = "page_info";
static final String STATUS_QUERY_PARAMETER = "status";
static final String ANY_STATUSES = "any";
static final String CREATED_AT_MIN_QUERY_PARAMETER = "created_at_min";
static final String CREATED_AT_MAX_QUERY_PARAMETER = "created_at_max";
static final String UPDATED_AT_MIN_QUERY_PARAMETER = "updated_at_min";
static final String UPDATED_AT_MAX_QUERY_PARAMETER = "updated_at_max";
static final String ATTRIBUTION_APP_ID_QUERY_PARAMETER = "attribution_app_id";
static final String IDS_QUERY_PARAMETER = "ids";
static final String SINCE_ID_QUERY_PARAMETER = "since_id";
static final String QUERY_QUERY_PARAMETER = "query";
static final String CALCULATE = "calculate";
static final String REFUNDS = "refunds";
static final String TRANSACTIONS = "transactions";
static final String GIFT_CARDS = "gift_cards";
static final String REFUND_KIND = "refund";
static final String SET = "set";
private static final String CLIENT_ID = "client_id";
private static final String CLIENT_SECRET = "client_secret";
private static final String AUTHORIZATION_CODE = "code";
private static final int DEFAULT_REQUEST_LIMIT = 50;
private static final int MAX_REQUEST_LIMIT = 250;
private static final int TOO_MANY_REQUESTS_STATUS_CODE = 429;
private static final int UNPROCESSABLE_ENTITY_STATUS_CODE = 422;
private static final int LOCKED_STATUS_CODE = 423;
private static final String SHOP_RETRIEVED_MESSAGE = "Starting to make calls for Shopify store with ID of {} and name of {}";
private static final String COULD_NOT_BE_SAVED_SHOPIFY_ERROR_MESSAGE = "could not successfully be saved";
private static final String RETRY_FAILED_MESSAGE = "Request retry has failed.";
private static final String DEPRECATED_SHOPIFY_CALL_ERROR_MESSAGE = "Shopify call is deprecated. Please take note of the X-Shopify-API-Deprecated-Reason and correct the call.\nRequest Location of {}\nResponse Status Code of {}\nResponse Headers of:\n{}";
static final String GENERAL_ACCESS_TOKEN_EXCEPTION_MESSAGE = "There was a problem generating access token using shop subdomain of %s and authorization code of %s.";
private static final Long DEFAULT_MAXIMUM_REQUEST_RETRY_TIMEOUT_IN_MILLISECONDS = 180000L;
private static final Long DEFAULT_MAXIMUM_REQUEST_RETRY_RANDOM_DELAY_IN_MILLISECONDS = 5000L;
private static final Long DEFAULT_MINIMUM_REQUEST_RETRY_RANDOM_DELAY_IN_MILLISECONDS = 1000L;
private static final long DEFAULT_READ_TIMEOUT_IN_MILLISECONDS = 15000L;
private static final long DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS = 60000L;
private String shopSubdomain;
private String apiUrl;
private String apiVersion;
private String clientId;
private String clientSecret;
private String authorizationToken;
private WebTarget webTarget;
private String accessToken;
private long minimumRequestRetryRandomDelayMilliseconds;
private long maximumRequestRetryRandomDelayMilliseconds;
private long maximumRequestRetryTimeoutMilliseconds;
private static final Client CLIENT = buildClient();
private static final String CUSTOMERS = "customers";
private static final String SEARCH = "search";
public static interface OptionalsStep {
/**
* The Shopify SDK uses random waits in between retry attempts. Minimum
* duration time to wait before retrying a failed request. Value must
* also be less than
* {@link #withMaximumRequestRetryRandomDelay(int, TimeUnit) Maximum
* Request Retry Random Delay}.<br>
* Default value is: 1 second.
*
* @param duration of the minimum retry delay
* @param timeUnit the time unit to be used (miliseconds, seconds, etc...)
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withMinimumRequestRetryRandomDelay(int duration, TimeUnit timeUnit);
/**
* The Shopify SDK uses random waits in between retry attempts. Maximum
* duration time to wait before retrying a failed request. Value must
* also be more than
* {@link #withMinimumRequestRetryRandomDelay(int, TimeUnit) Minimum
* Request Retry Random Delay}.<br>
* Default value is: 5 seconds.
*
* @param duration of the maximum retry delay
* @param timeUnit the time unit to be used (miliseconds, seconds, etc...)
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withMaximumRequestRetryRandomDelay(int duration, TimeUnit timeUnit);
/**
* Maximum duration time to keep attempting requests <br>
* Default value is: 3 minutes.
*
* @param duration of the request timeout
* @param timeUnit the time unit to be used (miliseconds, seconds, etc...)
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withMaximumRequestRetryTimeout(int duration, TimeUnit timeUnit);
/**
* The duration to wait when connecting to Shopify's API. <br>
* Default value is: 1 minute.
*
* @param duration of the connection timeout
* @param timeUnit the time unit to be used (miliseconds, seconds, etc...)
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withConnectionTimeout(int duration, TimeUnit timeUnit);
/**
* The duration to attempt to read a response from Shopify's API. <br>
* Default value is: 15 seconds.
*
* @param duration of the duration to be considered
* @param timeUnit the time unit to be used (miliseconds, seconds, etc...)
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withReadTimeout(int duration, TimeUnit timeUnit);
/**
* String representation of the version you want to use. If not
* populated, this will use shopify oldest stable version. Although this
* is not recommended so you can target a set of shopify features. Ex:
* '2020-10' '2020-07' '2020-04'. If you are specifying the API URL
* ensure you leave off the version if you are using this.
*
* @param apiVersion current apiVersion value
* @return {@link OptionalsStep} the OptionalsStep interface
*/
OptionalsStep withApiVersion(final String apiVersion);
ShopifySdk build();
}
public static interface AuthorizationTokenStep {
OptionalsStep withAuthorizationToken(final String authorizationToken);
}
public static interface ClientSecretStep {
AuthorizationTokenStep withClientSecret(final String clientSecret);
}
public static interface AccessTokenStep {
OptionalsStep withAccessToken(final String accessToken);
ClientSecretStep withClientId(final String clientId);
}
public static interface SubdomainStep {
AccessTokenStep withSubdomain(final String subdomain);
AccessTokenStep withApiUrl(final String apiUrl);
}
public static SubdomainStep newBuilder() {
return new Steps();
}
protected ShopifySdk(final Steps steps) {
if (steps != null) {
this.shopSubdomain = steps.subdomain;
this.accessToken = steps.accessToken;
this.clientId = steps.clientId;
this.clientSecret = steps.clientSecret;
this.authorizationToken = steps.authorizationToken;
this.apiUrl = steps.apiUrl;
this.apiVersion = steps.apiVersion;
this.minimumRequestRetryRandomDelayMilliseconds = steps.minimumRequestRetryRandomDelayMilliseconds;
this.maximumRequestRetryRandomDelayMilliseconds = steps.maximumRequestRetryRandomDelayMilliseconds;
this.maximumRequestRetryTimeoutMilliseconds = steps.maximumRequestRetryTimeoutMilliseconds;
CLIENT.property(ClientProperties.CONNECT_TIMEOUT, Math.toIntExact(steps.connectionTimeoutMilliseconds));
CLIENT.property(ClientProperties.READ_TIMEOUT, Math.toIntExact(steps.readTimeoutMilliseconds));
validateConstructionOfShopifySdk();
}
}
private void validateConstructionOfShopifySdk() {
if (this.minimumRequestRetryRandomDelayMilliseconds < TWO_HUNDRED_MILLISECONDS) {
throw new IllegalArgumentException(INVALID_MINIMUM_REQUEST_RETRY_DELAY_MESSAGE);
}
if (minimumRequestRetryRandomDelayMilliseconds > maximumRequestRetryRandomDelayMilliseconds) {
throw new IllegalArgumentException(
MINIMUM_REQUEST_RETRY_DELAY_CANNOT_BE_LARGER_THAN_MAXIMUM_REQUEST_RETRY_DELAY_MESSAGE);
}
}
protected static class Steps
implements SubdomainStep, ClientSecretStep, AuthorizationTokenStep, AccessTokenStep, OptionalsStep {
private String subdomain;
private String accessToken;
private String clientId;
private String clientSecret;
private String authorizationToken;
private String apiUrl;
private String apiVersion;
private long minimumRequestRetryRandomDelayMilliseconds = DEFAULT_MINIMUM_REQUEST_RETRY_RANDOM_DELAY_IN_MILLISECONDS;
private long maximumRequestRetryRandomDelayMilliseconds = DEFAULT_MAXIMUM_REQUEST_RETRY_RANDOM_DELAY_IN_MILLISECONDS;
private long maximumRequestRetryTimeoutMilliseconds = DEFAULT_MAXIMUM_REQUEST_RETRY_TIMEOUT_IN_MILLISECONDS;
private long connectionTimeoutMilliseconds = DEFAULT_CONNECTION_TIMEOUT_IN_MILLISECONDS;
private long readTimeoutMilliseconds = DEFAULT_READ_TIMEOUT_IN_MILLISECONDS;
@Override
public ShopifySdk build() {
return new ShopifySdk(this);
}
@Override
public OptionalsStep withAccessToken(final String accessToken) {
this.accessToken = accessToken;
return this;
}
@Override
public AccessTokenStep withSubdomain(final String subdomain) {
this.subdomain = subdomain;
return this;
}
@Override
public AccessTokenStep withApiUrl(final String apiUrl) {
this.apiUrl = apiUrl;
return this;
}
@Override
public ClientSecretStep withClientId(final String clientId) {
this.clientId = clientId;
return this;
}
@Override
public OptionalsStep withAuthorizationToken(final String authorizationToken) {
this.authorizationToken = authorizationToken;
return this;
}
@Override
public AuthorizationTokenStep withClientSecret(final String clientSecret) {
this.clientSecret = clientSecret;
return this;
}
@Override
public OptionalsStep withMinimumRequestRetryRandomDelay(final int duration, final TimeUnit timeUnit) {
this.minimumRequestRetryRandomDelayMilliseconds = timeUnit.toMillis(duration);
return this;
}
@Override
public OptionalsStep withMaximumRequestRetryRandomDelay(final int duration, final TimeUnit timeUnit) {
this.maximumRequestRetryRandomDelayMilliseconds = timeUnit.toMillis(duration);
return this;
}
@Override
public OptionalsStep withMaximumRequestRetryTimeout(final int duration, final TimeUnit timeUnit) {
this.maximumRequestRetryTimeoutMilliseconds = timeUnit.toMillis(duration);
return this;
}
@Override
public OptionalsStep withConnectionTimeout(final int duration, final TimeUnit timeUnit) {
this.connectionTimeoutMilliseconds = timeUnit.toMillis(duration);
return this;
}
@Override
public OptionalsStep withReadTimeout(final int duration, final TimeUnit timeUnit) {
this.readTimeoutMilliseconds = timeUnit.toMillis(duration);
return this;
}
@Override
public OptionalsStep withApiVersion(final String apiVersion) {
this.apiVersion = apiVersion;
return this;
}
}
public boolean revokeOAuthToken() {
try {
final Response response = delete(getUnversionedWebTarget().path(OAUTH).path(REVOKE));
return Status.OK.getStatusCode() == response.getStatus();
} catch (final ShopifyErrorResponseException e) {
return false;
}
}
public ShopifyProduct getProduct(final String productId) {
final Response response = get(getWebTarget().path(PRODUCTS).path(productId));
final ShopifyProductRoot shopifyProductRootResponse = response.readEntity(ShopifyProductRoot.class);
return shopifyProductRootResponse.getProduct();
}
public ShopifyVariant getVariant(final String variantId) {
final Response response = get(getWebTarget().path(VARIANTS).path(variantId));
final ShopifyVariantRoot shopifyVariantRootResponse = response.readEntity(ShopifyVariantRoot.class);
return shopifyVariantRootResponse.getVariant();
}
public ShopifyPage<ShopifyProduct> getProducts(final int pageSize) {
return this.getProducts(null, pageSize);
}
public ShopifyPage<ShopifyProduct> getProducts(final String pageInfo, final int pageSize) {
final Response response = get(getWebTarget().path(PRODUCTS).queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyProductsRoot shopifyProductsRoot = response.readEntity(ShopifyProductsRoot.class);
return mapPagedResponse(shopifyProductsRoot.getProducts(), response);
}
public ShopifyProducts getProducts() {
final List<ShopifyProduct> shopifyProducts = new LinkedList<>();
ShopifyPage<ShopifyProduct> shopifyProductsPage = getProducts(DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} products from first page", shopifyProductsPage.size());
shopifyProducts.addAll(shopifyProductsPage);
while (shopifyProductsPage.getNextPageInfo() != null) {
shopifyProductsPage = getProducts(shopifyProductsPage.getNextPageInfo(), DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} products from page {}", shopifyProductsPage.size(),
shopifyProductsPage.getNextPageInfo());
shopifyProducts.addAll(shopifyProductsPage);
}
return new ShopifyProducts(shopifyProducts);
}
public List<ShopifyTheme> getThemes() {
final List<ShopifyTheme> shopifyThemes = new LinkedList<>();
ShopifyPage<ShopifyTheme> shopifyThemesPage = getThemes(MAX_REQUEST_LIMIT);
LOGGER.info("Retrieved {} themes from first page", shopifyThemesPage.size());
shopifyThemes.addAll(shopifyThemesPage);
while (shopifyThemesPage.getNextPageInfo() != null) {
shopifyThemesPage = getThemes(shopifyThemesPage.getNextPageInfo(), MAX_REQUEST_LIMIT);
LOGGER.info("Retrieved {} themes from page {}", shopifyThemesPage.size(),
shopifyThemesPage.getNextPageInfo());
shopifyThemes.addAll(shopifyThemesPage);
}
return shopifyThemes;
}
public ShopifyPage<ShopifyTheme> getThemes(final int pageSize) {
return this.getThemes(null, pageSize);
}
public ShopifyPage<ShopifyTheme> getThemes(final String pageInfo, final int pageSize) {
final Response response = get(getWebTarget().path(THEMES).queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyThemesRoot shopifyThemesRoot = response.readEntity(ShopifyThemesRoot.class);
return mapPagedResponse(shopifyThemesRoot.getThemes(), response);
}
public ShopifyAsset getAsset(String themeId, String assetKey) {
final Response response = get(getWebTarget().path(THEMES).path(themeId).path(ASSETS).queryParam(ASSET_KEY_PARAMETER,assetKey));
final ShopifyAssetRoot shopifyAssetRootResponse = response.readEntity(ShopifyAssetRoot.class);
return shopifyAssetRootResponse.getAsset();
}
public List<ShopifyAsset> getAssets(String themeId) {
final List<ShopifyAsset> shopifyAssets = new LinkedList<>();
ShopifyPage<ShopifyAsset> shopifyAssetsPage = getAssets(MAX_REQUEST_LIMIT, themeId);
LOGGER.info("Retrieved {} assets from first page", shopifyAssetsPage.size());
shopifyAssets.addAll(shopifyAssetsPage);
while (shopifyAssetsPage.getNextPageInfo() != null) {
shopifyAssetsPage = getAssets(shopifyAssetsPage.getNextPageInfo(), MAX_REQUEST_LIMIT, themeId);
LOGGER.info("Retrieved {} assets from page {}", shopifyAssetsPage.size(),
shopifyAssetsPage.getNextPageInfo());
shopifyAssets.addAll(shopifyAssetsPage);
}
return shopifyAssets;
}
public ShopifyPage<ShopifyAsset> getAssets(final String pageInfo, final int pageSize, String themeId) {
final Response response = get(getWebTarget().path(THEMES).path(themeId).path(ASSETS).queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyAssertsRoot shopifyAsseRoot = response.readEntity(ShopifyAssertsRoot.class);
return mapPagedResponse(shopifyAsseRoot.getAssets(), response);
}
public ShopifyPage<ShopifyAsset> getAssets(final int pageSize, String themeId) {
return this.getAssets(null, pageSize, themeId);
}
public ShopifyTheme createTheme(ShopifyTheme request) {
final ShopifyThemeRoot themeRoot = new ShopifyThemeRoot();
themeRoot.setTheme(request);
final Response response = post(getWebTarget().path(THEMES), themeRoot);
final ShopifyThemeRoot result = response.readEntity(ShopifyThemeRoot.class);
return result.getTheme();
}
public boolean deleteTheme(final String themeId) {
final Response response = delete(getWebTarget().path(THEMES).path(themeId));
return Status.OK.getStatusCode() == response.getStatus();
}
public ShopifyWebhook createWebhook(ShopifyWebhook request) {
final ShopifyWebhookRoot webhookRoot = new ShopifyWebhookRoot();
webhookRoot.setWebhook(request);
final Response response = post(getWebTarget().path(WEBHOOKS), webhookRoot);
final ShopifyWebhookRoot result = response.readEntity(ShopifyWebhookRoot.class);
return result.getWebhook();
}
public ShopifyWebhook getWebhook(String webhookId) {
final Response response = get(getWebTarget().path(WEBHOOKS).path(webhookId));
final ShopifyWebhookRoot result = response.readEntity(ShopifyWebhookRoot.class);
return result.getWebhook();
}
public boolean deleteWebhook(final String webhookId) {
final Response response = delete(getWebTarget().path(WEBHOOKS).path(webhookId));
return Status.OK.getStatusCode() == response.getStatus();
}
public List<ShopifyWebhook> getWebhooks() {
final List<ShopifyWebhook> shopifyWebhooks = new LinkedList<>();
ShopifyPage<ShopifyWebhook> shopifyWebhooksPage = getWebhooks(MAX_REQUEST_LIMIT);
LOGGER.info("Retrieved {} Webhooks from first page", shopifyWebhooksPage.size());
shopifyWebhooks.addAll(shopifyWebhooksPage);
while (shopifyWebhooksPage.getNextPageInfo() != null) {
shopifyWebhooksPage = getWebhooks(shopifyWebhooksPage.getNextPageInfo(), MAX_REQUEST_LIMIT);
LOGGER.info("Retrieved {} Webhooks from page {}", shopifyWebhooksPage.size(),
shopifyWebhooksPage.getNextPageInfo());
shopifyWebhooks.addAll(shopifyWebhooksPage);
}
return shopifyWebhooks;
}
public ShopifyPage<ShopifyWebhook> getWebhooks(final String pageInfo, final int pageSize) {
final Response response = get(getWebTarget().path(WEBHOOKS).queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyWebhooksRoot shopifyWebhooksRoot = response.readEntity(ShopifyWebhooksRoot.class);
return mapPagedResponse(shopifyWebhooksRoot.getWebhooks(), response);
}
public ShopifyPage<ShopifyWebhook> getWebhooks(final int pageSize) {
return this.getWebhooks(null, pageSize);
}
public int getProductCount() {
final Response response = get(getWebTarget().path(PRODUCTS).path(COUNT));
final Count count = response.readEntity(Count.class);
return count.getCount();
}
public ShopifyPage<ShopifyCustomCollection> getCustomCollections(final int pageSize) {
final Response response = get(
getWebTarget().path(CUSTOM_COLLECTIONS).queryParam(LIMIT_QUERY_PARAMETER, pageSize));
final ShopifyCustomCollectionsRoot shopifyCustomCollectionsRoot = response
.readEntity(ShopifyCustomCollectionsRoot.class);
return mapPagedResponse(shopifyCustomCollectionsRoot.getCustomCollections(), response);
}
public ShopifyPage<ShopifyCustomCollection> getCustomCollections(final String pageInfo, final int pageSize) {
final Response response = get(getWebTarget().path(CUSTOM_COLLECTIONS)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize).queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyCustomCollectionsRoot shopifyCustomCollectionsRoot = response
.readEntity(ShopifyCustomCollectionsRoot.class);
return mapPagedResponse(shopifyCustomCollectionsRoot.getCustomCollections(), response);
}
public List<ShopifyCustomCollection> getCustomCollections() {
final List<ShopifyCustomCollection> shopifyCustomCollections = new LinkedList<>();
ShopifyPage<ShopifyCustomCollection> customCollectionsPage = getCustomCollections(DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} custom collections from first page", customCollectionsPage.size());
shopifyCustomCollections.addAll(customCollectionsPage);
while (customCollectionsPage.getNextPageInfo() != null) {
customCollectionsPage = getCustomCollections(customCollectionsPage.getNextPageInfo(),
DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} custom collections from page info {}", customCollectionsPage.size(),
customCollectionsPage.getNextPageInfo());
shopifyCustomCollections.addAll(customCollectionsPage);
}
return shopifyCustomCollections;
}
public ShopifyCustomCollection createCustomCollection(
final ShopifyCustomCollectionCreationRequest shopifyCustomCollectionCreationRequest) {
final ShopifyCustomCollectionRoot shopifyCustomCollectionRootRequest = new ShopifyCustomCollectionRoot();
final ShopifyCustomCollection shopifyCustomCollection = shopifyCustomCollectionCreationRequest.getRequest();
shopifyCustomCollectionRootRequest.setCustomCollection(shopifyCustomCollection);
final Response response = post(getWebTarget().path(CUSTOM_COLLECTIONS), shopifyCustomCollectionRootRequest);
final ShopifyCustomCollectionRoot shopifyCustomCollectionRootResponse = response
.readEntity(ShopifyCustomCollectionRoot.class);
return shopifyCustomCollectionRootResponse.getCustomCollection();
}
public ShopifyShop getShop() {
final Response response = get(getWebTarget().path(SHOP));
return response.readEntity(ShopifyShop.class);
}
public ShopifyProduct createProduct(final ShopifyProductCreationRequest shopifyProductCreationRequest) {
final ShopifyProductRoot shopifyProductRootRequest = new ShopifyProductRoot();
final ShopifyProduct shopifyProduct = shopifyProductCreationRequest.getRequest();
shopifyProductRootRequest.setProduct(shopifyProduct);
final Response response = post(getWebTarget().path(PRODUCTS), shopifyProductRootRequest);
final ShopifyProductRoot shopifyProductRootResponse = response.readEntity(ShopifyProductRoot.class);
final ShopifyProduct createdShopifyProduct = shopifyProductRootResponse.getProduct();
return updateProductImages(shopifyProductCreationRequest, createdShopifyProduct);
}
public ShopifyProduct updateProduct(final ShopifyProductUpdateRequest shopifyProductUpdateRequest) {
final ShopifyProductRoot shopifyProductRootRequest = new ShopifyProductRoot();
final ShopifyProduct shopifyProduct = shopifyProductUpdateRequest.getRequest();
shopifyProductRootRequest.setProduct(shopifyProduct);
final Response response = put(getWebTarget().path(PRODUCTS).path(shopifyProduct.getId()),
shopifyProductRootRequest);
final ShopifyProductRoot shopifyProductRootResponse = response.readEntity(ShopifyProductRoot.class);
final ShopifyProduct updatedShopifyProduct = shopifyProductRootResponse.getProduct();
return updateProductImages(shopifyProductUpdateRequest, updatedShopifyProduct);
}
public ShopifyVariant updateVariant(final ShopifyVariantUpdateRequest shopifyVariantUpdateRequest) {
final ShopifyVariant shopifyVariant = shopifyVariantUpdateRequest.getRequest();
final String shopifyVariantId = shopifyVariant.getId();
if (StringUtils.isNotBlank(shopifyVariantUpdateRequest.getImageSource())) {
final ShopifyImageRoot shopifyImageRootRequest = new ShopifyImageRoot();
final Image imageRequest = new Image();
imageRequest.setSource(shopifyVariantUpdateRequest.getImageSource());
final List<Metafield> metafields = ImageAltTextCreationRequest.newBuilder()
.withImageAltText(shopifyVariant.getTitle()).build();
imageRequest.setMetafields(metafields);
imageRequest.setVariantIds(Arrays.asList(shopifyVariantId));
shopifyImageRootRequest.setImage(imageRequest);
final String productId = shopifyVariant.getProductId();
final Response response = post(getWebTarget().path(PRODUCTS).path(productId).path(IMAGES),
shopifyImageRootRequest);
final ShopifyImageRoot shopifyImageRootResponse = response.readEntity(ShopifyImageRoot.class);
final Image createdImage = shopifyImageRootResponse.getImage();
shopifyVariant.setImageId(createdImage.getId());
}
final ShopifyVariantRoot shopifyVariantRootRequest = new ShopifyVariantRoot();
shopifyVariantRootRequest.setVariant(shopifyVariant);
final Response response = put(getWebTarget().path(VARIANTS).path(shopifyVariantId), shopifyVariantRootRequest);
final ShopifyVariantRoot shopifyVariantRootResponse = response.readEntity(ShopifyVariantRoot.class);
return shopifyVariantRootResponse.getVariant();
}
public boolean deleteProduct(final String productId) {
final Response response = delete(getWebTarget().path(PRODUCTS).path(productId));
return Status.OK.getStatusCode() == response.getStatus();
}
public ShopifyRecurringApplicationCharge createRecurringApplicationCharge(
final ShopifyRecurringApplicationChargeCreationRequest shopifyRecurringApplicationChargeCreationRequest) {
final ShopifyRecurringApplicationChargeRoot shopifyRecurringApplicationChargeRootRequest = new ShopifyRecurringApplicationChargeRoot();
final ShopifyRecurringApplicationCharge shopifyRecurringApplicationChargeRequest = shopifyRecurringApplicationChargeCreationRequest
.getRequest();
shopifyRecurringApplicationChargeRootRequest
.setRecurringApplicationCharge(shopifyRecurringApplicationChargeRequest);
final Response response = post(getWebTarget().path(RECURRING_APPLICATION_CHARGES),
shopifyRecurringApplicationChargeRootRequest);
final ShopifyRecurringApplicationChargeRoot shopifyRecurringApplicationChargeRootResponse = response
.readEntity(ShopifyRecurringApplicationChargeRoot.class);
return shopifyRecurringApplicationChargeRootResponse.getRecurringApplicationCharge();
}
public boolean deleteCharge(final String chargeId) {
final Response response = delete(getWebTarget().path(RECURRING_APPLICATION_CHARGES).path(chargeId));
return Status.OK.getStatusCode() == response.getStatus();
}
public ShopifyRecurringApplicationCharge getRecurringApplicationCharge(final String chargeId) {
final Response response = get(getWebTarget().path(RECURRING_APPLICATION_CHARGES).path(chargeId));
final ShopifyRecurringApplicationChargeRoot shopifyRecurringApplicationChargeRootResponse = response
.readEntity(ShopifyRecurringApplicationChargeRoot.class);
return shopifyRecurringApplicationChargeRootResponse.getRecurringApplicationCharge();
}
public ShopifyRecurringApplicationCharge activateRecurringApplicationCharge(final String chargeId) {
final ShopifyRecurringApplicationCharge shopifyRecurringApplicationChargeRequest = getRecurringApplicationCharge(
chargeId);
final Response response = post(getWebTarget().path(RECURRING_APPLICATION_CHARGES).path(chargeId).path(ACTIVATE),
shopifyRecurringApplicationChargeRequest);
final ShopifyRecurringApplicationChargeRoot shopifyRecurringApplicationChargeRootResponse = response
.readEntity(ShopifyRecurringApplicationChargeRoot.class);
return shopifyRecurringApplicationChargeRootResponse.getRecurringApplicationCharge();
}
public ShopifyOrder getOrder(final String orderId) {
final Response response = get(buildOrdersEndpoint().path(orderId));
final ShopifyOrderRoot shopifyOrderRootResponse = response.readEntity(ShopifyOrderRoot.class);
return shopifyOrderRootResponse.getOrder();
}
public List<ShopifyTransaction> getOrderTransactions(final String orderId) {
final Response response = get(buildOrdersEndpoint().path(orderId).path(TRANSACTIONS));
final ShopifyTransactionsRoot shopifyTransactionsRootResponse = response
.readEntity(ShopifyTransactionsRoot.class);
return shopifyTransactionsRootResponse.getTransactions();
}
public ShopifyPage<ShopifyOrder> getOrders() {
return getOrders(DEFAULT_REQUEST_LIMIT);
}
public ShopifyPage<ShopifyOrder> getOrders(final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(STATUS_QUERY_PARAMETER, ANY_STATUSES)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize));
return getOrders(response);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate) {
return getOrders(mininumCreationDate, DEFAULT_REQUEST_LIMIT);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate, final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(STATUS_QUERY_PARAMETER, ANY_STATUSES)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(CREATED_AT_MIN_QUERY_PARAMETER, mininumCreationDate.toString()));
return getOrders(response);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate, final DateTime maximumCreationDate) {
return getOrders(mininumCreationDate, maximumCreationDate, DEFAULT_REQUEST_LIMIT);
}
public ShopifyPage<ShopifyOrder> getUpdatedOrdersCreatedBefore(final DateTime minimumUpdatedAtDate,
final DateTime maximumUpdatedAtDate, final DateTime maximumCreatedAtDate, final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(STATUS_QUERY_PARAMETER, ANY_STATUSES)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(UPDATED_AT_MIN_QUERY_PARAMETER, minimumUpdatedAtDate.toString())
.queryParam(UPDATED_AT_MAX_QUERY_PARAMETER, maximumUpdatedAtDate.toString())
.queryParam(CREATED_AT_MAX_QUERY_PARAMETER, maximumCreatedAtDate.toString()));
return getOrders(response);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate, final DateTime maximumCreationDate,
final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(STATUS_QUERY_PARAMETER, ANY_STATUSES)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(CREATED_AT_MIN_QUERY_PARAMETER, mininumCreationDate.toString())
.queryParam(CREATED_AT_MAX_QUERY_PARAMETER, maximumCreationDate.toString()));
return getOrders(response);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate, final DateTime maximumCreationDate,
final String appId) {
return getOrders(mininumCreationDate, maximumCreationDate, appId, DEFAULT_REQUEST_LIMIT);
}
public ShopifyPage<ShopifyOrder> getOrders(final DateTime mininumCreationDate, final DateTime maximumCreationDate,
final String appId, final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(STATUS_QUERY_PARAMETER, ANY_STATUSES)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(CREATED_AT_MIN_QUERY_PARAMETER, mininumCreationDate.toString())
.queryParam(CREATED_AT_MAX_QUERY_PARAMETER, maximumCreationDate.toString())
.queryParam(ATTRIBUTION_APP_ID_QUERY_PARAMETER, appId));
return getOrders(response);
}
public ShopifyPage<ShopifyOrder> getOrders(final String pageInfo, final int pageSize) {
final Response response = get(buildOrdersEndpoint().queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
return getOrders(response);
}
/**
* Creates a fulfillment in shopify
*
* @deprecated starting in March 01, 2023 this method will be obsolete
* @param shopifyFulfillmentCreationRequest the fulfillment creaton request
* @return the newly created fulfillment
*/
@Deprecated
public ShopifyFulfillment createFulfillment(
final ShopifyFulfillmentCreationRequest shopifyFulfillmentCreationRequest) {
return this.createFulfillmentWithLegacyApi(shopifyFulfillmentCreationRequest);
}
/**
* Creates a fulfillment in shopify starting with api 2023-04
*
* @param shopifyFulfillmentCreationRequest the fulfillment creaton request
*
* @param fulfillmentOrders the fulfillment orders list to
* create the new fulfillment
* @return the newly created fulfillment
* @throws ShopifyEmptyLineItemsException in case we have a fulfillment
* associated with a fulfillmentOrder
* without supported action
*/
public ShopifyFulfillment createFulfillment(
final ShopifyFulfillmentCreationRequest shopifyFulfillmentCreationRequest,
List<ShopifyFulfillmentOrder> fulfillmentOrders) throws ShopifyEmptyLineItemsException {
return this.createFulfillmentWithFulfillmentOrderApi(shopifyFulfillmentCreationRequest, fulfillmentOrders);
}
/**
* Updates a fulfillment in shopify
*
* @deprecated starting in March 01, 2023 this method will be obsolete
* @param shopifyFulfillmentUpdateRequest the fulfillment update request
* @return the newly updated fulfillment
*/
@Deprecated
public ShopifyFulfillment updateFulfillment(final ShopifyFulfillmentUpdateRequest shopifyFulfillmentUpdateRequest) {
final ShopifyFulfillmentRoot shopifyFulfillmentRoot = new ShopifyFulfillmentRoot();
final ShopifyFulfillment shopifyFulfillment = shopifyFulfillmentUpdateRequest.getRequest();
shopifyFulfillmentRoot.setFulfillment(shopifyFulfillment);
final Response response = put(buildOrdersEndpoint().path(shopifyFulfillment.getOrderId()).path(FULFILLMENTS)
.path(shopifyFulfillment.getId()), shopifyFulfillmentRoot);
final ShopifyFulfillmentRoot shopifyFulfillmentRootResponse = response.readEntity(ShopifyFulfillmentRoot.class);
return shopifyFulfillmentRootResponse.getFulfillment();
}
/**
* Based on the api starting from 2023-04 it only updates tracking information
*
* @param shopifyFulfillmentUpdateRequest the information needed to update the
* fulfillment's tracking info
* @return the updated version of the shopify fulfillment
*/
public ShopifyFulfillment updateFulfillmentTrackingInfo(
final ShopifyFulfillmentUpdateRequest shopifyFulfillmentUpdateRequest) {
final ShopifyFulfillment shopifyFulfillment = shopifyFulfillmentUpdateRequest.getRequest();
final ShopifyUpdateFulfillmentPayloadRoot payload = LegacyToFulfillmentOrderMapping
.toUpdateShopifyFulfillmentPayloadRoot(shopifyFulfillment);
final Response response = post(
getWebTarget().path(FULFILLMENTS).path(shopifyFulfillment.getId()).path(UPDATE_TRACKING), payload);
final ShopifyFulfillmentRoot shopifyFulfillmentRootResponse = response.readEntity(ShopifyFulfillmentRoot.class);
return shopifyFulfillmentRootResponse.getFulfillment();
}
public ShopifyOrder createOrder(final ShopifyOrderCreationRequest shopifyOrderCreationRequest) {
final ShopifyOrderRoot shopifyOrderRoot = new ShopifyOrderRoot();
final ShopifyOrder shopifyOrder = shopifyOrderCreationRequest.getRequest();
shopifyOrderRoot.setOrder(shopifyOrder);
final Response response = post(buildOrdersEndpoint(), shopifyOrderRoot);
final ShopifyOrderRoot shopifyOrderRootResponse = response.readEntity(ShopifyOrderRoot.class);
return shopifyOrderRootResponse.getOrder();
}
public ShopifyOrder updateOrderShippingAddress(
final ShopifyOrderShippingAddressUpdateRequest shopifyOrderUpdateRequest) {
final ShopifyOrderUpdateRoot shopifyOrderRoot = new ShopifyOrderUpdateRoot();
shopifyOrderRoot.setOrder(shopifyOrderUpdateRequest);
final Response response = put(buildOrdersEndpoint().path(shopifyOrderUpdateRequest.getId()), shopifyOrderRoot);
final ShopifyOrderRoot shopifyOrderRootResponse = response.readEntity(ShopifyOrderRoot.class);
return shopifyOrderRootResponse.getOrder();
}
public ShopifyCustomer updateCustomer(final ShopifyCustomerUpdateRequest shopifyCustomerUpdateRequest) {
final ShopifyCustomerUpdateRoot shopifyCustomerUpdateRequestRoot = new ShopifyCustomerUpdateRoot();
shopifyCustomerUpdateRequestRoot.setCustomer(shopifyCustomerUpdateRequest);
final Response response = put(getWebTarget().path(CUSTOMERS).path(shopifyCustomerUpdateRequest.getId()),
shopifyCustomerUpdateRequestRoot);
final ShopifyCustomerRoot shopifyCustomerRootResponse = response.readEntity(ShopifyCustomerRoot.class);
return shopifyCustomerRootResponse.getCustomer();
}
public ShopifyCustomer getCustomer(final String customerId) {
final Response response = get(getWebTarget().path(CUSTOMERS).path(customerId));
final ShopifyCustomerRoot shopifyCustomerRootResponse = response.readEntity(ShopifyCustomerRoot.class);
return shopifyCustomerRootResponse.getCustomer();
}
public ShopifyPage<ShopifyCustomer> getCustomers(final ShopifyGetCustomersRequest shopifyGetCustomersRequest) {
WebTarget target = getWebTarget().path(CUSTOMERS);
if (shopifyGetCustomersRequest.getPageInfo() != null) {
target = target.queryParam(PAGE_INFO_QUERY_PARAMETER, shopifyGetCustomersRequest.getPageInfo());
}
if (shopifyGetCustomersRequest.getLimit() != 0) {
target = target.queryParam(LIMIT_QUERY_PARAMETER, shopifyGetCustomersRequest.getLimit());
} else {
target = target.queryParam(LIMIT_QUERY_PARAMETER, DEFAULT_REQUEST_LIMIT);
}
if (shopifyGetCustomersRequest.getIds() != null) {
target = target.queryParam(IDS_QUERY_PARAMETER, String.join(",", shopifyGetCustomersRequest.getIds()));
}
if (shopifyGetCustomersRequest.getSinceId() != null) {
target = target.queryParam(SINCE_ID_QUERY_PARAMETER, shopifyGetCustomersRequest.getSinceId());
}
if (shopifyGetCustomersRequest.getCreatedAtMin() != null) {
target = target.queryParam(CREATED_AT_MIN_QUERY_PARAMETER, shopifyGetCustomersRequest.getCreatedAtMin());
}
if (shopifyGetCustomersRequest.getCreatedAtMax() != null) {
target = target.queryParam(CREATED_AT_MAX_QUERY_PARAMETER, shopifyGetCustomersRequest.getCreatedAtMax());
}
final Response response = get(target);
return getCustomers(response);
}
public ShopifyPage<ShopifyCustomer> searchCustomers(final String query) {
final Response response = get(getWebTarget().path(CUSTOMERS).path(SEARCH)
.queryParam(QUERY_QUERY_PARAMETER, query).queryParam(LIMIT_QUERY_PARAMETER, DEFAULT_REQUEST_LIMIT));
return getCustomers(response);
}
public ShopifyFulfillment cancelFulfillment(final String orderId, final String fulfillmentId) {
final WebTarget buildOrdersEndpoint = buildOrdersEndpoint();
final Response response = post(
buildOrdersEndpoint.path(orderId).path(FULFILLMENTS).path(fulfillmentId).path(CANCEL),
new ShopifyFulfillment());
final ShopifyFulfillmentRoot shopifyFulfillmentRootResponse = response.readEntity(ShopifyFulfillmentRoot.class);
return shopifyFulfillmentRootResponse.getFulfillment();
}
public ShopifyOrder closeOrder(final String orderId) {
final Response response = post(buildOrdersEndpoint().path(orderId).path(CLOSE), new ShopifyOrder());
final ShopifyOrderRoot shopifyOrderRootResponse = response.readEntity(ShopifyOrderRoot.class);
return shopifyOrderRootResponse.getOrder();
}
public ShopifyOrder cancelOrder(final String orderId, final String reason) {
final ShopifyCancelOrderRequest shopifyCancelOrderRequest = new ShopifyCancelOrderRequest();
shopifyCancelOrderRequest.setReason(reason);
final Response response = post(buildOrdersEndpoint().path(orderId).path(CANCEL), shopifyCancelOrderRequest);
final ShopifyOrderRoot shopifyOrderRootResponse = response.readEntity(ShopifyOrderRoot.class);
return shopifyOrderRootResponse.getOrder();
}
public Metafield createVariantMetafield(
final ShopifyVariantMetafieldCreationRequest shopifyVariantMetafieldCreationRequest) {
final MetafieldRoot metafieldRoot = new MetafieldRoot();
metafieldRoot.setMetafield(shopifyVariantMetafieldCreationRequest.getRequest());
final Response response = post(getWebTarget().path(VARIANTS)
.path(shopifyVariantMetafieldCreationRequest.getVariantId()).path(METAFIELDS), metafieldRoot);
final MetafieldRoot metafieldRootResponse = response.readEntity(MetafieldRoot.class);
return metafieldRootResponse.getMetafield();
}
public List<Metafield> getVariantMetafields(final String variantId) {
final Response response = get(getWebTarget().path(VARIANTS).path(variantId).path(METAFIELDS));
final MetafieldsRoot metafieldsRootResponse = response.readEntity(MetafieldsRoot.class);
return metafieldsRootResponse.getMetafields();
}
public Metafield createProductMetafield(
final ShopifyProductMetafieldCreationRequest shopifyProductMetafieldCreationRequest) {
final MetafieldRoot metafieldRoot = new MetafieldRoot();
metafieldRoot.setMetafield(shopifyProductMetafieldCreationRequest.getRequest());
final Response response = post(getWebTarget().path(PRODUCTS)
.path(shopifyProductMetafieldCreationRequest.getProductId()).path(METAFIELDS), metafieldRoot);
final MetafieldRoot metafieldRootResponse = response.readEntity(MetafieldRoot.class);
return metafieldRootResponse.getMetafield();
}
public List<Metafield> getProductMetafields(final String productId) {
final Response response = get(getWebTarget().path(PRODUCTS).path(productId).path(METAFIELDS));
final MetafieldsRoot metafieldsRootResponse = response.readEntity(MetafieldsRoot.class);
return metafieldsRootResponse.getMetafields();
}
public List<ShopifyOrderRisk> getOrderRisks(final String orderId) {
final Response response = get(buildOrdersEndpoint().path(orderId).path(RISKS));
final ShopifyOrderRisksRoot shopifyOrderRisksRootResponse = response.readEntity(ShopifyOrderRisksRoot.class);
return shopifyOrderRisksRootResponse.getRisks();
}
public List<ShopifyLocation> getLocations() {
final String locationsEndpoint = new StringBuilder().append(LOCATIONS).append(JSON).toString();
final Response response = get(getWebTarget().path(locationsEndpoint));
final ShopifyLocationsRoot shopifyLocationRootResponse = response.readEntity(ShopifyLocationsRoot.class);
return shopifyLocationRootResponse.getLocations();
}
public ShopifyInventoryLevel updateInventoryLevel(final String inventoryItemId, final String locationId,
final long quantity) {
final ShopifyInventoryLevel shopifyInventoryLevel = new ShopifyInventoryLevel();
shopifyInventoryLevel.setAvailable(quantity);
shopifyInventoryLevel.setLocationId(locationId);
shopifyInventoryLevel.setInventoryItemId(inventoryItemId);
final Response response = post(getWebTarget().path(INVENTORY_LEVELS).path(SET), shopifyInventoryLevel);
final ShopifyInventoryLevelRoot shopifyInventoryLevelRootResponse = response
.readEntity(ShopifyInventoryLevelRoot.class);
return shopifyInventoryLevelRootResponse.getInventoryLevel();
}
public List<Metafield> getOrderMetafields(final String orderId) {
final Response response = get(buildOrdersEndpoint().path(orderId).path(METAFIELDS));
final MetafieldsRoot metafieldsRootResponse = response.readEntity(MetafieldsRoot.class);
return metafieldsRootResponse.getMetafields();
}
public ShopifyRefund refund(final ShopifyRefundCreationRequest shopifyRefundCreationRequest) {
final ShopifyRefund calculatedShopifyRefund = calculateRefund(shopifyRefundCreationRequest);
calculatedShopifyRefund.getTransactions().forEach(transaction -> transaction.setKind(REFUND_KIND));
final WebTarget path = buildOrdersEndpoint().path(shopifyRefundCreationRequest.getRequest().getOrderId())
.path(REFUNDS);
final ShopifyRefundRoot shopifyRefundRoot = new ShopifyRefundRoot();
shopifyRefundRoot.setRefund(calculatedShopifyRefund);
final Response response = post(path, shopifyRefundRoot);
final ShopifyRefundRoot shopifyRefundRootResponse = response.readEntity(ShopifyRefundRoot.class);
return shopifyRefundRootResponse.getRefund();
}
public ShopifyGiftCard createGiftCard(final ShopifyGiftCardCreationRequest shopifyGiftCardCreationRequest) {
final ShopifyGiftCardRoot shopifyGiftCardRoot = new ShopifyGiftCardRoot();
final ShopifyGiftCard shopifyGiftCard = shopifyGiftCardCreationRequest.getRequest();
shopifyGiftCardRoot.setGiftCard(shopifyGiftCard);
final Response response = post(getWebTarget().path(GIFT_CARDS), shopifyGiftCardRoot);
final ShopifyGiftCardRoot shopifyOrderRootResponse = response.readEntity(ShopifyGiftCardRoot.class);
return shopifyOrderRootResponse.getGiftCard();
}
public String getAccessToken() {
return accessToken;
}
private ShopifyPage<ShopifyCustomer> getCustomers(final Response response) {
final ShopifyCustomersRoot shopifyCustomersRootResponse = response.readEntity(ShopifyCustomersRoot.class);
return mapPagedResponse(shopifyCustomersRootResponse.getCustomers(), response);
}