Skip to content

Commit 54119d1

Browse files
Implemented publish queue class (#39) (#40)
1 parent 277569b commit 54119d1

File tree

12 files changed

+340
-2
lines changed

12 files changed

+340
-2
lines changed

.talismanrc

+8
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,12 @@ fileignoreconfig:
304304
- filename: tests/unit/management_token/test_management_token_unit.py
305305
checksum: ddb2b5dd54b6ac762fa78936196a4b2088d82254c0ee25fccae290375d1f71e7
306306
version: ""
307+
fileignoreconfig:
308+
- filename: tests/api/publish_queue/test_publish_queue_api.py
309+
checksum: 8364fcd077ec8e99556738460d0659e2ec7bf8747969428f2d99761ef3ae4020
310+
- filename: tests/mock/publish_queue/test_publish_queue_mock.py
311+
checksum: 6581d4def863f65e0321e0ff3e9ee074aa5ae1a53bf46035ea01b41e9de0bc36
312+
- filename: tests/unit/publish_queue/test_publish_queue_unit.py
313+
checksum: add55174c8c3ee4e931433e9bf54a80304d6927681912a19e5ee78747dc608bf
314+
version: ""
307315

contentstack_management/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .release_items.release_item import ReleaseItems
3030
from .delivery_token.delivery_token import DeliveryToken
3131
from .management_token.management_token import ManagementToken
32+
from .publish_queue.publish_queue import PublishQueue
3233

3334

3435
__all__ = (
@@ -61,7 +62,8 @@
6162
"Releases",
6263
"ReleaseItems",
6364
"DeliveryToken",
64-
"ManagementToken"
65+
"ManagementToken",
66+
"PublishQueue"
6567
)
6668

6769
__title__ = 'contentstack-management-python'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from contentstack_management import contentstack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 PublishQueue(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, publish_queue_uid: str):
20+
self.client = client
21+
self.publish_queue_uid = publish_queue_uid
22+
super().__init__(self.client)
23+
24+
self.path = "publish-queue"
25+
26+
def find(self):
27+
"""
28+
The "Get publish queue" request returns comprehensive information on activities such as publish, unpublish, and delete performed on entries and/or assets.
29+
This request also includes the details of the release deployments in the response body.
30+
:return: Json, with publish_queue details.
31+
32+
-------------------------------
33+
[Example:]
34+
35+
>>> from contentstack_management import contentstack
36+
>>> client = contentstack.client(authtoken='your_authtoken')
37+
>>> result = client.stack("api_key").publish_queue().find().json()
38+
39+
-------------------------------
40+
"""
41+
return self.client.get(self.path, headers = self.client.headers)
42+
43+
44+
45+
def fetch(self):
46+
"""
47+
The "Get publish queue activity" request returns comprehensive information on a specific publish, unpublish, or delete action performed on an entry and/or asset. You can also retrieve details of a specific release deployment.
48+
49+
:return: Json, with publish_queue details.
50+
-------------------------------
51+
[Example:]
52+
53+
>>> from contentstack_management import contentstack
54+
>>> client = contentstack.client(authtoken='your_authtoken')
55+
>>> result = client.stack('api_key').publish_queue('publish_queue_uid').fetch().json()
56+
57+
-------------------------------
58+
"""
59+
self.validate_uid()
60+
url = f"{self.path}/{self.publish_queue_uid}"
61+
return self.client.get(url, headers = self.client.headers)
62+
63+
64+
def cancel(self):
65+
"""
66+
The "Cancel Scheduled Action" request will allow you to cancel any scheduled publishing or unpublishing activity of entries and/or assets and cancel the deployment of releases.
67+
68+
:param data: The `data` parameter is the payload that you want to send in the request body. It
69+
should be a dictionary or a JSON serializable object that you want to send as the request body
70+
:return: Json, with publish_queue details.
71+
72+
-------------------------------
73+
[Example:]
74+
>>> from contentstack_management import contentstack
75+
>>> client = contentstack.client(authtoken='your_authtoken')
76+
>>> result = client.stack('api_key').publish_queue().create(data).json()
77+
78+
-------------------------------
79+
"""
80+
81+
self.validate_uid()
82+
url = f"{self.path}/{self.publish_queue_uid}/unschedule"
83+
return self.client.get(url, headers = self.client.headers)
84+
85+
86+
def validate_uid(self):
87+
if self.publish_queue_uid is None or '':
88+
raise ArgumentException("Publish Queue Uid is required")

contentstack_management/stack/stack.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ..releases.release import Releases
2020
from ..delivery_token.delivery_token import DeliveryToken
2121
from ..management_token.management_token import ManagementToken
22+
from ..publish_queue.publish_queue import PublishQueue
2223

2324

2425
class Stack(Parameter):
@@ -367,4 +368,7 @@ def delivery_token(self, delivery_token: str = None):
367368
return DeliveryToken(self.client, delivery_token)
368369

369370
def management_token(self, management_token: str = None):
370-
return ManagementToken(self.client, management_token)
371+
return ManagementToken(self.client, management_token)
372+
373+
def publish_queue(self, publish_queue_uid: str = None):
374+
return PublishQueue(self.client, publish_queue_uid)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
import unittest
3+
from dotenv import load_dotenv
4+
from contentstack_management import contentstack
5+
from tests.cred import get_credentials
6+
7+
credentials = get_credentials()
8+
username = credentials["username"]
9+
password = credentials["password"]
10+
host = credentials["host"]
11+
api_key = credentials["api_key"]
12+
publish_queue_uid = credentials["publish_queue_uid"]
13+
14+
class publish_queueApiTests(unittest.TestCase):
15+
16+
def setUp(self):
17+
self.client = contentstack.ContentstackClient(host=host)
18+
self.client.login(username, password)
19+
20+
def test_get_all_publish_queue(self):
21+
response = self.client.stack(api_key).publish_queue().find()
22+
self.assertEqual(response.request.url, f"{self.client.endpoint}publish-queue")
23+
self.assertEqual(response.status_code, 200)
24+
25+
26+
def test_get_a_publish_queue(self):
27+
response = self.client.stack(api_key).publish_queue(publish_queue_uid).fetch()
28+
self.assertEqual(response.request.url,
29+
f"{self.client.endpoint}publish-queue/{publish_queue_uid}")
30+
self.assertEqual(response.status_code, 200)
31+
32+
33+
def test_cancel_a_publish_queue(self):
34+
response = self.client.stack(api_key).publish_queue(publish_queue_uid).cancel()
35+
self.assertEqual(response.request.url,
36+
f"{self.client.endpoint}publish-queue/{publish_queue_uid}/unschedule")
37+
self.assertEqual(response.status_code, 200)
38+
39+
40+

tests/cred.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
default_release_uid = "release_uid" #default release uid
4040
default_delivery_token_uid = "delivery_token_uid" #default delivery token uid
4141
default_management_token_uid = "management_token_uid" #default management token uid
42+
default_publish_queue_uid = "publish_queue_uid" # default publish queue uid
4243

4344
def get_credentials():
4445
load_dotenv()
@@ -83,6 +84,7 @@ def get_credentials():
8384
"release_uid": os.getenv("RELEASE_UID", default_release_uid),
8485
"delivery_token_uid": os.getenv("DELIVERY_TOKEN_UID", default_delivery_token_uid),
8586
"management_token_uid": os.getenv("MANAGEMENT_TOKEN_UID", default_management_token_uid),
87+
"publish_queue_uid": os.getenv("PUBLISH_QUEUE_UID", default_publish_queue_uid),
8688

8789
}
8890
return credentials
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from dotenv import load_dotenv
6+
from contentstack_management import contentstack
7+
from tests.cred import get_credentials
8+
9+
credentials = get_credentials()
10+
username = credentials["username"]
11+
password = credentials["password"]
12+
api_key = credentials["api_key"]
13+
host = credentials["host"]
14+
publish_queue_uid = credentials["publish_queue_uid"]
15+
16+
17+
class publish_queueMockTests(unittest.TestCase):
18+
19+
def setUp(self):
20+
21+
self.client = contentstack.ContentstackClient(host = host)
22+
self.client.login(username, password)
23+
24+
25+
def read_file(self, file_name):
26+
file_path= f"tests/resources/mock_publish_queue/{file_name}"
27+
infile = open(file_path, 'r')
28+
data = infile.read()
29+
infile.close()
30+
return data
31+
32+
33+
def test_get_all_publish_queue(self):
34+
response = self.client.stack(api_key).publish_queue().find().json()
35+
read_mock_publish_queue_data = self.read_file("find.json")
36+
mock_publish_queue_data = json.loads(read_mock_publish_queue_data)
37+
self.assertEqual(mock_publish_queue_data.keys(), response.keys())
38+
39+
def test_get_a_publish_queue(self):
40+
response = self.client.stack(api_key).publish_queue(publish_queue_uid).fetch().json()
41+
read_mock_publish_queue_data = self.read_file("fetch.json")
42+
mock_publish_queue_data = json.loads(read_mock_publish_queue_data)
43+
self.assertEqual(mock_publish_queue_data.keys(), response.keys())
44+
45+
def test_cancel_a_publish_queue(self):
46+
response = self.client.stack(api_key).publish_queue(publish_queue_uid).cancel().json()
47+
read_mock_publish_queue_data = self.read_file("cancel.json")
48+
mock_publish_queue_data = json.loads(read_mock_publish_queue_data)
49+
self.assertEqual(mock_publish_queue_data.keys(), response.keys())
50+
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"notice": "Release unscheduled successfully."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"entry": {
3+
"uid": "publish_queue_uid",
4+
"stack": "api_key",
5+
"created_at": "2023-09-27T17:50:55.624Z",
6+
"updated_at": "2023-09-27T17:50:55.624Z",
7+
"created_by": "created_by_uid",
8+
"updated_by": "updated_by_uid",
9+
"type": "release",
10+
"publish_details": {
11+
"status": "success"
12+
},
13+
"entry": {
14+
"title": "New_Name",
15+
"uid": "entry_uid",
16+
"skip_items_webhook_trigger": false,
17+
"release": {
18+
"uid": "release_uid",
19+
"title": "New_Name"
20+
}
21+
},
22+
"environment": [
23+
"environment_uid"
24+
],
25+
"action": "deploy",
26+
"published_at": "2023-09-27T17:50:55.536Z",
27+
"user": "user_uid",
28+
"approval": false,
29+
"approved": true,
30+
"rejected": false
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"queue": [
3+
{
4+
"uid": "queue_uid",
5+
"stack": "api_key",
6+
"created_at": "2023-09-28T10:48:20.416Z",
7+
"updated_at": "2023-09-28T10:48:20.416Z",
8+
"created_by": "created_by_uid",
9+
"updated_by": "updated_by_uid",
10+
"type": "release",
11+
"publish_details": {
12+
"status": "success"
13+
},
14+
"entry": {
15+
"title": "tesrting2",
16+
"uid": "entry_uid",
17+
"skip_items_webhook_trigger": false,
18+
"release": {
19+
"uid": "release_uid",
20+
"title": "tesrting2"
21+
}
22+
},
23+
"environment": [
24+
"environment_uid"
25+
],
26+
"action": "deploy",
27+
"published_at": "2023-09-28T10:48:20.334Z",
28+
"user": "user_uid",
29+
"approval": false,
30+
"approved": true,
31+
"rejected": false
32+
},
33+
{
34+
"uid": "queue_uid",
35+
"stack": "api_key",
36+
"created_at": "2023-09-27T18:32:01.934Z",
37+
"updated_at": "2023-09-27T18:32:01.934Z",
38+
"created_by": "created_by_uid",
39+
"updated_by": "updated_by_uid",
40+
"type": "release",
41+
"publish_details": {
42+
"status": "success"
43+
},
44+
"entry": {
45+
"title": "New_Name",
46+
"uid": "entry_uid",
47+
"skip_items_webhook_trigger": false,
48+
"release": {
49+
"uid": "release_uid",
50+
"title": "New_Name"
51+
}
52+
},
53+
"environment": [
54+
"environment_uid"
55+
],
56+
"action": "deploy",
57+
"published_at": "2023-09-27T18:32:01.780Z",
58+
"user": "user_uid",
59+
"approval": false,
60+
"approved": true,
61+
"rejected": false
62+
}
63+
]
64+
}

0 commit comments

Comments
 (0)