printipy 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
printipy/__init__.py ADDED
File without changes
printipy/api.py ADDED
@@ -0,0 +1,937 @@
1
+ import base64
2
+ import json
3
+ import os
4
+ from copy import deepcopy
5
+ from json import JSONDecodeError
6
+ from typing import List, Optional, Dict, Union, Any
7
+ from dataclasses import dataclass, field
8
+ from dataclasses_json import dataclass_json, config
9
+
10
+ import requests
11
+ from requests import Response
12
+
13
+
14
+ def _exclude_if_none(value):
15
+ return value is None
16
+
17
+
18
+ @dataclass_json
19
+ @dataclass
20
+ class Shop:
21
+ id: str
22
+ title: str
23
+ sales_channel: str
24
+
25
+
26
+ @dataclass_json
27
+ @dataclass
28
+ class Blueprint:
29
+ id: int
30
+ title: str
31
+ description: str
32
+ brand: str
33
+ model: str
34
+ images: List[str]
35
+
36
+
37
+ @dataclass_json
38
+ @dataclass
39
+ class Location:
40
+ address1: str
41
+ city: str
42
+ country: str
43
+ region: str
44
+ zip: str
45
+ address2: Optional[str] = field(default=None)
46
+
47
+
48
+ @dataclass_json
49
+ @dataclass
50
+ class Address:
51
+ first_name: str
52
+ last_name: str
53
+ address1: str
54
+ city: str
55
+ country: str
56
+ region: str
57
+ zip: str
58
+ address2: Optional[str] = field(default=None)
59
+ email: Optional[str] = field(default=None)
60
+ phone: Optional[str] = field(default=None)
61
+ company: Optional[str] = field(
62
+ default=None, metadata=config(exclude=_exclude_if_none)
63
+ )
64
+
65
+
66
+ @dataclass_json
67
+ @dataclass
68
+ class PrintProvider:
69
+ id: int
70
+ title: str
71
+ location: Optional[Location] = field(
72
+ default=None, metadata=config(exclude=_exclude_if_none)
73
+ )
74
+
75
+
76
+ @dataclass_json
77
+ @dataclass
78
+ class VariantOption:
79
+ color: Optional[str] = field(
80
+ default=None, metadata=config(exclude=_exclude_if_none)
81
+ )
82
+ size: Optional[str] = field(
83
+ default=None, metadata=config(exclude=_exclude_if_none)
84
+ )
85
+ paper: Optional[str] = field(
86
+ default=None, metadata=config(exclude=_exclude_if_none)
87
+ )
88
+ quantity: Optional[str] = field(
89
+ default=None, metadata=config(exclude=_exclude_if_none)
90
+ )
91
+
92
+
93
+ @dataclass_json
94
+ @dataclass
95
+ class VariantPlaceholder:
96
+ position: str
97
+ height: int
98
+ width: int
99
+
100
+
101
+ @dataclass_json
102
+ @dataclass
103
+ class Variant:
104
+ id: int
105
+ title: str
106
+ options: VariantOption
107
+ placeholders: List[VariantPlaceholder]
108
+
109
+
110
+ @dataclass_json
111
+ @dataclass
112
+ class PrintProviderVariants:
113
+ id: int
114
+ title: str
115
+ variants: List[Variant]
116
+
117
+ def get_variant_ids(self):
118
+ return [x.id for x in self.variants]
119
+
120
+
121
+ @dataclass_json
122
+ @dataclass
123
+ class ShippingInfoHandlingTime:
124
+ value: int
125
+ unit: str
126
+
127
+
128
+ @dataclass_json
129
+ @dataclass
130
+ class ShippingInfoProfileCost:
131
+ cost: int
132
+ currency: str
133
+
134
+
135
+ @dataclass_json
136
+ @dataclass
137
+ class ShippingInfoProfile:
138
+ variant_ids: List[int]
139
+ first_item: ShippingInfoProfileCost
140
+ additional_items: ShippingInfoProfileCost
141
+ countries: List[str]
142
+
143
+
144
+ @dataclass_json
145
+ @dataclass
146
+ class ShippingInfo:
147
+ handling_time: ShippingInfoHandlingTime
148
+ profiles: List[ShippingInfoProfile]
149
+
150
+
151
+ @dataclass_json
152
+ @dataclass
153
+ class ShippingCost:
154
+ standard: int
155
+ express: int
156
+
157
+
158
+ @dataclass_json
159
+ @dataclass
160
+ class ShippingEstimateLineItemByProduct:
161
+ product_id: str
162
+ variant_id: int
163
+ quantity: int
164
+
165
+
166
+ @dataclass_json
167
+ @dataclass
168
+ class ShippingEstimateLineItemByVariant:
169
+ print_provider_id: int
170
+ blueprint_id: int
171
+ variant_id: int
172
+ quantity: int
173
+
174
+
175
+ @dataclass_json
176
+ @dataclass
177
+ class ShippingEstimateLineItemBySku:
178
+ sku: str
179
+ quantity: int
180
+
181
+
182
+ @dataclass_json
183
+ @dataclass
184
+ class CreateShippingEstimate:
185
+ line_items: List[
186
+ Union[ShippingEstimateLineItemByProduct, ShippingEstimateLineItemByVariant, ShippingEstimateLineItemBySku]]
187
+ address_to: Address
188
+
189
+
190
+ @dataclass_json
191
+ @dataclass
192
+ class ProductOptionValue:
193
+ id: str
194
+ title: str
195
+
196
+
197
+ @dataclass_json
198
+ @dataclass
199
+ class ProductOption:
200
+ name: str
201
+ type: str
202
+ values: List[ProductOptionValue]
203
+
204
+
205
+ @dataclass_json
206
+ @dataclass
207
+ class ProductVariant:
208
+ id: int
209
+ price: int
210
+ is_enabled: bool
211
+ sku: Optional[str] = None
212
+ cost: Optional[int] = None
213
+ title: Optional[str] = None
214
+ grams: Optional[int] = None
215
+ is_default: Optional[bool] = None
216
+ is_available: Optional[bool] = None
217
+ options: Optional[List[int]] = None
218
+ quantity: Optional[int] = None
219
+
220
+
221
+ @dataclass_json
222
+ @dataclass
223
+ class ProductImage:
224
+ src: str
225
+ variant_ids: List[int]
226
+ position: str
227
+ is_default: bool
228
+ is_selected_for_publishing: Optional[bool] = None
229
+
230
+
231
+ @dataclass_json
232
+ @dataclass
233
+ class PrintAreaInfo:
234
+ x: float
235
+ y: float
236
+ scale: float
237
+ angle: float
238
+
239
+
240
+ @dataclass_json
241
+ @dataclass
242
+ class PlaceholderImage(PrintAreaInfo):
243
+ id: str
244
+ name: Optional[str] = None
245
+ type: Optional[str] = None
246
+ height: Optional[int] = None
247
+ width: Optional[int] = None
248
+
249
+
250
+ @dataclass_json
251
+ @dataclass
252
+ class ProductPlaceholder:
253
+ position: str
254
+ images: List[PlaceholderImage]
255
+
256
+
257
+ @dataclass_json
258
+ @dataclass
259
+ class ProductPrintArea:
260
+ variant_ids: List[int]
261
+ placeholders: List[ProductPlaceholder]
262
+ background: Optional[str] = None
263
+
264
+
265
+ @dataclass_json
266
+ @dataclass
267
+ class ProductExternal:
268
+ id: str
269
+ handle: str
270
+ shipping_template_id: Optional[str] = None
271
+
272
+
273
+ @dataclass_json
274
+ @dataclass
275
+ class Product:
276
+ id: str
277
+ title: str
278
+ description: str
279
+ tags: List[str]
280
+ options: List[ProductOption]
281
+ variants: List[ProductVariant]
282
+ images: List[ProductImage]
283
+ created_at: str
284
+ updated_at: str
285
+ visible: bool
286
+ is_locked: bool
287
+ blueprint_id: int
288
+ user_id: int
289
+ shop_id: int
290
+ print_provider_id: int
291
+ print_areas: List[ProductPrintArea]
292
+ # sales_channel_properties: List
293
+ # print_details: List
294
+ twodaydelivery_enabled: Optional[bool] = None
295
+ external: Optional[ProductExternal] = None
296
+
297
+
298
+ @dataclass_json
299
+ @dataclass
300
+ class Publish:
301
+ title: bool = True
302
+ description: bool = True
303
+ images: bool = True
304
+ variants: bool = True
305
+ tags: bool = True
306
+ keyFeatures: bool = True
307
+ shipping_template: bool = True
308
+
309
+
310
+ @dataclass_json
311
+ @dataclass
312
+ class PublishingSucceededExternal:
313
+ id: str
314
+ handle: str
315
+
316
+
317
+ @dataclass_json
318
+ @dataclass
319
+ class PublishingSucceeded:
320
+ external: PublishingSucceededExternal
321
+
322
+
323
+ @dataclass_json
324
+ @dataclass
325
+ class LineItem:
326
+ product_id: str
327
+ quantity: int
328
+ variant_id: int
329
+ print_provider_id: int
330
+ cost: int
331
+ shipping_cost: int
332
+ status: str
333
+ metadata: Dict
334
+ sent_to_production_at: Optional[str] = None
335
+ fulfilled_at: Optional[str] = None
336
+
337
+
338
+ @dataclass_json
339
+ @dataclass
340
+ class Shipment:
341
+ carrier: str
342
+ number: str
343
+ url: str
344
+ delivered_at: str
345
+
346
+
347
+ @dataclass_json
348
+ @dataclass
349
+ class Order:
350
+ id: str
351
+ address_to: Address
352
+ line_items: List[LineItem]
353
+ metadata: Dict
354
+ total_price: int
355
+ total_shipping: int
356
+ total_tax: int
357
+ status: str
358
+ shipping_method: int
359
+ created_at: str
360
+ sent_to_production_at: Optional[str] = None
361
+ shipments: Optional[List[Shipment]] = None
362
+ fulfilled_at: Optional[str] = None
363
+ fulfilment_type: Optional[str] = None
364
+
365
+
366
+ @dataclass_json
367
+ @dataclass
368
+ class __CreateOrderLineItemBase:
369
+ variant_id: int
370
+ quantity: int
371
+
372
+
373
+ @dataclass_json
374
+ @dataclass
375
+ class CreateOrderLineItem(__CreateOrderLineItemBase):
376
+ product_id: str
377
+
378
+
379
+ @dataclass_json
380
+ @dataclass
381
+ class _CreateOrder:
382
+ pass
383
+
384
+
385
+ @dataclass_json
386
+ @dataclass
387
+ class CreateOrderExistingProduct(_CreateOrder):
388
+ external_id: str
389
+ label: str
390
+ line_items: List[CreateOrderLineItem]
391
+ shipping_method: int
392
+ send_shipping_notification: bool
393
+ address_to: Address
394
+
395
+
396
+ @dataclass_json
397
+ @dataclass
398
+ class CreateOrderLineItemSimpleProcessing(__CreateOrderLineItemBase):
399
+ print_provider_id: int
400
+ blueprint_id: int
401
+ print_areas: Dict[str, Any]
402
+
403
+
404
+ @dataclass_json
405
+ @dataclass
406
+ class CreateOrderSimpleImageProcessing(CreateOrderExistingProduct):
407
+ external_id: str
408
+ label: str
409
+ line_items: List[CreateOrderLineItemSimpleProcessing]
410
+ shipping_method: int
411
+ send_shipping_notification: bool
412
+ address_to: Address
413
+
414
+
415
+ @dataclass_json
416
+ @dataclass
417
+ class CreateOrderLineItemAdvancedProcessingPrintAreaInfo(PrintAreaInfo):
418
+ src: str
419
+
420
+
421
+ @dataclass_json
422
+ @dataclass
423
+ class CreateOrderLineItemAdvancedProcessing(__CreateOrderLineItemBase):
424
+ blueprint_id: int
425
+ print_provider_id: int
426
+ print_areas: Dict[str, List[PrintAreaInfo]]
427
+
428
+
429
+ @dataclass_json
430
+ @dataclass
431
+ class CreateOrderAdvancedImageProcessing(_CreateOrder):
432
+ external_id: str
433
+ label: str
434
+ line_items: List[CreateOrderLineItemAdvancedProcessing]
435
+ shipping_method: int
436
+ send_shipping_notification: bool
437
+ address_to: Address
438
+
439
+
440
+ @dataclass_json
441
+ @dataclass
442
+ class CreateOrderLineItemPrintDetails(CreateOrderLineItemSimpleProcessing):
443
+ print_details: Dict[str, Any]
444
+
445
+
446
+ @dataclass_json
447
+ @dataclass
448
+ class CreateOrderPrintDetails(_CreateOrder):
449
+ external_id: str
450
+ label: str
451
+ line_items = List[CreateOrderLineItemPrintDetails]
452
+ shipping_method: int
453
+ send_shipping_notification: bool
454
+ address_to: Address
455
+
456
+
457
+ @dataclass_json
458
+ @dataclass
459
+ class CreateOrderLineItemSku:
460
+ sku: str
461
+ quantity: int
462
+
463
+
464
+ @dataclass_json
465
+ @dataclass
466
+ class CreateOrderSku(_CreateOrder):
467
+ external_id: str
468
+ label: str
469
+ line_items = List[CreateOrderLineItemSku]
470
+ shipping_method: int
471
+ send_shipping_notification: bool
472
+ address_to: Address
473
+
474
+
475
+ @dataclass_json
476
+ @dataclass
477
+ class Artwork:
478
+ id: str
479
+ file_name: str
480
+ height: int
481
+ width: int
482
+ size: int
483
+ mime_type: str
484
+ preview_url: str
485
+ upload_time: str
486
+
487
+
488
+ @dataclass_json
489
+ @dataclass
490
+ class Webhook:
491
+ id: str
492
+ shop_id: str
493
+ url: str
494
+ topic: str
495
+
496
+
497
+ @dataclass_json
498
+ @dataclass
499
+ class CreateWebhook:
500
+ url: str
501
+ topic: str
502
+
503
+
504
+ @dataclass_json
505
+ @dataclass
506
+ class UpdateWebhook:
507
+ url: Optional[str] = field(
508
+ default=None, metadata=config(exclude=_exclude_if_none)
509
+ )
510
+ topic: Optional[str] = field(
511
+ default=None, metadata=config(exclude=_exclude_if_none)
512
+ )
513
+
514
+
515
+ @dataclass_json
516
+ @dataclass
517
+ class CreateProductPrintAreaPlaceholderImage(PrintAreaInfo):
518
+ id: str
519
+
520
+
521
+ @dataclass_json
522
+ @dataclass
523
+ class CreateProductPrintAreaPlaceholder:
524
+ position: str
525
+ images: List[CreateProductPrintAreaPlaceholderImage]
526
+
527
+
528
+ @dataclass_json
529
+ @dataclass
530
+ class CreateProductPrintArea:
531
+ variant_ids: List[int]
532
+ placeholders: List[CreateProductPrintAreaPlaceholder]
533
+
534
+
535
+ @dataclass_json
536
+ @dataclass
537
+ class CreateProductVariant:
538
+ id: int
539
+ price: int
540
+ is_enabled: bool
541
+
542
+
543
+ @dataclass_json
544
+ @dataclass
545
+ class CreateProduct:
546
+ title: str
547
+ description: str
548
+ blueprint_id: int
549
+ print_provider_id: int
550
+ variants: List[CreateProductVariant]
551
+ print_areas: List[CreateProductPrintArea]
552
+
553
+ def add_variant(self, variant: CreateProductVariant):
554
+ self.variants.append(variant)
555
+
556
+ def add_print_area(self, print_area: CreateProductPrintArea):
557
+ self.print_areas.append(print_area)
558
+
559
+
560
+ @dataclass_json
561
+ @dataclass
562
+ class UpdateProductExternal:
563
+ id: Optional[str] = field(
564
+ default=None, metadata=config(exclude=_exclude_if_none)
565
+ )
566
+ handle: Optional[str] = field(
567
+ default=None, metadata=config(exclude=_exclude_if_none)
568
+ )
569
+ shipping_template_id: Optional[str] = field(
570
+ default=None, metadata=config(exclude=_exclude_if_none)
571
+ )
572
+
573
+
574
+ @dataclass_json
575
+ @dataclass
576
+ class UpdateProduct:
577
+ title: Optional[str] = field(
578
+ default=None, metadata=config(exclude=_exclude_if_none)
579
+ )
580
+ description: Optional[str] = field(
581
+ default=None, metadata=config(exclude=_exclude_if_none)
582
+ )
583
+ blueprint_id: Optional[int] = field(
584
+ default=None, metadata=config(exclude=_exclude_if_none)
585
+ )
586
+ print_provider_id: Optional[int] = field(
587
+ default=None, metadata=config(exclude=_exclude_if_none)
588
+ )
589
+ variants: Optional[List[CreateProductVariant]] = field(
590
+ default=None, metadata=config(exclude=_exclude_if_none)
591
+ )
592
+ print_areas: Optional[List[CreateProductPrintArea]] = field(
593
+ default=None, metadata=config(exclude=_exclude_if_none)
594
+ )
595
+ external: Optional[UpdateProductExternal] = field(
596
+ default=None, metadata=config(exclude=_exclude_if_none)
597
+ )
598
+
599
+
600
+ class PrintiPyException(Exception):
601
+ pass
602
+
603
+
604
+ class InvalidScopeException(Exception):
605
+ pass
606
+
607
+
608
+ class InvalidRequestException(Exception):
609
+ pass
610
+
611
+
612
+ class PrintifyException(Exception):
613
+ pass
614
+
615
+
616
+ class ApiV1:
617
+ __api_url = 'https://api.printify.com'
618
+
619
+ def __init__(self, api_token: str):
620
+ self.api_token = api_token
621
+
622
+ @staticmethod
623
+ def __get_next_page_url(initial_url: str, info: Dict) -> Optional[str]:
624
+ next_page = info.get('next_page_url', None)
625
+ if not next_page:
626
+ return None
627
+ return f'{initial_url}{next_page}'
628
+
629
+ @staticmethod
630
+ def __check_status(resp: Response, url: str):
631
+ if resp.status_code == 400:
632
+ try:
633
+ info = json.loads(resp.text)
634
+ message = f'{info["message"]} {info["errors"]["reason"]}'
635
+ except JSONDecodeError:
636
+ message = f'Bad Request: {url}'
637
+ raise PrintiPyException(message)
638
+ elif resp.status_code == 403:
639
+ raise InvalidScopeException('This API key is not permitted to access this information.')
640
+ elif resp.status_code == 500:
641
+ raise InvalidRequestException(f'Bad request to {url}')
642
+ data = resp.json()
643
+ if 'error' in data:
644
+ raise PrintifyException(data['error'])
645
+ return data
646
+
647
+ def __get(self, url):
648
+ headers = {'Authorization': f'Bearer {self.api_token}'}
649
+ resp = requests.get(url, headers=headers)
650
+ data = self.__check_status(resp, url)
651
+ return data
652
+
653
+ def __post(self, url: str, data: Optional[Dict[str, Any]] = None):
654
+ headers = {'Authorization': f'Bearer {self.api_token}', 'content-type': 'application/json'}
655
+ resp = requests.post(url, headers=headers, json=data)
656
+ data = self.__check_status(resp, url)
657
+ return data
658
+
659
+ def __put(self, url, data):
660
+ headers = {'Authorization': f'Bearer {self.api_token}', 'content-type': 'application/json'}
661
+ resp = requests.put(url, headers=headers, json=data)
662
+ data = self.__check_status(resp, url)
663
+ return data
664
+
665
+ def __delete(self, url):
666
+ headers = {'Authorization': f'Bearer {self.api_token}'}
667
+ resp = requests.delete(url, headers=headers)
668
+ data = self.__check_status(resp, url)
669
+ return data
670
+
671
+ @staticmethod
672
+ def __parse(clazz, data: Union[List, Dict]):
673
+ if type(data) == list:
674
+ all_elements = []
675
+ for item in data:
676
+ all_elements.append(clazz.from_dict(item))
677
+ return all_elements
678
+ elif type(data) == dict:
679
+ return clazz.from_dict(data)
680
+ else:
681
+ raise Exception('Unable to parse response: was not a list or object')
682
+
683
+ # SHOPS
684
+ def get_shops(self) -> List[Shop]:
685
+ shops_url = f'{self.__api_url}/v1/shops.json'
686
+ shop_information = self.__get(shops_url)
687
+ return self.__parse(Shop, shop_information)
688
+
689
+ def delete_shop(self, shop: Shop):
690
+ delete_url = f'{self.__api_url}/v1/shops/{shop.id}/connection.json'
691
+ self.__delete(delete_url)
692
+
693
+ # CATALOG
694
+ def get_blueprints(self) -> List[Blueprint]:
695
+ blueprint_url = f'{self.__api_url}/v1/catalog/blueprints.json'
696
+ blueprint_information = self.__get(blueprint_url)
697
+ return self.__parse(Blueprint, blueprint_information)
698
+
699
+ def get_blueprint(self, blueprint_id: Union[str, int]) -> Optional[Blueprint]:
700
+ # GET / v1 / catalog / blueprints / {blueprint_id}.json
701
+ blueprint_url = f'{self.__api_url}/v1/catalog/blueprints/{blueprint_id}.json'
702
+ blueprint_information = self.__get(blueprint_url)
703
+ return self.__parse(Blueprint, blueprint_information)
704
+
705
+ def get_print_providers_for_blueprint(self, blueprint_id: Union[str, int]) -> List[PrintProvider]:
706
+ # GET / v1 / catalog / blueprints / {blueprint_id} / print_providers.json
707
+ print_providers_url = f'{self.__api_url}/v1/catalog/blueprints/{blueprint_id}/print_providers.json'
708
+ print_provider_information = self.__get(print_providers_url)
709
+ return self.__parse(PrintProvider, print_provider_information)
710
+
711
+ def get_variants(self, blueprint_id: Union[str, int], print_provider_id: Union[str, int]) -> PrintProviderVariants:
712
+ # GET / v1 / catalog / blueprints / {blueprint_id} / print_providers / {print_provider_id} / variants.json
713
+ variants_url = f'{self.__api_url}/v1/catalog/blueprints/{blueprint_id}/' \
714
+ f'print_providers/{print_provider_id}/variants.json'
715
+ variant_information = self.__get(variants_url)
716
+ return self.__parse(PrintProviderVariants, variant_information)
717
+
718
+ def get_shipping_info(self, blueprint_id: Union[str, int], print_provider_id: Union[str, int]) -> ShippingInfo:
719
+ # GET / v1 / catalog / blueprints / {blueprint_id} / print_providers / {print_provider_id} / shipping.json
720
+ shipping_url = f'{self.__api_url}/v1/catalog/blueprints/{blueprint_id}/' \
721
+ f'print_providers/{print_provider_id}/shipping.json'
722
+ shipping_information = self.__get(shipping_url)
723
+ return self.__parse(ShippingInfo, shipping_information)
724
+
725
+ def get_print_providers(self) -> List[PrintProvider]:
726
+ # GET / v1 / catalog / print_providers.json
727
+ print_providers_url = f'{self.__api_url}/v1/catalog/print_providers.json'
728
+ print_provider_information = self.__get(print_providers_url)
729
+ return self.__parse(PrintProvider, print_provider_information)
730
+
731
+ def get_print_provider(self, print_provider_id: Union[str, int]) -> Optional[PrintProvider]:
732
+ # GET / v1 / catalog / print_providers / {print_provider_id}.json
733
+ print_provider_url = f'{self.__api_url}/v1/catalog/print_providers/{print_provider_id}.json'
734
+ print_provider_information = self.__get(print_provider_url)
735
+ return self.__parse(PrintProvider, print_provider_information)
736
+
737
+ # PRODUCTS
738
+ def get_products(self, shop_id: Union[str, int], max_pages: int = 1) -> List[Product]:
739
+ # GET / v1 / shops / {shop_id} / products.json
740
+ initial_url = f'{self.__api_url}/v1/shops/{shop_id}/products.json'
741
+ products_url = deepcopy(initial_url)
742
+ all_products = []
743
+ for _ in range(max_pages):
744
+ if products_url is None:
745
+ break
746
+ products_information = self.__get(products_url)
747
+ all_products.extend(self.__parse(Product, products_information['data']))
748
+ products_url = self.__get_next_page_url(initial_url, products_information)
749
+ return all_products
750
+
751
+ def get_product(self, shop_id: Union[str, int], product_id: str) -> Optional[Product]:
752
+ # GET / v1 / shops / {shop_id} / products / {product_id}.json
753
+ product_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}.json'
754
+ product_information = self.__get(product_url)
755
+ return self.__parse(Product, product_information)
756
+
757
+ def create_product(self, shop_id: Union[str, int], create_product: CreateProduct) -> Product:
758
+ # POST / v1 / shops / {shop_id} / products.json
759
+ create_product_url = f'{self.__api_url}/v1/shops/{shop_id}/products.json'
760
+ product_information = self.__post(create_product_url, data=create_product.to_dict())
761
+ return self.__parse(Product, product_information)
762
+
763
+ def update_product(self, shop_id: Union[str, int], product_id: str, update_product: UpdateProduct) -> Product:
764
+ # PUT / v1 / shops / {shop_id} / products / {product_id}.json
765
+ update_product_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}.json'
766
+ product_information = self.__put(update_product_url, data=update_product.to_dict())
767
+ return self.__parse(Product, product_information)
768
+
769
+ def delete_product(self, shop_id: Union[str, int], product_id: str) -> True:
770
+ # DELETE / v1 / shops / {shop_id} / products / {product_id}.json
771
+ delete_product_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}.json'
772
+ self.__delete(delete_product_url)
773
+ return True
774
+
775
+ def publish_product(self, shop_id: Union[str, int], product_id: str, publish: Publish) -> True:
776
+ # POST / v1 / shops / {shop_id} / products / {product_id} / publish.json
777
+ publish_product_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}/publish.json'
778
+ self.__post(publish_product_url, data=publish.to_dict())
779
+ return True
780
+
781
+ def set_product_published_success(self, shop_id: Union[str, int], product_id: str,
782
+ publishing_succeeded: PublishingSucceeded) -> True:
783
+ # POST / v1 / shops / {shop_id} / products / {product_id} / publishing_succeeded.json
784
+ publishing_succeeded_url = f'{self.__api_url}/v1/shops/{shop_id}/products/' \
785
+ f'{product_id}/publishing_succeeded.json'
786
+ self.__post(publishing_succeeded_url, data=publishing_succeeded.to_dict())
787
+ return True
788
+
789
+ def set_product_published_failed(self, shop_id: Union[str, int], product_id: str, reason: str) -> True:
790
+ # POST / v1 / shops / {shop_id} / products / {product_id} / publishing_failed.json
791
+ publishing_failed_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}/publishing_failed.json'
792
+ self.__post(publishing_failed_url, data={"reason": reason})
793
+ return True
794
+
795
+ def unpublish_product(self, shop_id: Union[str, int], product_id: str) -> True:
796
+ # POST / v1 / shops / {shop_id} / products / {product_id} / unpublish.json
797
+ unpublish_product_url = f'{self.__api_url}/v1/shops/{shop_id}/products/{product_id}/unpublish.json'
798
+ self.__post(unpublish_product_url)
799
+ return True
800
+
801
+ # ORDERS
802
+ def get_orders(self, shop_id: Union[str, int], max_pages: int = 1) -> List[Order]:
803
+ # GET / v1 / shops / {shop_id} / orders.json
804
+ initial_url = f'{self.__api_url}/v1/shops/{shop_id}/orders.json'
805
+ orders_url = deepcopy(initial_url)
806
+ all_orders = []
807
+ for _ in range(max_pages):
808
+ if orders_url is None:
809
+ break
810
+ orders_information = self.__get(orders_url)
811
+ all_orders.extend(self.__parse(Order, orders_information['data']))
812
+ orders_url = self.__get_next_page_url(initial_url, orders_information)
813
+ return all_orders
814
+
815
+ def get_order(self, shop_id: Union[str, int], order_id: str) -> Order:
816
+ # GET / v1 / shops / {shop_id} / orders / {order_id}.json
817
+ order_url = f'{self.__api_url}/v1/shops/{shop_id}/orders/{order_id}.json'
818
+ order_information = self.__get(order_url)
819
+ return self.__parse(Order, order_information)
820
+
821
+ def __create_order(self, shop_id: Union[str, int], create_order: _CreateOrder) -> str:
822
+ # POST / v1 / shops / {shop_id} / orders.json
823
+ create_order_url = f'{self.__api_url}/v1/shops/{shop_id}/orders.json'
824
+ order_information = self.__post(create_order_url, data=create_order.to_dict())
825
+ return order_information['id']
826
+
827
+ def create_order_for_existing_product(self, shop_id: Union[str, int],
828
+ create_order: CreateOrderExistingProduct) -> str:
829
+ return self.__create_order(shop_id, create_order)
830
+
831
+ def create_order_with_simple_image_positioning(self, shop_id: Union[str, int],
832
+ create_order: CreateOrderExistingProduct) -> str:
833
+ return self.__create_order(shop_id, create_order)
834
+
835
+ def create_order_with_advanced_image_positioning(self, shop_id: Union[str, int],
836
+ create_order: CreateOrderAdvancedImageProcessing) -> str:
837
+ return self.__create_order(shop_id, create_order)
838
+
839
+ def create_order_with_print_details(self, shop_id: Union[str, int], create_order: CreateOrderPrintDetails) -> str:
840
+ return self.__create_order(shop_id, create_order)
841
+
842
+ def create_order_with_sku(self, shop_id: Union[str, int], create_order: CreateOrderSku) -> str:
843
+ return self.__create_order(shop_id, create_order)
844
+
845
+ def send_order_to_production(self, shop_id: Union[str, int], order_id: str) -> Order:
846
+ # POST / v1 / shops / {shop_id} / orders / {order_id} / send_to_production.json
847
+ send_order_to_production_url = f'{self.__api_url}/v1/shops/{shop_id}/orders/{order_id}/send_to_production.json'
848
+ order_information = self.__post(send_order_to_production_url)
849
+ return self.__parse(Order, order_information)
850
+
851
+ def calc_shipping_for_order(self, shop_id: Union[str, int],
852
+ create_shipping_cost_estimate: CreateShippingEstimate) -> ShippingCost:
853
+ # POST / v1 / shops / {shop_id} / orders / shipping.json
854
+ shipping_estimate_url = f'{self.__api_url}/v1/shops/{shop_id}/orders/shipping.json'
855
+ shipping_information = self.__post(shipping_estimate_url, data=create_shipping_cost_estimate.to_dict())
856
+ return self.__parse(ShippingCost, shipping_information)
857
+
858
+ def cancel_order(self, shop_id: Union[str, int], order_id: str) -> Order:
859
+ # POST / v1 / shops / {shop_id} / orders / {order_id} / cancel.json
860
+ cancel_order_url = f'{self.__api_url}/v1/shops/{shop_id}/orders/{order_id}/cancel.json'
861
+ order_information = self.__post(cancel_order_url)
862
+ return self.__parse(Order, order_information)
863
+
864
+ # ARTWORK UPLOADS
865
+ def get_artwork_uploads(self, max_pages: int = 1) -> List[Artwork]:
866
+ # GET / v1 / uploads.json
867
+ initial_url = f'{self.__api_url}/v1/uploads.json'
868
+ artwork_url = deepcopy(initial_url)
869
+ all_artworks = []
870
+ for _ in range(max_pages):
871
+ if artwork_url is None:
872
+ break
873
+ artwork_information = self.__get(artwork_url)
874
+ all_artworks.extend(self.__parse(Artwork, artwork_information['data']))
875
+ artwork_url = self.__get_next_page_url(initial_url, artwork_information)
876
+ return all_artworks
877
+
878
+ def get_artwork(self, image_id: str) -> Artwork:
879
+ # GET / v1 / uploads / {image_id}.json
880
+ artwork_url = f'{self.__api_url}/v1/uploads/{image_id}.json'
881
+ artwork_information = self.__get(artwork_url)
882
+ return self.__parse(Artwork, artwork_information)
883
+
884
+ def upload_artwork(self, filename: Optional[str] = None, url: Optional[str] = None) -> Artwork:
885
+ if filename and url:
886
+ raise PrintiPyException("Must provide a local filename or url for upload, not both.")
887
+
888
+ # POST / v1 / uploads / images.json
889
+ upload_artwork_url = f'{self.__api_url}/v1/uploads/images.json'
890
+ if url:
891
+ artwork_data = {
892
+ "file_name": url.split('/')[-1],
893
+ "url": url,
894
+ }
895
+ elif filename:
896
+ with open(filename, 'rb') as f:
897
+ b64data = base64.b64encode(f.read())
898
+ artwork_data = {
899
+ "file_name": os.path.basename(filename),
900
+ "contents": b64data.decode('utf-8'),
901
+ }
902
+ else:
903
+ raise PrintiPyException("Must provide at least a local filename or url for upload.")
904
+
905
+ artwork_information = self.__post(upload_artwork_url, data=artwork_data)
906
+ return self.__parse(Artwork, artwork_information)
907
+
908
+ def archive_artwork(self, image_id: str) -> True:
909
+ # post / v1 / uploads / {image_id} / archive.json
910
+ archive_artwork_url = f'{self.__api_url}/v1/uploads/{image_id}/archive.json'
911
+ self.__post(archive_artwork_url)
912
+ return True
913
+
914
+ # EVENTS AND WEBHOOKS
915
+ def get_shop_webhooks(self, shop_id: Union[str, int]) -> List[Webhook]:
916
+ # / v1 / shops / {shop_id} / webhooks.json
917
+ webhooks_url = f'{self.__api_url}/v1/shops/{shop_id}/webhooks.json'
918
+ webhooks_information = self.__get(webhooks_url)
919
+ return self.__parse(Webhook, webhooks_information)
920
+
921
+ def create_webhook(self, shop_id: Union[str, int], create_webhook: CreateWebhook) -> Webhook:
922
+ # POST /v1/shops/{shop_id}/webhooks.json
923
+ create_webhook_url = f'{self.__api_url}/v1/shops/{shop_id}/webhooks.json'
924
+ webhook_information = self.__post(create_webhook_url, data=create_webhook.to_dict())
925
+ return self.__parse(Webhook, webhook_information)
926
+
927
+ def update_webhook(self, shop_id: Union[str, int], webhook_id: str, update_webhook: UpdateWebhook) -> Webhook:
928
+ # PUT /v1/shops/{shop_id}/webhooks/{webhook_id}.json
929
+ create_webhook_url = f'{self.__api_url}/v1/shops/{shop_id}/webhooks/{webhook_id}.json'
930
+ webhook_information = self.__put(create_webhook_url, data=update_webhook.to_dict())
931
+ return self.__parse(Webhook, webhook_information)
932
+
933
+ def delete_webhook(self, shop_id: Union[str, int], webhook_id: str) -> True:
934
+ # DELETE /v1/shops/{shop_id}/webhooks/{webhook_id}.json
935
+ delete_webhook_url = f'{self.__api_url}/v1/shops/{shop_id}/webhooks/{webhook_id}.json'
936
+ self.__delete(delete_webhook_url)
937
+ return True
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Lawrence Weikum
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,260 @@
1
+ Metadata-Version: 2.1
2
+ Name: printipy
3
+ Version: 0.0.1
4
+ Summary: Printify API for Python
5
+ Home-page: https://github.com/lawrencemq/printipy
6
+ Author: Lawrence Weikum
7
+ Keywords: printify,print on demand,api
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.8
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Topic :: Internet
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Requires-Python: >=3.8
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: requests
20
+ Requires-Dist: dataclasses-json
21
+
22
+ # PrintiPy
23
+ The Printify API for Python
24
+
25
+ Tested with Python 3.8+
26
+
27
+
28
+ ## documentation - to do
29
+
30
+ ## create product
31
+
32
+ Example:
33
+ Even though the data below has more information than the API needs, the extraneous data will be filtered out automatically and the call will be sucessfully made.
34
+ ```python
35
+ new_product_data = {
36
+ "title": "Testy McTestFace",
37
+ "description": "suc test. wow!",
38
+ "tags": [
39
+ "Home & Living",
40
+ "Paper",
41
+ "Sustainable",
42
+ "Greeting Card",
43
+ "Postcard",
44
+ "Card",
45
+ "Post cards",
46
+ "Postcards",
47
+ "Made in USA"
48
+ ],
49
+ "blueprint_id": 1094,
50
+ "print_provider_id": 228,
51
+ "variants": [
52
+ {
53
+ "id": 81870,
54
+ "price": 999,
55
+ "is_enabled": True,
56
+ },
57
+ {
58
+ "id": 81871,
59
+ "price": 2499,
60
+ "is_enabled": True,
61
+ },
62
+ {
63
+ "id": 81872,
64
+ "price": 4399,
65
+ "is_enabled": True,
66
+ },
67
+ {
68
+ "id": 81873,
69
+ "price": 5599,
70
+ "is_enabled": True,
71
+ },
72
+ ],
73
+ "print_areas": [
74
+ {
75
+ "variant_ids": [
76
+ 81810,
77
+ 81811,
78
+ 81812,
79
+ 81813,
80
+ 81822,
81
+ 81823,
82
+ 81824,
83
+ 81825,
84
+ 81814,
85
+ 81815,
86
+ 81816,
87
+ 81817,
88
+ 81818,
89
+ 81819,
90
+ 81820,
91
+ 81821,
92
+ 81874,
93
+ 81875,
94
+ 81876,
95
+ 81877,
96
+ 81886,
97
+ 81887,
98
+ 81888,
99
+ 81889,
100
+ 81878,
101
+ 81879,
102
+ 81880,
103
+ 81881,
104
+ 81882,
105
+ 81883,
106
+ 81884,
107
+ 81885,
108
+ 81890,
109
+ 81891,
110
+ 81892,
111
+ 81893,
112
+ 81902,
113
+ 81903,
114
+ 81904,
115
+ 81905,
116
+ 81894,
117
+ 81895,
118
+ 81896,
119
+ 81897,
120
+ 81898,
121
+ 81899,
122
+ 81900,
123
+ 81901,
124
+ 81906,
125
+ 81907,
126
+ 81908,
127
+ 81909,
128
+ 81918,
129
+ 81919,
130
+ 81920,
131
+ 81921,
132
+ 81910,
133
+ 81911,
134
+ 81912,
135
+ 81913,
136
+ 81914,
137
+ 81915,
138
+ 81916,
139
+ 81917,
140
+ 81962,
141
+ 81963,
142
+ 81964,
143
+ 81965,
144
+ 81974,
145
+ 81975,
146
+ 81976,
147
+ 81977,
148
+ 81966,
149
+ 81967,
150
+ 81968,
151
+ 81969,
152
+ 81970,
153
+ 81971,
154
+ 81972,
155
+ 81973,
156
+ 81826,
157
+ 81827,
158
+ 81828,
159
+ 81829,
160
+ 81838,
161
+ 81839,
162
+ 81840,
163
+ 81841,
164
+ 81830,
165
+ 81831,
166
+ 81832,
167
+ 81833,
168
+ 81834,
169
+ 81835,
170
+ 81836,
171
+ 81837,
172
+ 81842,
173
+ 81843,
174
+ 81844,
175
+ 81845,
176
+ 81854,
177
+ 81855,
178
+ 81856,
179
+ 81857,
180
+ 81846,
181
+ 81847,
182
+ 81848,
183
+ 81849,
184
+ 81850,
185
+ 81851,
186
+ 81852,
187
+ 81853,
188
+ 81858,
189
+ 81859,
190
+ 81860,
191
+ 81861,
192
+ 81870,
193
+ 81871,
194
+ 81872,
195
+ 81873,
196
+ 81862,
197
+ 81863,
198
+ 81864,
199
+ 81865,
200
+ 81866,
201
+ 81867,
202
+ 81868,
203
+ 81869
204
+ ],
205
+ "placeholders": [
206
+ {
207
+ "position": "inside",
208
+ "images": [
209
+ {
210
+ "id": "63e7f5fbf08c3242e6859f58",
211
+ "name": "bday_0030_back.jpg",
212
+ "type": "image/jpeg",
213
+ "height": 3849,
214
+ "width": 2750,
215
+ "x": 0.7440094762474443,
216
+ "y": 0.4906514988405507,
217
+ "scale": 0.49589968099975856,
218
+ "angle": 0
219
+ },
220
+ {
221
+ "id": "63e7f5fbf08c3242e6859f58",
222
+ "name": "bday_0030_back.jpg",
223
+ "type": "image/jpeg",
224
+ "height": 3849,
225
+ "width": 2750,
226
+ "x": 0.24780487804878043,
227
+ "y": 0.4903646422579296,
228
+ "scale": 0.49560975609756086,
229
+ "angle": 0
230
+ }
231
+ ]
232
+ },
233
+ {
234
+ "position": "cover",
235
+ "images": [
236
+ {
237
+ "id": "63e7f5bb3ff59340bd57938f",
238
+ "name": "bday_0030_front.jpg",
239
+ "type": "image/jpeg",
240
+ "height": 3849,
241
+ "width": 2750,
242
+ "x": 0.7390939252654081,
243
+ "y": 0.49405654771100854,
244
+ "scale": 0.48716923114741456,
245
+ "angle": 0
246
+ }
247
+ ]
248
+ }
249
+ ],
250
+ }
251
+ ]
252
+ }
253
+ new_product = CreateProduct.from_dict(new_product_data)
254
+ print(api.create_product(7370017, new_product))
255
+ ```
256
+
257
+
258
+ ## Running Tests
259
+
260
+ `pipenv run pytest`
@@ -0,0 +1,7 @@
1
+ printipy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ printipy/api.py,sha256=gcZoT4qTon04ixo8TkffHVunhhLr681va79HYfZN1N4,28758
3
+ printipy-0.0.1.dist-info/LICENSE,sha256=rr9Vc-AnU5QntBb4w0Obi4L9xksdTcE-5IBohanXdrs,1071
4
+ printipy-0.0.1.dist-info/METADATA,sha256=VpA15aaGp_TigcPqj8XR0155VHS4tBBiXuDXfkaTN0M,7722
5
+ printipy-0.0.1.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
6
+ printipy-0.0.1.dist-info/top_level.txt,sha256=Uc3x-7XmEisnwP9XVSbbG0mt5gVXMkCEy0SqWb1Gf58,9
7
+ printipy-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.40.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ printipy