Contentstack 0.1.0__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.
contentstack/asset.py ADDED
@@ -0,0 +1,784 @@
1
+ """
2
+
3
+ Created by Shailesh Mishra on 22/06/19.
4
+ Copyright 2019 Contentstack. All rights reserved.
5
+
6
+ contentstack.asset
7
+ ~~~~~~~~~~~~~~~~~~
8
+
9
+ API Reference: https://www.contentstack.com/docs/apis/content-delivery-api/#assets
10
+
11
+ """
12
+
13
+
14
+ class Asset:
15
+
16
+ """Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded
17
+ in your Contentstack repository for future use.
18
+ These files can be attached and used in multiple entries.
19
+ contentstack.asset
20
+ ==============================
21
+
22
+ This module implements the Asset class.
23
+ API Reference: https://www.contentstack.com/docs/apis/content-delivery-api/#assets
24
+
25
+ [Example]:
26
+
27
+ >>> from contentstack import Stack
28
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
29
+ >>> asset = stack.asset()
30
+
31
+ ==============================
32
+
33
+ """
34
+
35
+ def __init__(self, uid=None):
36
+ if uid is not None and isinstance(uid, str):
37
+ self.__uid = uid
38
+ self.__config = None
39
+ self.__stack_instance = None
40
+ self.__response = None
41
+ self.__http_request = None
42
+ self.__query_params = {}
43
+ self.__stack_headers = {}
44
+ self.__count = 0
45
+ self.__file_name = None
46
+ self.__file_size = None
47
+ self.__file_type = None
48
+ self.__file_url = None
49
+ self.__created_at = None
50
+ self.__created_by = None
51
+ self.__updated_at = None
52
+ self.__updated_by = None
53
+ self.__version = None
54
+ self.__dimension = None
55
+ self.__tags = None
56
+
57
+ def _instance(self, stack_instance):
58
+ # _instance is the protected member of the asset, So outsiders can not access this file.
59
+ from contentstack.stack import Stack
60
+ from contentstack.errors import StackException
61
+ if stack_instance is None:
62
+ raise StackException('Kindly initialise stack first')
63
+ if isinstance(stack_instance, Stack):
64
+ self.__stack_instance = stack_instance
65
+ self.__config = self.__stack_instance.config
66
+ self.__http_request = self.__stack_instance.get_http_instance
67
+ else:
68
+ raise ValueError('Invalid Stack type, Please provide Stack Instance')
69
+
70
+ return self
71
+
72
+ def _configure(self, response):
73
+ if response is not None and isinstance(response, dict) and len(response) > 0:
74
+
75
+ self.__response = response
76
+ if 'filename' in self.__response:
77
+ self.__file_name = self.__response['filename']
78
+ if 'file_size' in self.__response:
79
+ self.__file_size = self.__response['file_size']
80
+ if 'content_type' in self.__response:
81
+ self.__file_type = self.__response['content_type']
82
+ if 'url' in self.__response:
83
+ self.__file_url = self.__response['url']
84
+ if 'uid' in self.__response:
85
+ self.__uid = self.__response['uid']
86
+ if 'tags' in self.__response:
87
+ self.__tags = self.__response['tags']
88
+ if 'created_at' in self.__response:
89
+ self.__created_at = self.__response['created_at']
90
+ if 'created_by' in self.__response:
91
+ self.__created_by = self.__response['created_by']
92
+ if 'updated_at' in self.__response:
93
+ self.__updated_at = self.__response['updated_at']
94
+ if 'updated_by' in self.__response:
95
+ self.__updated_by = self.__response['updated_by']
96
+ if '_version' in self.__response:
97
+ self.__version = self.__response['_version']
98
+ if 'dimension' in self.__response:
99
+ self.__dimension = self.__response['dimension']
100
+ return self
101
+
102
+ @property
103
+ def uid(self):
104
+
105
+ """uid property helps to set uid of the asset
106
+
107
+ Returns:
108
+ str -- uid of the asset
109
+
110
+ ==============================
111
+
112
+ [Example]:
113
+
114
+ >>> from contentstack import Stack
115
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
116
+ >>> asset = stack.asset('uid')
117
+ >>> uid = asset.uid
118
+
119
+ ==============================
120
+ """
121
+ return self.__uid
122
+
123
+ @property
124
+ def count(self):
125
+
126
+ """
127
+ count property helps to get the Size of asset objects.
128
+
129
+ Returns:
130
+ int -- count of asset object
131
+
132
+ ==============================
133
+
134
+ [Example]:
135
+
136
+ >>> from contentstack import Stack
137
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
138
+ >>> asset = stack.asset()
139
+ >>> count = asset.count
140
+
141
+ ==============================
142
+
143
+ """
144
+
145
+ return self.__count
146
+
147
+ def _count(self, counter):
148
+ if counter is not None:
149
+ self.__count = counter
150
+
151
+ @property
152
+ def filetype(self):
153
+
154
+ """This function returns type of the asset file
155
+
156
+ Returns:
157
+ str -- type of file
158
+
159
+ ==============================
160
+
161
+ [Example]:
162
+
163
+ >>> from contentstack import Stack
164
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
165
+ >>> asset = stack.asset('uid')
166
+ >>> file = asset.filetype
167
+
168
+ ==============================
169
+
170
+ """
171
+ return self.__file_type
172
+
173
+ @property
174
+ def filesize(self):
175
+
176
+ """Returns size of asset file
177
+
178
+ Returns:
179
+ int -- size of asset file
180
+
181
+ ==============================
182
+
183
+ [Example]:
184
+
185
+ >>> from contentstack import Stack
186
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
187
+ >>> asset = stack.asset('uid')
188
+ >>> size = asset.filesize
189
+
190
+ ==============================
191
+
192
+ """
193
+
194
+ return self.__file_size
195
+
196
+ @property
197
+ def filename(self):
198
+
199
+ """This property returns the filename asset file.
200
+
201
+ Returns:
202
+ str -- filename of asset
203
+
204
+ ==============================
205
+
206
+ [Example]:
207
+
208
+ >>> from contentstack import Stack
209
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
210
+ >>> asset = stack.asset('uid')
211
+ >>> filename = asset.filename
212
+
213
+ ==============================
214
+
215
+ """
216
+
217
+ return self.__file_name
218
+
219
+ @property
220
+ def url(self):
221
+
222
+ """This property returns the url of the Asset
223
+
224
+ Returns:
225
+ str -- asset url
226
+
227
+ ==============================
228
+
229
+ [Example]:
230
+
231
+ >>> from contentstack import Stack
232
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
233
+ >>> asset = stack.asset('uid')
234
+ >>> file_url = asset.url
235
+
236
+ ==============================
237
+
238
+ """
239
+
240
+ return self.__file_url
241
+
242
+ @property
243
+ def json(self):
244
+
245
+ """
246
+ This property returns response in json
247
+
248
+ Returns:
249
+ str -- Json response of Asset
250
+
251
+ ==============================
252
+
253
+ [Example]:
254
+
255
+
256
+ >>> from contentstack import Stack
257
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
258
+ >>> asset = stack.asset('uid')
259
+ >>> response = asset.json
260
+
261
+ ==============================
262
+ """
263
+
264
+ return self.__response
265
+
266
+ @property
267
+ def created_at(self):
268
+
269
+ """
270
+ This property returns the time when Asset is created.
271
+
272
+ Returns:
273
+ str -- Time when Asset is created
274
+
275
+ ==============================
276
+
277
+ [Example]:
278
+
279
+
280
+ >>> from contentstack import Stack
281
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
282
+ >>> asset = stack.asset('uid')
283
+ >>> response = asset.created_at
284
+
285
+ ==============================
286
+ """
287
+
288
+ return self.__created_at
289
+
290
+ @property
291
+ def created_by(self):
292
+
293
+ """This property created_by returns the owner of the Asset
294
+
295
+ Returns:
296
+ str -- Owner of the Asset
297
+
298
+ ===============================
299
+
300
+ [Example]:
301
+
302
+ >>> from contentstack import Stack
303
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
304
+ >>> asset = stack.asset('uid')
305
+ >>> response = asset.created_by
306
+
307
+ ================================
308
+
309
+ """
310
+
311
+ return self.__created_by
312
+
313
+ @property
314
+ def updated_at(self):
315
+
316
+ """This property updated_at returns the time the Asset was updated.
317
+
318
+ Returns:
319
+ str -- Time the Asset was updated
320
+
321
+ ==============================
322
+
323
+ [Example]:
324
+
325
+ >>> from contentstack import Stack
326
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
327
+ >>> asset = stack.asset('uid')
328
+ >>> response = asset.updated_at
329
+
330
+ ==============================
331
+
332
+ """
333
+
334
+ return self.__updated_at
335
+
336
+ @property
337
+ def updated_by(self):
338
+
339
+ """
340
+ This function returns the time of Asset was updated
341
+
342
+ Returns:
343
+ str -- Asset was updated by whome
344
+
345
+ ==============================
346
+
347
+ [Example]:
348
+
349
+ >>> from contentstack import Stack
350
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
351
+ >>> asset = stack.asset('uid')
352
+ >>> response = asset.updated_by
353
+
354
+ ==============================
355
+
356
+ """
357
+
358
+ return self.__updated_by
359
+
360
+ @property
361
+ def tags(self):
362
+
363
+ """This property returns the list of tag str
364
+
365
+ Returns:
366
+ list -- list of tags
367
+
368
+ ==============================
369
+
370
+ [Example]:
371
+
372
+ >>> from contentstack import Stack
373
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
374
+ >>> asset = stack.asset('uid')
375
+ >>> tags = asset.tags
376
+
377
+ ==============================
378
+
379
+ """
380
+
381
+ return self.__tags
382
+
383
+ @property
384
+ def version(self):
385
+
386
+ """This property returns the version of asset
387
+
388
+ Returns:
389
+ int -- version of the asset
390
+
391
+ ==============================
392
+
393
+ [Example]:
394
+
395
+ >>> from contentstack import Stack
396
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
397
+ >>> asset = stack.asset('uid')
398
+ >>> tags = asset.get_version
399
+
400
+ ==============================
401
+
402
+ """
403
+
404
+ return self.__version
405
+
406
+ @property
407
+ def dimension(self):
408
+
409
+ """This property returns the dimension of the Asset
410
+
411
+ Returns:
412
+ dict -- Dimension of the Asset
413
+
414
+ ==============================
415
+
416
+ [Example]:
417
+
418
+ >>> from contentstack import Stack
419
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
420
+ >>> asset = stack.asset('uid')
421
+ >>> tags = asset.dimension
422
+
423
+ ==============================
424
+
425
+ """
426
+
427
+ return self.__dimension
428
+
429
+ def add_header(self, key, value):
430
+
431
+ """
432
+ add_header is the funtion helps to pass additional header to the asset class
433
+
434
+ Arguments:
435
+ key {str} -- key of the header
436
+ value {str} -- value of the header
437
+
438
+ Raises:
439
+ ValueError: Kindly provide valid KEY and Value
440
+ ValueError: key and value should be str type
441
+
442
+ Returns:
443
+ Asset -- Asset, So we can chain the call
444
+
445
+ ==============================
446
+
447
+ [Example]:
448
+
449
+ >>> from contentstack import Stack
450
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
451
+ >>> asset = stack.asset('uid')
452
+ >>> asset = asset.add_header('key', 'value')
453
+
454
+ ==============================
455
+
456
+ """
457
+
458
+ if None in (key, value):
459
+ raise ValueError('Kindly provide valid KEY and Value')
460
+ elif isinstance(key, str) and isinstance(value, str):
461
+ self.__stack_headers[key] = value
462
+ else:
463
+ raise ValueError('KEY and Value should be str type')
464
+
465
+ return self
466
+
467
+ def environment(self, environment):
468
+
469
+ """
470
+ Provide the name of the environment if you wish to retrieve the assets published
471
+ in a particular environment.
472
+
473
+ Arguments:
474
+ environment {str} -- the name of the environment
475
+
476
+ Returns:
477
+ Asset -- Asset, So we can chain the call
478
+
479
+ ==============================
480
+
481
+ [Example]:
482
+
483
+ >>> from contentstack import Stack
484
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
485
+ >>> asset = stack.asset('uid')
486
+ >>> asset = asset.environment('production')
487
+
488
+ ==============================
489
+ """
490
+
491
+ if environment is None:
492
+ raise ValueError('Kindly provide valid environment')
493
+ elif isinstance(environment, str):
494
+ self.__query_params['environment'] = environment
495
+ else:
496
+ raise ValueError('Environment should be str type')
497
+
498
+ return self
499
+
500
+ def add_params(self, key, value):
501
+
502
+ """
503
+ add_param is helpful to pass addtional parameters to the asset class
504
+
505
+ Arguments:
506
+ key {str} -- key of the query parameter
507
+ value {str} -- value of the query parameter
508
+
509
+ Raises:
510
+ KeyError: if key is None or type of key is not str
511
+ ValueError: if value is None
512
+
513
+ Returns:
514
+ Asset -- [Returns Asset object So we can chain the call]
515
+
516
+ ==============================
517
+
518
+ [Example]:
519
+
520
+ >>> from contentstack import Stack
521
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
522
+ >>> asset = stack.asset('uid')
523
+ >>> asset = asset.add_params('key', 'value')
524
+
525
+ ==============================
526
+ """
527
+
528
+ if None in (key, value):
529
+ raise KeyError('Kindly provide valid params')
530
+ elif isinstance(key, str):
531
+ self.__query_params[key] = value
532
+ else:
533
+ raise ValueError('Kindly provide str type KEY')
534
+
535
+ return self
536
+
537
+ def relative_urls(self):
538
+
539
+ """
540
+ Include the relative URLs of the assets in the response.
541
+
542
+ Returns:
543
+ Asset -- Asset, so we can chain the call
544
+
545
+ ==============================
546
+
547
+ [Example]:
548
+
549
+ >>> from contentstack import Stack
550
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
551
+ >>> asset = stack.asset('uid')
552
+ >>> asset = asset.relative_urls()
553
+
554
+ ==============================
555
+
556
+ """
557
+
558
+ self.__query_params['relative_urls'] = 'true'
559
+
560
+ return self
561
+
562
+ def include_dimension(self):
563
+
564
+ """
565
+ Include the dimensions (height and width) of the image in the response.
566
+ Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, and PSD.
567
+
568
+ Returns:
569
+ Asset -- Asset, so we can chain the call
570
+
571
+ ==============================
572
+
573
+ [Example]:
574
+
575
+ >>> from contentstack import Stack
576
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
577
+ >>> asset = stack.asset('uid')
578
+ >>> asset = asset.include_dimension()
579
+
580
+ ==============================
581
+
582
+ """
583
+
584
+ self.__query_params['include_dimension'] = "true"
585
+
586
+ return self
587
+
588
+ def remove_header(self, key):
589
+
590
+ """
591
+ remove_header function helps to delete the existing key from the header.
592
+
593
+ Arguments:
594
+ key {str} -- existing key of the header parameter
595
+
596
+ Raises:
597
+ ValueError: If key is None
598
+
599
+ Returns:
600
+ Asset -- Asset, So we can chain the call
601
+
602
+ ==============================
603
+
604
+ [Example]:
605
+
606
+ >>> from contentstack import Stack
607
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
608
+ >>> asset = stack.asset('uid')
609
+ >>> asset = asset.remove_header('some_key')
610
+
611
+ ==============================
612
+
613
+ """
614
+
615
+ if key is None:
616
+ raise ValueError('Kindly provide valid KEY')
617
+ elif isinstance(key, str) and key in self.__stack_headers:
618
+ self.__stack_headers.pop(key, None)
619
+
620
+ return self
621
+
622
+ def set_uid(self, asset_uid):
623
+
624
+ """
625
+ Enter the unique ID of the asset of which you wish to retrieve the details.
626
+
627
+ Arguments:
628
+ asset_uid {str} -- asset_uid is unique ID of the asset of which you wish to retrieve the details
629
+
630
+ Raises:
631
+ KeyError: If asset_uid is None
632
+ KeyError: If type of asset_uid is str
633
+
634
+ Returns:
635
+ Asset -- Asset, so we can chain the call
636
+
637
+ ==============================
638
+
639
+ [Example]:
640
+
641
+ >>> from contentstack import Stack
642
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
643
+ >>> asset = stack.asset('uid')
644
+ >>> asset = asset.set_uid('uid')
645
+
646
+ ==============================
647
+
648
+ """
649
+
650
+ if asset_uid is None:
651
+ raise KeyError('Invalid asset_uid')
652
+ elif isinstance(asset_uid, str):
653
+ self.__uid = asset_uid
654
+ else:
655
+ raise KeyError('Kindly provide str type of asset_uid')
656
+
657
+ return self
658
+
659
+ def include_count(self):
660
+
661
+ """
662
+ include_count is used to return total asset count
663
+
664
+ Returns:
665
+ Asset -- Asset, so we can chain the call
666
+
667
+ ==============================
668
+
669
+ [Example]:
670
+
671
+ >>> from contentstack import Stack
672
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
673
+ >>> asset = stack.asset('uid')
674
+ >>> asset = asset.set_uid('uid')
675
+
676
+ ==============================
677
+
678
+ """
679
+ self.__query_params['include_count'] = 'true'
680
+
681
+ return self
682
+
683
+ def sort(self, key, order_by):
684
+
685
+ """
686
+ Request asset by sort by key, It caould be sorted to ASCENDING and DESCENDING order
687
+ param key: provides the key on which OrderType.ASC or OrderType.DESC need to apply.
688
+ :param key: key that to be constrained
689
+ :param order_by: object option either OrderType.ASC or OrderType.DESC
690
+ :return self , instance of AssetLibrary
691
+
692
+ Arguments:
693
+ key {str} -- field uid
694
+ order_by {OrderType} -- type of oerder ASC or DES
695
+
696
+ Raises:
697
+ ValueError: If key and OrderType is None
698
+
699
+ Returns:
700
+ Asset -- Asset instance, So call will be chainable.
701
+
702
+ ==============================
703
+
704
+ [Example]:
705
+
706
+ >>> from contentstack import Stack
707
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
708
+ >>> asset = stack.asset('uid')
709
+ >>> asset = asset.sort('some_key', OrderType.ASC)
710
+
711
+ ==============================
712
+ """
713
+ if None in (key, order_by):
714
+ raise ValueError('Kindly provide valid arguments')
715
+ elif isinstance(key, str) and isinstance(order_by, OrderType):
716
+ if order_by is not None:
717
+ if order_by == 0:
718
+ self.__query_params['asc'] = key
719
+ else:
720
+ self.__query_params['desc'] = key
721
+
722
+ return self
723
+
724
+ def fetch_all(self):
725
+
726
+ """Assets refer to all the media files (images, videos, PDFs, audio files, and so on)
727
+ uploaded in your Contentstack repository for future use. These files can be
728
+ attached and used in multiple entries.
729
+ Learn more about Assets
730
+ [https://www.contentstack.com/docs/guide/content-management#working-with-assets].
731
+
732
+ Returns:
733
+ list[Assets] -- This call fetches the list of all the assets of a particular stack.
734
+ It also returns the content of each asset in the form of list[Assets]
735
+
736
+ ==============================
737
+
738
+ [Example]:
739
+
740
+ >>> from contentstack import Stack
741
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
742
+ >>> asset = stack.asset()
743
+ >>> result = asset.fetch_all()
744
+
745
+ ==============================
746
+
747
+ """
748
+
749
+ endpoint = self.__config.endpoint
750
+ url = '{}/assets'.format(endpoint)
751
+ return self.__http_request.get_result(url, self.__query_params, self.__stack_headers)
752
+
753
+ def fetch(self):
754
+
755
+ """
756
+ This call fetches the latest version of a specific asset of a particular stack.
757
+
758
+ Returns:
759
+ Asset -- It returns Asset on the basis of asset_uid
760
+
761
+ ==============================
762
+
763
+ [Example]:
764
+
765
+ >>> from contentstack import Stack
766
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
767
+ >>> asset = stack.asset('uid')
768
+ >>> result = asset.fetch()
769
+
770
+ ==============================
771
+
772
+ """
773
+ if self.__uid is not None and len(self.__uid) > 0:
774
+ endpoint = self.__config.endpoint
775
+ url = '{}/assets/{}'.format(endpoint, self.__uid)
776
+ else:
777
+ raise Exception("Kindly Provide Asset UID")
778
+ return self.__http_request.get_result(url, self.__query_params, self.__stack_headers)
779
+
780
+
781
+ class OrderType(object):
782
+ """OrderType is used to choose one of the ascending or descending"""
783
+
784
+ ASC, DESC = range(0, 2)