-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglobalfields.py
More file actions
73 lines (65 loc) · 2.8 KB
/
globalfields.py
File metadata and controls
73 lines (65 loc) · 2.8 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
"""
Global field defines the structure or schema of a page or a section of your web
or mobile property. To create content for your application, you are required
to first create a Global field, and then create entries using the
Global field.
"""
import logging
from urllib import parse
class GlobalField:
"""
Global field defines the structure or schema of a page or a
section of your web or mobile property. To create
content for your application, you are required to
first create a Global field, and then create entries using the
Global field.
"""
def __init__(self, http_instance, global_field_uid, logger=None):
self.http_instance = http_instance
self.__global_field_uid = global_field_uid
self.local_param = {}
self.logger = logger or logging.getLogger(__name__)
def fetch(self):
"""
This method is useful to fetch GlobalField of the of the stack.
:return:dict -- GlobalField response
------------------------------
Example:
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> global_field = stack.global_field('global_field_uid')
>>> some_dict = {'abc':'something'}
>>> response = global_field.fetch(some_dict)
------------------------------
"""
if self.__global_field_uid is None:
raise KeyError(
'global_field_uid can not be None to fetch GlobalField')
self.local_param['environment'] = self.http_instance.headers['environment']
uri = f'{self.http_instance.endpoint}/global_fields/{self.__global_field_uid}'
encoded_params = parse.urlencode(self.local_param)
url = f'{uri}?{encoded_params}'
result = self.http_instance.get(url)
return result
def find(self, params=None):
"""
This method is useful to fetch GlobalField of the of the stack.
:param params: dictionary of params
:return:dict -- GlobalField response
------------------------------
Example:
>>> import contentstack
>>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
>>> global_field = stack.global_field()
>>> some_dict = {'abc':'something'}
>>> response = global_field.find(param=some_dict)
------------------------------
"""
self.local_param['environment'] = self.http_instance.headers['environment']
if params is not None:
self.local_param.update(params)
encoded_params = parse.urlencode(self.local_param)
endpoint = self.http_instance.endpoint
url = f'{endpoint}/global_fields?{encoded_params}'
result = self.http_instance.get(url)
return result