-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasset.py
More file actions
162 lines (149 loc) · 6.81 KB
/
asset.py
File metadata and controls
162 lines (149 loc) · 6.81 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
r"""Assets refer to all the media files (images, videos, PDFs, audio files,
and so on) uploaded in your Contentstack repository for future use.
These files can be attached and used in multiple entries.
"""
import logging
from urllib import parse
from contentstack.error_messages import ErrorMessages
class Asset:
r"""`Asset` refer to all the media files (images, videos, PDFs, audio files, and so on)."""
def __init__(self, http_instance, uid=None, logger=None):
self.http_instance = http_instance
self.asset_params = {}
self.__uid = uid
if self.__uid is None or self.__uid.strip() == 0:
raise KeyError(ErrorMessages.INVALID_UID)
self.uid = uid
self.base_url = f'{self.http_instance.endpoint}/assets/{self.__uid}'
if 'environment' in self.http_instance.headers:
self.asset_params['environment'] = self.http_instance.headers['environment']
self.logger = logger or logging.getLogger(__name__)
def environment(self, environment):
r"""Provide the name of the environment if you wish to retrieve the assets published
in a particular environment.
:param environment {str} - name of the environment
:return: `Asset`, so we can chain the call
-------------------------------
[Example]:
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> asset = asset.environment('production')
-------------------------------
"""
if environment is not None or environment is str:
self.http_instance.headers['environment'] = environment
return self
def remove_environment(self):
r"""Removes environment from the request params
:return: `Asset`, so we can chain the call
-------------------------------
[Example]:
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> asset = asset.remove_header()
>>> asset.fetch()
-------------------------------
"""
if 'environment' in self.http_instance.headers:
self.http_instance.headers.pop('environment')
return self
def params(self, key, value):
r"""params is used to pass additional parameters to the asset query
:param key: key of the query parameter
:param value: value of the query parameter
:return: `Asset`, so we can chain the call
-----------------------------
Example::
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> asset = asset.params('key', 'value')
-----------------------------
"""
if None in (key, value) or not isinstance(key, str):
raise KeyError(ErrorMessages.INVALID_PARAMS)
self.asset_params[key] = value
return self
def relative_urls(self):
"""Include the relative URLs of the assets in the response.
:return: `Asset`, so we can chain the call
----------------------------
Example::
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> asset = asset.relative_urls()
----------------------------
"""
self.asset_params['relative_urls'] = 'true'
return self
def include_dimension(self):
r"""Include the dimensions (height and width) of the image in the response.
Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, and PSD.
:return: `Asset`, so we can chain the call
----------------------------
Example::
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> asset = asset.include_dimension()
----------------------------
"""
self.asset_params['include_dimension'] = "true"
return self
def include_fallback(self):
r"""Retrieve the published content of the fallback locale if an
entry is not localized in specified locale
:return: `Asset`, so we can chain the call
----------------------------
Example::
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> result = asset.include_fallback().fetch()
----------------------------
"""
self.asset_params['include_fallback'] = "true"
return self
def asset_fields(self, *field_names):
r"""Include specific asset fields in the response.
Supported values: user_defined_fields, embedded_metadata, ai_generated_metadata, visual_markups.
Pass one or more field names. Can be called multiple times to add more fields.
:param field_names: One or more asset field names (user_defined_fields, embedded_metadata, ai_generated_metadata, visual_markups)
:return: `Asset`, so we can chain the call
----------------------------
Example::
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> result = asset.asset_fields('user_defined_fields', 'visual_markups').fetch()
----------------------------
"""
if field_names:
values = []
for name in field_names:
if isinstance(name, (list, tuple)):
values.extend(str(v) for v in name)
else:
values.append(str(name))
if values:
existing = self.asset_params.get('asset_fields[]', [])
if not isinstance(existing, list):
existing = [existing]
self.asset_params['asset_fields[]'] = existing + values
return self
def fetch(self):
r"""This call fetches the latest version of a specific asset of a particular stack.
:return: json response of asset
-----------------------------
[Example]:
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> asset = stack.asset(uid='asset_uid')
>>> result = asset.fetch()
------------------------------
"""
url = f'{self.base_url}?{parse.urlencode(self.asset_params)}'
return self.http_instance.get(url)