Skip to content

Commit 277569b

Browse files
Implemented Delivery token and Management token class (#37) (#38)
1 parent 209149e commit 277569b

File tree

22 files changed

+1691
-5
lines changed

22 files changed

+1691
-5
lines changed

.talismanrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,18 @@ fileignoreconfig:
290290
- filename: tests/unit/releases/test_release_unit.py
291291
checksum: ffef2e354ac901eafd0f0aa8a95394892ec23b9d8b807de3e096a28536a75126
292292
version: ""
293+
fileignoreconfig:
294+
- filename: tests/mock/management_token/test_management_token_mock.py
295+
checksum: 3e477590f628e3f713a7e17e397b1cb2f4d07400e70165dbc6b880be2e5c85d8
296+
- filename: tests/mock/delivery_token/test_delivery_token_mock.py
297+
checksum: 295c1c8b7474c6d6c27e9020de0b4c8ee4546f24db66324279abc841b8068e1f
298+
- filename: tests/api/delivery_token/test_delivery_token_api.py
299+
checksum: 169ceff6f69e548878508c090016bb6df2a04b4d81f6a4139c10dc85dc244996
300+
- filename: tests/unit/delivery_token/test_delivery_token_unit.py
301+
checksum: b078f78a4856f5f3aff7627747b6fae4d1dfcbd2f93fb2ec47ce1e9fd00384ac
302+
- filename: tests/api/management_token/test_management_token_api.py
303+
checksum: 11318bdb60aebf8a65c78fcb0bdd6cf6b42bb6ba6462871b170d95be08caf5b0
304+
- filename: tests/unit/management_token/test_management_token_unit.py
305+
checksum: ddb2b5dd54b6ac762fa78936196a4b2088d82254c0ee25fccae290375d1f71e7
306+
version: ""
293307

contentstack_management/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from .bulk_operations.bulk_operation import BulkOperation
2828
from .releases.release import Releases
2929
from .release_items.release_item import ReleaseItems
30+
from .delivery_token.delivery_token import DeliveryToken
31+
from .management_token.management_token import ManagementToken
3032

3133

3234
__all__ = (
@@ -57,7 +59,9 @@
5759
"Terms",
5860
"BulkOperation",
5961
"Releases",
60-
"ReleaseItems"
62+
"ReleaseItems",
63+
"DeliveryToken",
64+
"ManagementToken"
6165
)
6266

6367
__title__ = 'contentstack-management-python'
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
11+
class DeliveryToken(Parameter):
12+
"""
13+
This class takes a base URL as an argument when it's initialized,
14+
which is the endpoint for the RESTFUL API that
15+
we'll be interacting with. The create(), read(), update(), and delete()
16+
methods each correspond to the CRUD
17+
operations that can be performed on the API """
18+
19+
def __init__(self, client, delivery_token_uid: str):
20+
self.client = client
21+
self.delivery_token_uid = delivery_token_uid
22+
super().__init__(self.client)
23+
24+
self.path = "stacks/delivery_tokens"
25+
26+
def find(self):
27+
"""
28+
The "Get all delivery tokens" request returns the details of all the delivery tokens created in a stack.
29+
:return: Json, with delivery_token details.
30+
31+
-------------------------------
32+
[Example:]
33+
34+
>>> from contentstack_management import contentstack
35+
>>> client = contentstack.client(authtoken='your_authtoken')
36+
>>> result = client.stack("api_key").delivery_token().find().json()
37+
38+
-------------------------------
39+
"""
40+
return self.client.get(self.path, headers = self.client.headers)
41+
42+
def fetch(self):
43+
"""
44+
The "Get a single delivery token" request returns the details of all the delivery tokens created in a stack.
45+
:return: Json, with delivery_token details.
46+
-------------------------------
47+
[Example:]
48+
49+
>>> from contentstack_management import contentstack
50+
>>> client = contentstack.client(authtoken='your_authtoken')
51+
>>> result = client.stack('api_key').delivery_token('delivery_token_uid').fetch().json()
52+
53+
-------------------------------
54+
"""
55+
self.validate_uid()
56+
url = f"{self.path}/{self.delivery_token_uid}"
57+
return self.client.get(url, headers = self.client.headers)
58+
59+
60+
def create(self, data: dict):
61+
"""
62+
The Create delivery token request creates a delivery token in the stack.
63+
64+
:param data: The `data` parameter is the payload that you want to send in the request body. It
65+
should be a dictionary or a JSON serializable object that you want to send as the request body
66+
:return: Json, with delivery_token details.
67+
68+
-------------------------------
69+
[Example:]
70+
>>> data ={
71+
>>> "token":{
72+
>>> "name":"Test",
73+
>>> "description":"This is a demo token.",
74+
>>> "scope":[
75+
>>> {
76+
>>> "module":"environment",
77+
>>> "environments":[
78+
>>> "production"
79+
>>> ],
80+
>>> "acl":{
81+
>>> "read":true
82+
>>> }
83+
>>> },
84+
>>> {
85+
>>> "module":"branch",
86+
>>> "branches":[
87+
>>> "main",
88+
>>> "development"
89+
>>> ],
90+
>>> "acl":{
91+
>>> "read":true
92+
>>> }
93+
>>> },
94+
>>> {
95+
>>> "module":"branch_alias",
96+
>>> "branch_aliases":[
97+
>>> "deploy",
98+
>>> "release"
99+
>>> ],
100+
>>> "acl":{
101+
>>> "read":true
102+
>>> }
103+
>>> }
104+
>>> ]
105+
>>> }
106+
>>> }
107+
>>> from contentstack_management import contentstack
108+
>>> client = contentstack.client(authtoken='your_authtoken')
109+
>>> result = client.stack('api_key').delivery_token().create(data).json()
110+
111+
-------------------------------
112+
"""
113+
114+
data = json.dumps(data)
115+
return self.client.post(self.path, headers = self.client.headers, data=data)
116+
117+
def update(self, data: dict):
118+
"""
119+
The "Update delivery token" request lets you update the details of a delivery token.
120+
121+
:param data: The `data` parameter is the data that you want to update. It should be a dictionary
122+
or an object that can be serialized to JSON
123+
:return: Json, with updated delivery_token details.
124+
-------------------------------
125+
[Example:]
126+
>>> data = {
127+
>>> "token":{
128+
>>> "name":"Test",
129+
>>> "description":"This is a updated token.",
130+
>>> "scope":[
131+
>>> {
132+
>>> "module":"environment",
133+
>>> "environments":[
134+
>>> "production"
135+
>>> ],
136+
>>> "acl":{
137+
>>> "read":true
138+
>>> }
139+
>>> },
140+
>>> {
141+
>>> "module":"branch",
142+
>>> "branches":[
143+
>>> "main",
144+
>>> "development"
145+
>>> ],
146+
>>> "acl":{
147+
>>> "read":true
148+
>>> }
149+
>>> },
150+
>>> {
151+
>>> "module":"branch_alias",
152+
>>> "branch_aliases":[
153+
>>> "deploy"
154+
>>> ],
155+
>>> "acl":{
156+
>>> "read":true
157+
>>> }
158+
>>> }
159+
>>> ]
160+
>>> }
161+
>>> }
162+
>>> from contentstack_management import contentstack
163+
>>> client = contentstack.client(authtoken='your_authtoken')
164+
>>> result = client.stack('api_key').delivery_token("delivery_token_uid").update(data).json()
165+
166+
-------------------------------
167+
"""
168+
self.validate_uid()
169+
url = f"{self.path}/{self.delivery_token_uid}"
170+
data = json.dumps(data)
171+
return self.client.put(url, headers = self.client.headers, data=data)
172+
173+
174+
def delete(self):
175+
"""
176+
The "Delete delivery token" request deletes a specific delivery token.
177+
178+
:return: The delete() method returns the status code and message as a response.
179+
180+
-------------------------------
181+
[Example:]
182+
183+
>>> from contentstack_management import contentstack
184+
>>> client = contentstack.client(authtoken='your_authtoken')
185+
>>> result = client.stack('api_key').delivery_token('delivery_token_uid').delete().json()
186+
187+
-------------------------------
188+
"""
189+
self.validate_uid()
190+
url = f"{self.path}/{self.delivery_token_uid}"
191+
return self.client.delete(url, headers = self.client.headers)
192+
193+
def validate_uid(self):
194+
if self.delivery_token_uid is None or '':
195+
raise ArgumentException("Delivery Token Uid is required")

0 commit comments

Comments
 (0)