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/config.py ADDED
@@ -0,0 +1,162 @@
1
+ import enum
2
+ import logging
3
+
4
+ """
5
+ Config
6
+ contentstack
7
+ Created by Shailesh Mishra on 22/06/19.
8
+ Copyright 2019 Contentstack. All rights reserved.
9
+
10
+ """
11
+
12
+ logging.basicConfig(filename='report.log', format='%(asctime)s - %(message)s', level=logging.INFO)
13
+ logging.getLogger(__name__)
14
+
15
+
16
+ class ContentstackRegion(enum.Enum):
17
+ US = 'us'
18
+ EU = 'eu'
19
+
20
+
21
+ class Config(object):
22
+
23
+ def __init__(self):
24
+
25
+ """Config class arguments accepts protocol, region, host and version:
26
+
27
+ protocol: It support https protocol only
28
+ region: It accepts us and eu region.
29
+ host: host of the stack
30
+ version: API version, contentstack support v3 API version
31
+
32
+ """
33
+ self.default = dict(protocol="https", region=ContentstackRegion.US, host="cdn.contentstack.io", version="v3")
34
+
35
+ @property
36
+ def region(self):
37
+
38
+ """It sets the region in the API, We support us and eu region
39
+ default region will be us.
40
+
41
+ Returns:
42
+ ContentstackRegion -- It returns type of ContentstackRegion
43
+ """
44
+ return self.default['region']
45
+
46
+ @region.setter
47
+ def region(self, region=ContentstackRegion.US):
48
+
49
+ """
50
+ A Contentstack region refers to the location of the data centers where your organization's data resides.
51
+
52
+ Keyword Arguments:
53
+ region {ContentstackRegion} -- region refers to the location of the data centers where your organization's
54
+ data resides. (default: {ContentstackRegion.US})
55
+
56
+ For more details: https://www.contentstack.com/docs/guide/contentstack-regions
57
+
58
+ ==============================
59
+
60
+ [Example:]
61
+
62
+ >>> config = Config()
63
+ >>> config.region = ContentstackRegion.US
64
+
65
+ ==============================
66
+ """
67
+
68
+ if region is not None and isinstance(region, ContentstackRegion):
69
+ if region != ContentstackRegion.US:
70
+ if self.default["host"] == 'cdn.contentstack.io':
71
+ self.default["host"] = 'cdn.contentstack.com'
72
+ self.default['region'] = region
73
+
74
+ @property
75
+ def host(self):
76
+
77
+ """host property returns host of the API
78
+
79
+ Returns:
80
+ str -- host of the API
81
+ """
82
+
83
+ return self.default['host']
84
+
85
+ @host.setter
86
+ def host(self, host):
87
+
88
+ """
89
+ The base URL for Content Delivery API is cdn.contentstack.io.
90
+ host is the domain name or IP address of the host that serves the API. It may include the
91
+ port number if different from the scheme's default
92
+ port (443 for HTTPS).
93
+
94
+ Arguments:
95
+ host {str} -- host of the stack
96
+
97
+ Returns:
98
+ [Config] -- Config, So we can chain more functions
99
+
100
+ ==============================
101
+
102
+ [Example:]
103
+
104
+ >>> config = Config()
105
+ >>> config.host = 'cdn.contentstack.io'
106
+
107
+ ==============================
108
+ """
109
+
110
+ if host is not None and isinstance(host, str):
111
+ self.default['host'] = host
112
+
113
+ def version(self, version=None):
114
+
115
+ """
116
+ Only version v3 is supported on the CDN.
117
+
118
+ Keyword Arguments:
119
+ version {str} -- version of the API (default: {None})
120
+
121
+ Returns:
122
+ Config -- class instance to chain the functions
123
+
124
+ ==============================
125
+
126
+ [Example:]
127
+
128
+ >>> config = Config()
129
+ >>> config = config.version('v3')
130
+
131
+ ==============================
132
+
133
+ """
134
+
135
+ if version is not None and isinstance(version, str):
136
+ self.default['version'] = version
137
+
138
+ return self
139
+
140
+ @property
141
+ def endpoint(self):
142
+
143
+ """It returns endpoint url of the API
144
+ The base URL for Content Delivery API for the US region is cdn.contentstack.io. and
145
+ for the European region it is eu-cdn.contentstack.com
146
+ For more details: https://www.contentstack.com/docs/apis/content-delivery-api/#base-url
147
+
148
+ Returns:
149
+ str -- endpoint url of the API
150
+
151
+ """
152
+ return self.__get_url()
153
+
154
+ def __get_url(self):
155
+ host = self.default["host"]
156
+ region = self.default['region'].value
157
+ if region is not None:
158
+ if region is not 'us':
159
+ if self.default["host"] == 'cdn.contentstack.io':
160
+ self.default["host"] = 'cdn.contentstack.com'
161
+ host = '{}-{}'.format(region, self.default["host"])
162
+ return "{0}://{1}/{2}".format(self.default["protocol"], host, self.default["version"])
@@ -0,0 +1,233 @@
1
+ """
2
+
3
+ Created by Shailesh Mishra on 22/06/19.
4
+ Copyright 2019 Contentstack. All rights reserved.
5
+
6
+ contentstack.content_type
7
+ ~~~~~~~~~~~~~~~~~~
8
+
9
+ API Reference: https://www.contentstack.com/docs/guide/content-types
10
+
11
+ """
12
+
13
+
14
+ class ContentType:
15
+ """Content type defines the structure or schema of a page or a section of your web or mobile property. To create
16
+ content for your application, you are required to first create a content type, and then create entries using the
17
+ content type.
18
+
19
+ Read more about Content Types[https://www.contentstack.com/docs/guide/content-types].
20
+
21
+ """
22
+
23
+ def __init__(self, content_type_uid: str):
24
+ self.__config = None
25
+ self.__stack_instance = None
26
+ self.__http_request = None
27
+ self.__stack_headers = {}
28
+
29
+ if content_type_uid is None or not isinstance(content_type_uid, str):
30
+ raise KeyError('content_type_uid can not be None')
31
+ self.__content_type_uid = content_type_uid
32
+
33
+ def _instance(self, stack_instance):
34
+ # _instance is the protected member of the asset, So outsiders can not access this file.
35
+ from contentstack.stack import Stack
36
+ from contentstack.errors import StackException
37
+ if stack_instance is None:
38
+ raise StackException('Kindly initialise stack first')
39
+ self.__stack_instance: Stack = stack_instance
40
+ self.__config = self.__stack_instance.config
41
+ self.__stack_headers.update(self.__stack_instance.headers)
42
+ self.__http_request = self.__stack_instance.get_http_instance
43
+
44
+ @property
45
+ def header(self):
46
+
47
+ """header property is useful to get header of the the content_type
48
+
49
+ Returns:
50
+ dict -- It returns stack_headers
51
+
52
+ ==============================
53
+
54
+ [Example:]
55
+
56
+ >>> from contentstack import Stack
57
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
58
+ >>> content_type = stack.content_type('content_type_uid')
59
+ >>> headers = content_type.header
60
+
61
+ ==============================
62
+
63
+ """
64
+
65
+ return self.__stack_headers
66
+
67
+ def add_header(self, key, value):
68
+
69
+ """
70
+ Adds header in content_type as key value pair
71
+
72
+ Arguments:
73
+ key {str} -- key of the header
74
+ value {object} -- value of the respected key in header
75
+
76
+ Raises:
77
+ KeyError: key and value should not be None
78
+ KeyError: key and value both should be str type
79
+
80
+ Returns:
81
+ ContentType -- ContentType, So we can chain the call
82
+
83
+ ==============================
84
+
85
+ [Example:]
86
+ >>> from contentstack import Stack
87
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
88
+ >>> content_type = stack.content_type('content_type_uid')
89
+ >>> content_type = content_type.add_header('key', 'value')
90
+
91
+ ==============================
92
+ """
93
+ if None in (key, value):
94
+ raise KeyError('Kindly provide a valid key and value')
95
+ elif isinstance(key, str) and isinstance(value, str):
96
+ self.__stack_headers[key] = value
97
+ else:
98
+ raise KeyError('Key and value should be str type')
99
+
100
+ return self
101
+
102
+ def remove_header(self, key):
103
+
104
+ """It deletes the header against specified key
105
+
106
+ Arguments:
107
+ key {str} -- existing key that has to delete from header
108
+
109
+ Raises:
110
+ KeyError: If Key is None, Empty or not str type
111
+
112
+ Returns:
113
+ ContentType -- ContentType class object so we can chain more functions
114
+
115
+ ==============================
116
+
117
+ [Example:]
118
+ >>> from contentstack import Stack
119
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
120
+ >>> content_type = stack.content_type('content_type_uid')
121
+ >>> content_type = content_type.remove_header('key')
122
+
123
+ ==============================
124
+
125
+ """
126
+
127
+ if key is None:
128
+ raise KeyError('key should not be None')
129
+ elif isinstance(key, str):
130
+ if key in self.__stack_headers:
131
+ self.__stack_headers.pop(key)
132
+
133
+ return self
134
+
135
+ def entry(self, uid):
136
+
137
+ """An entry is the actual piece of content created using one of the defined content types.
138
+
139
+ Read more about Entries.
140
+ [https://www.contentstack.com/docs/apis/content-delivery-api/#entries]
141
+
142
+ Arguments:
143
+ uid {str} -- uid of the entry
144
+
145
+ Raises:
146
+ EnvironmentError: http instance is not found (invalid http request)
147
+ KeyError: entru uid should be str type
148
+
149
+ Returns:
150
+ Entry -- Returns the Entry class object so we can chain the entry functions
151
+
152
+ ==============================
153
+
154
+ [Example:]
155
+
156
+ >>> from contentstack import Stack
157
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
158
+ >>> content_type = stack.content_type('content_type_uid')
159
+ >>> entry: Entry = content_type.entry('uid')
160
+
161
+ ==============================
162
+
163
+ """
164
+
165
+ from contentstack import Entry
166
+ if self.__http_request is None:
167
+ raise EnvironmentError("Invalid http request")
168
+ entry: Entry = Entry(content_type_id=self.__content_type_uid)
169
+ entry._instance(self.__stack_instance)
170
+ if uid is not None:
171
+ if isinstance(uid, str):
172
+ entry.uid=uid
173
+ else:
174
+ raise KeyError('entry uid should be str type')
175
+ return entry
176
+
177
+ def query(self):
178
+
179
+ """
180
+ It returns query class object so we can query on entry of specified ContentType
181
+
182
+ Returns:
183
+ Query -- query object instance, so we can chain the query functions to it.
184
+
185
+ ==============================
186
+
187
+ [Example:]
188
+
189
+ >>> from contentstack import Stack
190
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
191
+ >>> content_type = stack.content_type('content_type_uid')
192
+ >>> query: Query = content_type.query()
193
+
194
+ ==============================
195
+
196
+ """
197
+
198
+ from contentstack.query import Query
199
+ query = Query(self.__content_type_uid)
200
+ query._instance(self.__stack_instance)
201
+ return query
202
+
203
+ def fetch(self, params=None):
204
+
205
+ """This method is useful to fetch ContentType of the of the stack.
206
+ :return: response returns Json
207
+ :rtype: ContentType
208
+
209
+ Returns:
210
+ dict -- Returns dictionary response
211
+
212
+ ==============================
213
+
214
+ Example:
215
+
216
+ >>> from contentstack import Stack
217
+ >>> stack = Stack(api_key='stack_api_key', access_token='stack_access_token', environment='env')
218
+ >>> content_type = stack.content_type('content_type_uid')
219
+ >>> content_type.add_header('key', 'someheader')
220
+ >>> some_dict = {'abc':'something'}
221
+ >>> response = content_type.fetch(some_dict)
222
+
223
+ ==============================
224
+
225
+ """
226
+ params_dict = {}
227
+ endpoint = self.__config.endpoint
228
+ url = '{}/content_types'.format(endpoint)
229
+ content_type_url = '{0}/{1}'.format(url, self.__content_type_uid)
230
+ if params is not None and isinstance(dict, params):
231
+ params_dict.update(params)
232
+ result = self.__http_request.get_result(content_type_url, params_dict, self.__stack_headers)
233
+ return result