1
+ """This class takes a base URL as an argument when it's initialized,
2
+ which is the endpoint for the RESTFUL API that we'll be interacting with.
3
+ The create(), read(), update(), and delete() methods each correspond to
4
+ the CRUD operations that can be performed on the API """
5
+
6
+ import json
7
+ from ..common import Parameter
8
+ from urllib .parse import quote
9
+ from .._errors import ArgumentException
10
+ from requests_toolbelt .multipart .encoder import MultipartEncoder
11
+
12
+ class Extension (Parameter ):
13
+ """
14
+ This class takes a base URL as an argument when it's initialized,
15
+ which is the endpoint for the RESTFUL API that
16
+ we'll be interacting with. The create(), read(), update(), and delete()
17
+ methods each correspond to the CRUD
18
+ operations that can be performed on the API """
19
+
20
+ def __init__ (self , client , extension_uid : str ):
21
+ self .client = client
22
+ self .extension_uid = extension_uid
23
+ super ().__init__ (self .client )
24
+
25
+ self .path = "extensions"
26
+
27
+ def find (self ):
28
+ """
29
+ The "Get all custom fields" request is used to get the information of all custom fields created in a stack.
30
+ :return: Json, with extension details.
31
+ -------------------------------
32
+ [Example:]
33
+
34
+ >>> from contentstack_management import contentstack
35
+ >>> client = contentstack.client(authtoken='your_authtoken')
36
+ >>> result = client.stack("api_key").extension().find().json()
37
+ -------------------------------
38
+ """
39
+ return self .client .get (self .path , headers = self .client .headers )
40
+
41
+
42
+
43
+ def fetch (self ):
44
+ """
45
+ The "Fetch" request returns information about a specific extension.
46
+ :return: Json, with extension details.
47
+ -------------------------------
48
+ [Example:]
49
+ >>> from contentstack_management import contentstack
50
+ >>> client = contentstack.client(authtoken='your_authtoken')
51
+ >>> result = client.stack('api_key').extension('extension_uid').fetch().json()
52
+ -------------------------------
53
+ """
54
+ self .validate_uid ()
55
+ url = f"{ self .path } /{ self .extension_uid } "
56
+ return self .client .get (url , headers = self .client .headers )
57
+
58
+
59
+ def upload (self , data : dict ):
60
+ """
61
+ The Upload is used to upload a new custom widget, custom field, dashboard Widget to a stack.
62
+
63
+ :param data: The `data` parameter is the payload that you want to send in the request body. It
64
+ should be a dictionary or a JSON serializable object that you want to send as the request body
65
+ :return: Json, with extension details.
66
+ -------------------------------
67
+ [Example:]
68
+ >>> extension = {
69
+ >>> "file_name": "demo.html",
70
+ >>> "file_path": "/Users/sunil.lakshman/Downloads/demo.html",
71
+ >>> "data_type": 'text',
72
+ >>> "title": 'Old Extension',
73
+ >>> "multiple": False,
74
+ >>> "tags": {},
75
+ >>> "type": 'dashboard'
76
+ >>> }
77
+ >>> from contentstack_management import contentstack
78
+ >>> client = contentstack.client(authtoken='your_authtoken')
79
+ >>> result = client.stack('api_key').extension().upload(extension).json()
80
+ -------------------------------
81
+ """
82
+
83
+ fields = {
84
+ 'extension[upload]' : (f"{ data ['file_name' ]} " , open (f"{ data ['file_name' ]} " , 'rb' ), 'text/html' ),
85
+ 'extension[title]' : f"{ data ['title' ]} " ,
86
+ 'extension[data_type]' : f"{ data ['data_type' ]} " ,
87
+ 'extension[type]' : f"{ data ['type' ]} " ,
88
+ 'extension[tags]' : f"{ data ['tags' ]} " ,
89
+ 'extension[multiple]' : f"{ data ['multiple' ]} "
90
+ }
91
+ content_type , body = self .encode_multipart_formdata (fields )
92
+ self .client .headers ['Content-Type' ] = content_type
93
+ return self .client .post (self .path , headers = self .client .headers , data = body )
94
+
95
+ def create (self , data : dict ):
96
+ """
97
+ The Create a extension call creates a new extension in a particular stack of your Contentstack account.
98
+
99
+ :param data: The `data` parameter is the data that you want to update. It should be a dictionary
100
+ or an object that can be serialized to JSON
101
+ :return: Json, with updated extension details.
102
+ -------------------------------
103
+ [Example:]
104
+ >>> extension = {
105
+ >>> tags: [
106
+ >>> 'tag1',
107
+ >>> 'tag2'
108
+ >>> ],
109
+ >>> data_type: 'text',
110
+ >>> title: 'Old Extension',
111
+ >>> src: "Enter either the source code (use 'srcdoc') or the external hosting link of the extension depending on the hosting method you selected.",
112
+ >>> multiple: false,
113
+ >>> config: {},
114
+ >>> type: 'field'
115
+ >>> }
116
+ >>> from contentstack_management import contentstack
117
+ >>> client = contentstack.client(authtoken='your_authtoken')
118
+ >>> result = client.stack('api_key').extension("extension_uid").update(extension).json()
119
+ -------------------------------
120
+ """
121
+ data = json .dumps (data )
122
+ return self .client .post (self .path , headers = self .client .headers , data = data )
123
+
124
+ def update (self , data : dict ):
125
+ """
126
+ The "Update Extensions call" will update the details of a custom field.
127
+
128
+ :param data: The `data` parameter is the data that you want to update. It should be a dictionary
129
+ or an object that can be serialized to JSON
130
+ :return: Json, with updated extension details.
131
+ -------------------------------
132
+ [Example:]
133
+ >>> data = {
134
+ >>> "extension": {
135
+ >>> "tags": [
136
+ >>> "tag1",
137
+ >>> "tag2"
138
+ >>> ],
139
+ >>> "data_type": "text",
140
+ >>> "title": "Old Extension",
141
+ >>> "src": "Enter either the source code (use 'srcdoc') or the external hosting link of the extension depending on the hosting method you selected.",
142
+ >>> "multiple": false,
143
+ >>> "config": "{}",
144
+ >>> "type": "field"
145
+ >>> }
146
+ >>> }
147
+ >>> from contentstack_management import contentstack
148
+ >>> client = contentstack.client(authtoken='your_authtoken')
149
+ >>> result = client.stack('api_key').extension("extension_uid").update(data).json()
150
+ -------------------------------
151
+ """
152
+ self .validate_uid ()
153
+ url = f"{ self .path } /{ self .extension_uid } "
154
+ data = json .dumps (data )
155
+ return self .client .put (url , headers = self .client .headers , data = data )
156
+
157
+
158
+ def delete (self ):
159
+ """
160
+ The "Delete custom field" request deletes a specific custom field.
161
+
162
+ :return: The delete() method returns the status code and message as a response.
163
+ -------------------------------
164
+ [Example:]
165
+ >>> from contentstack_management import contentstack
166
+ >>> client = contentstack.client(authtoken='your_authtoken')
167
+ >>> result = client.stack('api_key').extension('extension_uid').delete().json()
168
+ -------------------------------
169
+ """
170
+ self .validate_uid ()
171
+ url = f"{ self .path } /{ self .extension_uid } "
172
+ return self .client .delete (url , headers = self .client .headers )
173
+
174
+ def validate_uid (self ):
175
+ if self .extension_uid is None or '' :
176
+ raise ArgumentException ("Extension Uid is required" )
177
+
178
+ def encode_multipart_formdata (self , fields ):
179
+ # Create a MultipartEncoder instance with the specified fields
180
+ encoder = MultipartEncoder (fields )
181
+ # Set the content type to the encoder's content type
182
+ content_type = encoder .content_type
183
+ # Get the encoded body
184
+ body = encoder .to_string ()
185
+ return content_type , body
0 commit comments