|
| 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 | + |
| 8 | +class Globalfields: |
| 9 | + """ |
| 10 | + This class takes a base URL as an argument when it's initialized, |
| 11 | + which is the endpoint for the RESTFUL API that |
| 12 | + we'll be interacting with. The create(), read(), update(), and delete() |
| 13 | + methods each correspond to the CRUD |
| 14 | + operations that can be performed on the API """ |
| 15 | + |
| 16 | + def __init__(self, endpoint, authtoken, headers, api_client, api_key, global_field_uid): |
| 17 | + self.api_client = api_client |
| 18 | + self.endpoint = endpoint |
| 19 | + self.authtoken = authtoken |
| 20 | + self.headers = headers |
| 21 | + self.api_key = api_key |
| 22 | + self.global_field_uid = global_field_uid |
| 23 | + |
| 24 | + def find(self): |
| 25 | + """ |
| 26 | + Find the global fields entries |
| 27 | + :return: Json, with global fields details. |
| 28 | + ------------------------------- |
| 29 | + [Example:] |
| 30 | +
|
| 31 | + >>> from contentstack_management import contentstack |
| 32 | + >>> client = contentstack.client(host='HOST NAME') |
| 33 | + >>> client.login(email="email_id", password="password") |
| 34 | + >>> result = client.stack("API_KEY").global_fields('global_field_uid').find().json() |
| 35 | +
|
| 36 | + ------------------------------- |
| 37 | + """ |
| 38 | + url = "global_fields" |
| 39 | + self.headers['api_key'] = self.api_key |
| 40 | + self.headers['authtoken'] = self.authtoken |
| 41 | + return self.api_client.get(url, headers = self.headers) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + def fetch(self): |
| 46 | + """ |
| 47 | + Fetches the global fields entry |
| 48 | + :return: Json, with global fields details. |
| 49 | + ------------------------------- |
| 50 | + [Example:] |
| 51 | +
|
| 52 | + >>> from contentstack_management import contentstack |
| 53 | + >>> client = contentstack.client(host='HOST NAME') |
| 54 | + >>> client.login(email="email_id", password="password") |
| 55 | + >>> result = client.stack('API_KEY').global_fields('global_field_uid').fetch().json() |
| 56 | +
|
| 57 | + ------------------------------- |
| 58 | + """ |
| 59 | + url = f"global_fields/{self.global_field_uid}" |
| 60 | + self.headers['authtoken'] = self.authtoken |
| 61 | + self.headers['api_key'] = self.api_key |
| 62 | + return self.api_client.get(url, headers = self.headers) |
| 63 | + |
| 64 | + |
| 65 | + def create(self, data): |
| 66 | + """ |
| 67 | + Create the global fields entries |
| 68 | + :return: Json, with global fields details. |
| 69 | + ------------------------------- |
| 70 | + [Example:] |
| 71 | + >>> data = { |
| 72 | + "global_field": { |
| 73 | + "title": "Servlet", |
| 74 | + "uid": "servlet", |
| 75 | + "schema": [{ |
| 76 | + "display_name": "Name", |
| 77 | + "uid": "name", |
| 78 | + "data_type": "text" |
| 79 | + }, { |
| 80 | + "data_type": "text", |
| 81 | + "display_name": "Rich text editor", |
| 82 | + "uid": "description", |
| 83 | + "field_metadata": { |
| 84 | + "allow_rich_text": true, |
| 85 | + "description": "", |
| 86 | + "multiline": false, |
| 87 | + "rich_text_type": "advanced", |
| 88 | + "options": [], |
| 89 | + "version": 3 |
| 90 | + }, |
| 91 | + "multiple": false, |
| 92 | + "mandatory": false, |
| 93 | + "unique": false |
| 94 | + }] |
| 95 | + } |
| 96 | + } |
| 97 | + >>> from contentstack_management import contentstack |
| 98 | + >>> client = contentstack.client(host='HOST NAME') |
| 99 | + >>> client.login(email="email_id", password="password") |
| 100 | + >>> result = client.stack('API_KEY').global_fields().create(data).json() |
| 101 | +
|
| 102 | + ------------------------------- |
| 103 | + """ |
| 104 | + url = "global_fields" |
| 105 | + self.headers['api_key'] = self.api_key |
| 106 | + self.headers['authtoken'] = self.authtoken |
| 107 | + data = json.dumps(data) |
| 108 | + return self.api_client.post(url, headers = self.headers, data=data) |
| 109 | + |
| 110 | + def update(self, data): |
| 111 | + """ |
| 112 | + Update the global fields entries |
| 113 | + :return: Json, with global fields details. |
| 114 | + ------------------------------- |
| 115 | + [Example:] |
| 116 | + >>> data = { |
| 117 | + "global_field": { |
| 118 | + "title": "Servlet", |
| 119 | + "uid": "servlet", |
| 120 | + "schema": [{ |
| 121 | + "display_name": "Name", |
| 122 | + "uid": "name", |
| 123 | + "data_type": "text" |
| 124 | + }, { |
| 125 | + "data_type": "text", |
| 126 | + "display_name": "Rich text editor", |
| 127 | + "uid": "description", |
| 128 | + "field_metadata": { |
| 129 | + "allow_rich_text": true, |
| 130 | + "description": "", |
| 131 | + "multiline": false, |
| 132 | + "rich_text_type": "advanced", |
| 133 | + "options": [], |
| 134 | + "version": 3 |
| 135 | + }, |
| 136 | + "multiple": false, |
| 137 | + "mandatory": false, |
| 138 | + "unique": false |
| 139 | + }] |
| 140 | + } |
| 141 | + } |
| 142 | + >>> from contentstack_management import contentstack |
| 143 | + >>> client = contentstack.client(host='HOST NAME') |
| 144 | + >>> client.login(email="email_id", password="password") |
| 145 | + >>> result = client.stack('API_KEY').global_fields('global_field_uid').update(data).json() |
| 146 | +
|
| 147 | + ------------------------------- |
| 148 | + """ |
| 149 | + url = f"global_fields/{self.global_field_uid}" |
| 150 | + self.headers['authtoken'] = self.authtoken |
| 151 | + self.headers['api_key'] = self.api_key |
| 152 | + data = json.dumps(data) |
| 153 | + return self.api_client.put(url, headers = self.headers, data=data) |
| 154 | + |
| 155 | + def delete(self): |
| 156 | + """ |
| 157 | + Delete the global fields |
| 158 | + :return: Json, with status code and message. |
| 159 | + ------------------------------- |
| 160 | + [Example:] |
| 161 | +
|
| 162 | + >>> from contentstack_management import contentstack |
| 163 | + >>> client = contentstack.client(host='HOST NAME') |
| 164 | + >>> client.login(email="email_id", password="password") |
| 165 | + >>> result = result = client.stack('API_KEY').global_fields('global_field_uid').delete().json() |
| 166 | +
|
| 167 | + ------------------------------- |
| 168 | + """ |
| 169 | + url = f"global_fields/{self.global_field_uid}" |
| 170 | + self.headers['authtoken'] = self.authtoken |
| 171 | + self.headers['api_key'] = self.api_key |
| 172 | + params = {'force': True} |
| 173 | + return self.api_client.delete(url, headers = self.headers, params = params) |
| 174 | + |
| 175 | + def imports(self, file_path): |
| 176 | + """ |
| 177 | + Import the global fields |
| 178 | + :return: Json, with global fields details. |
| 179 | + ------------------------------- |
| 180 | + [Example:] |
| 181 | +
|
| 182 | + >>> from contentstack_management import contentstack |
| 183 | + >>> client = contentstack.client(host='HOST NAME') |
| 184 | + >>> client.login(email="email_id", password="password") |
| 185 | + >>> file_path = "tests/resources/mock_global_fields/import_global_fields.json" |
| 186 | + >>> result = client.stack('API_KEY').global_fields().imports(file_path).json() |
| 187 | +
|
| 188 | + ------------------------------- |
| 189 | + """ |
| 190 | + url = f"global_fields/import" |
| 191 | + self.headers['authtoken'] = self.authtoken |
| 192 | + self.headers['api_key'] = self.api_key |
| 193 | + self.headers['Content-Type'] = "multipart/form-data" |
| 194 | + params = {'include_branch': False} |
| 195 | + files = {'global_field': open(f"{file_path}",'rb')} |
| 196 | + return self.api_client.post(url, headers = self.headers, params = params, files = files) |
| 197 | + |
| 198 | + def export(self): |
| 199 | + """ |
| 200 | + Export the global fields |
| 201 | + :return: Json, with global fields details. |
| 202 | + ------------------------------- |
| 203 | + [Example:] |
| 204 | +
|
| 205 | + >>> from contentstack_management import contentstack |
| 206 | + >>> client = contentstack.client(host='HOST NAME') |
| 207 | + >>> client.login(email="email_id", password="password") |
| 208 | + >>> result = client.stack('API_KEY').global_fields().export().json() |
| 209 | +
|
| 210 | + ------------------------------- |
| 211 | + """ |
| 212 | + url = f"global_fields/{self.global_field_uid}/export" |
| 213 | + self.headers['authtoken'] = self.authtoken |
| 214 | + self.headers['api_key'] = self.api_key |
| 215 | + return self.api_client.get(url, headers = self.headers) |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
0 commit comments