Skip to content

Commit 7c5d513

Browse files
- General code improvements
- Corrected few testcases - Single place for credentials - linting added
1 parent d224cc3 commit 7c5d513

36 files changed

+2781
-949
lines changed

apidoc/Test Results - .html

Lines changed: 1824 additions & 0 deletions
Large diffs are not rendered by default.

contentstack_management/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""The __init__.py file that contains modules that need to import"""
22

3-
43
from contentstack_management import contentstack
54

65
__title__ = 'contentstack-cms'

contentstack_management/contentstack.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import contentstack_management
44
from .core.client import ApiClient
55

6-
76
default_host = 'api.contentstack.io'
87
default_endpoint = 'https://api.contentstack.io'
98
default_api_version = 'v3'
@@ -31,7 +30,7 @@ def user_agents(headers):
3130
), 'os': str(__platform())})
3231

3332
package = f"contentstack-management-python/{contentstack_management.__version__}"
34-
return {'User-Agent': str(headers), "X-User-Agent": package, 'Content-Type': 'application/json' }
33+
return {'User-Agent': str(headers), "X-User-Agent": package, 'Content-Type': 'application/json'}
3534

3635

3736
def client(endpoint=None,
@@ -76,4 +75,3 @@ def client(endpoint=None,
7675
headers=headers, authorization=authorization,
7776
timeout=timeout, failure_retry=failure_retry, exceptions=exceptions, errors=errors,
7877
max_requests=max_requests, retry_on_error=retry_on_error)
79-

contentstack_management/core/client.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
The create(), read(), update(), and delete() methods each correspond to
44
the CRUD operations that can be performed on the API """
55

6-
import requests
76
import json
7+
8+
import requests
9+
810
from ..organizations.organizations import Organization
9-
from ..users.user import User
1011
from ..stack.stack import Stack
1112
from ..user_session.user_session import UserSession
13+
from ..users.user import User
14+
1215

1316
class ApiClient:
1417
"""
@@ -32,7 +35,6 @@ def __init__(self, endpoint, host, headers, authtoken, authorization, failure_re
3235
self.max_requests = max_requests
3336
self.retry_on_error = retry_on_error
3437

35-
3638
def get(self, url, headers=None, params=None):
3739
"""
3840
Perform an HTTP GET request with the specified URL and parameters.
@@ -65,7 +67,7 @@ def post(self, url, headers=None, params=None, data=None, json_data=None):
6567
:param headers: Optional dictionary of headers to include in the request.
6668
:param params: Optional dictionary of URL parameters to include in the request.
6769
:param data: Optional dictionary, list of tuples, or bytes to include in the body of the request.
68-
:param json: Optional JSON data to include in the body of the request.
70+
:param json_data: Optional JSON data to include in the body of the request.
6971
:return: The response from the server.
7072
"""
7173
return self._call_request('POST', url, headers=headers, params=params, data=data, json_data=json_data)
@@ -80,9 +82,7 @@ def delete(self, url, headers=None, params=None):
8082
:return: The response from the server.
8183
"""
8284
return self._call_request('DELETE', url, headers=headers, params=params)
83-
8485

85-
8686
def _call_request(self, method, url_path, headers=None, params=None, data=None, json_data=None):
8787
url = f"{self.endpoint}/{url_path}"
8888
retries = self.failure_retry + 1
@@ -108,7 +108,7 @@ def _call_request(self, method, url_path, headers=None, params=None, data=None,
108108
else:
109109
return None
110110

111-
def login(self, email=None, password=None):
111+
def login(self, email=None, password=None):
112112
"""
113113
Fetches the user details and logging into the stack
114114
:return: User details, fetch authtoken.
@@ -122,17 +122,15 @@ def login(self, email=None, password=None):
122122
"""
123123

124124
self.api_client = ApiClient(
125-
host=self.host, endpoint=self.endpoint, authtoken=self.authtoken,
126-
headers=self.headers, authorization=self.authorization,
127-
timeout=self.timeout, failure_retry=self.failure_retry, exceptions=self.exceptions, errors=self.errors,
128-
max_requests=self.max_requests, retry_on_error=self.retry_on_error
125+
host=self.host, endpoint=self.endpoint, authtoken=self.authtoken,
126+
headers=self.headers, authorization=self.authorization,
127+
timeout=self.timeout, failure_retry=self.failure_retry, exceptions=self.exceptions, errors=self.errors,
128+
max_requests=self.max_requests, retry_on_error=self.retry_on_error
129129
)
130-
response = UserSession(username=email, password= password, api_client=self.api_client).login()
131-
self.auth_token = self.get_authtoken(response.json()) if response.status_code == 200 else self.authtoken
130+
response = UserSession(username=email, password=password, api_client=self.api_client).login()
131+
self.auth_token = self.get_authtoken(response.json()) if response.status_code == 200 else self.authtoken
132132
return response
133-
134133

135-
136134
def logout(self):
137135
"""
138136
Logging out from the stack
@@ -145,7 +143,7 @@ def logout(self):
145143
>>> response = client.logout()
146144
-------------------------------
147145
"""
148-
return UserSession(api_client = self.api_client).logout()
146+
return UserSession(api_client=self.api_client).logout()
149147

150148
def get_authtoken(self, response):
151149
"""
@@ -160,15 +158,13 @@ def get_authtoken(self, response):
160158
>>> auth_token = client.get_authtoken(response)
161159
-------------------------------
162160
"""
163-
return response['user']['authtoken']
164-
161+
return response['user']['authtoken']
162+
165163
def user(self):
166-
return User(self.endpoint, self.auth_token, self.headers,self.api_client)
167-
168-
def organizations(self, organization_uid = None):
169-
return Organization(self.endpoint, self.auth_token, self.headers,self.api_client, organization_uid)
170-
171-
def stack(self, api_key = None):
172-
return Stack(self.endpoint, self.auth_token, self.headers,self.api_client, api_key)
173-
164+
return User(self.endpoint, self.auth_token, self.headers, self.api_client)
165+
166+
def organizations(self, organization_uid=None):
167+
return Organization(self.endpoint, self.auth_token, self.headers, self.api_client, organization_uid)
174168

169+
def stack(self, api_key=None):
170+
return Stack(self.endpoint, self.auth_token, self.headers, self.api_client, api_key)

contentstack_management/global_fields/global_fields.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import json
77

8+
89
class Globalfields:
910
"""
1011
This class takes a base URL as an argument when it's initialized,
@@ -38,10 +39,8 @@ def find(self):
3839
url = "global_fields"
3940
self.headers['api_key'] = self.api_key
4041
self.headers['authtoken'] = self.authtoken
41-
return self.api_client.get(url, headers = self.headers)
42-
43-
44-
42+
return self.api_client.get(url, headers=self.headers)
43+
4544
def fetch(self):
4645
"""
4746
Fetches the global fields entry
@@ -59,9 +58,8 @@ def fetch(self):
5958
url = f"global_fields/{self.global_field_uid}"
6059
self.headers['authtoken'] = self.authtoken
6160
self.headers['api_key'] = self.api_key
62-
return self.api_client.get(url, headers = self.headers)
63-
64-
61+
return self.api_client.get(url, headers=self.headers)
62+
6563
def create(self, data):
6664
"""
6765
Create the global fields entries
@@ -105,8 +103,8 @@ def create(self, data):
105103
self.headers['api_key'] = self.api_key
106104
self.headers['authtoken'] = self.authtoken
107105
data = json.dumps(data)
108-
return self.api_client.post(url, headers = self.headers, data=data)
109-
106+
return self.api_client.post(url, headers=self.headers, data=data)
107+
110108
def update(self, data):
111109
"""
112110
Update the global fields entries
@@ -150,8 +148,8 @@ def update(self, data):
150148
self.headers['authtoken'] = self.authtoken
151149
self.headers['api_key'] = self.api_key
152150
data = json.dumps(data)
153-
return self.api_client.put(url, headers = self.headers, data=data)
154-
151+
return self.api_client.put(url, headers=self.headers, data=data)
152+
155153
def delete(self):
156154
"""
157155
Delete the global fields
@@ -170,8 +168,8 @@ def delete(self):
170168
self.headers['authtoken'] = self.authtoken
171169
self.headers['api_key'] = self.api_key
172170
params = {'force': True}
173-
return self.api_client.delete(url, headers = self.headers, params = params)
174-
171+
return self.api_client.delete(url, headers=self.headers, params=params)
172+
175173
def imports(self, file_path):
176174
"""
177175
Import the global fields
@@ -192,9 +190,9 @@ def imports(self, file_path):
192190
self.headers['api_key'] = self.api_key
193191
self.headers['Content-Type'] = "multipart/form-data"
194192
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-
193+
files = {'global_field': open(f"{file_path}", 'rb')}
194+
return self.api_client.post(url, headers=self.headers, params=params, files=files)
195+
198196
def export(self):
199197
"""
200198
Export the global fields
@@ -212,15 +210,4 @@ def export(self):
212210
url = f"global_fields/{self.global_field_uid}/export"
213211
self.headers['authtoken'] = self.authtoken
214212
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-
213+
return self.api_client.get(url, headers=self.headers)

contentstack_management/organizations/organizations.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
import json
32

3+
44
class Organization:
55
"""
66
This class takes a base URL as an argument when it's initialized,
@@ -17,7 +17,6 @@ def __init__(self, endpoint, authtoken, headers, api_client, organization_uid):
1717
self.organization_uid = organization_uid
1818
self.headers['authtoken'] = self.authtoken
1919

20-
2120
def find(self):
2221
"""
2322
Finds the organizations entries
@@ -32,8 +31,8 @@ def find(self):
3231
-------------------------------
3332
"""
3433
url = "organizations"
35-
return self.api_client.get(url, headers = self.headers)
36-
34+
return self.api_client.get(url, headers=self.headers)
35+
3736
def fetch(self):
3837
"""
3938
Fetches the organizations entry
@@ -49,9 +48,8 @@ def fetch(self):
4948
-------------------------------
5049
"""
5150
url = f"organizations/{self.organization_uid}"
52-
return self.api_client.get(url, headers = self.headers)
51+
return self.api_client.get(url, headers=self.headers)
5352

54-
5553
def roles(self):
5654
"""
5755
Fetches the organization roles entries
@@ -67,9 +65,8 @@ def roles(self):
6765
-------------------------------
6866
"""
6967
url = f"organizations/{self.organization_uid}/roles"
70-
return self.api_client.get(url, headers = self.headers)
68+
return self.api_client.get(url, headers=self.headers)
7169

72-
7370
def add_users(self, user_data):
7471
"""
7572
Add user to the organization
@@ -104,8 +101,8 @@ def add_users(self, user_data):
104101
"""
105102
url = f"organizations/{self.organization_uid}/share"
106103
data = json.dumps(user_data)
107-
return self.api_client.post(url, headers = self.headers, data = data)
108-
104+
return self.api_client.post(url, headers=self.headers, data=data)
105+
109106
def transfer_ownership(self, data):
110107
"""
111108
Add user to the organization
@@ -126,9 +123,8 @@ def transfer_ownership(self, data):
126123

127124
url = f"organizations/{self.organization_uid}/transfer-ownership"
128125
data = json.dumps(data)
129-
return self.api_client.post(url, headers = self.headers, data = data)
126+
return self.api_client.post(url, headers=self.headers, data=data)
130127

131-
132128
def stacks(self):
133129
"""
134130
Fetches the organization stacks
@@ -144,9 +140,8 @@ def stacks(self):
144140
-------------------------------
145141
"""
146142
url = f"organizations/{self.organization_uid}/stacks"
147-
return self.api_client.get(url, headers = self.headers)
143+
return self.api_client.get(url, headers=self.headers)
148144

149-
150145
def logs(self):
151146
"""
152147
Fetches the organization log entries
@@ -162,5 +157,4 @@ def logs(self):
162157
-------------------------------
163158
"""
164159
url = f"organizations/{self.organization_uid}/logs"
165-
return self.api_client.get(url, headers = self.headers)
166-
160+
return self.api_client.get(url, headers=self.headers)

0 commit comments

Comments
 (0)