1
|
|
|
import json |
2
|
|
|
import requests |
3
|
|
|
from enum import Enum |
4
|
|
|
|
5
|
|
|
# ======================================================= |
6
|
|
|
# Parameter enums |
7
|
|
|
# ======================================================= |
8
|
|
|
class BasicOptions(Enum): |
9
|
|
|
TRUE = "true" |
10
|
|
|
FALSE = "false" |
11
|
|
|
ANY = "any" |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class AssignedUserOptions(Enum): |
15
|
|
|
ME = "me" |
16
|
|
|
ANY = "any" |
17
|
|
|
NONE = "none" |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class AssignedTeamOptions(Enum): |
21
|
|
|
MINE = "mine" |
22
|
|
|
NONE = "none" |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class SortByOptions(Enum): |
26
|
|
|
LAST_ACTIVITY = "last_activity" |
27
|
|
|
CREATION_TIME = "creation_time" |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class UserRoles: |
31
|
|
|
ADMIN = ("admin", 20) |
32
|
|
|
AGENT = ("agent", 10) |
33
|
|
|
COLLABORATOR = ("collaborator", 9) |
34
|
|
|
CUSTOMER = ("customer", None) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class DataPointType(Enum): |
38
|
|
|
AVG_FIRST_RESPONSE_TIME = "avg_first_response_time" |
39
|
|
|
TICKETS_COUNT = "tickets_count" |
40
|
|
|
REPLIES_COUNT = "replies_count" |
41
|
|
|
|
42
|
|
|
# ======================================================= |
43
|
|
|
# Resource - Base class with default requests |
44
|
|
|
# ======================================================= |
45
|
|
|
class Resource: |
46
|
|
|
def __init__(self, api): |
47
|
|
|
self.api = api |
48
|
|
|
|
49
|
|
|
def _prepare_url(self, endpoint, **kwargs): |
50
|
|
|
url = self.api.BASE_URL.format(url=endpoint) |
51
|
|
|
for key, value in kwargs.items(): |
52
|
|
|
if value is not None: |
53
|
|
|
url += "&" + key + "=" + str(value) |
54
|
|
|
return url |
55
|
|
|
|
56
|
|
|
def _get_headers(self): |
57
|
|
|
return { |
58
|
|
|
"Content-Type": "application/json", |
59
|
|
|
"Accept": "application/json" |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
def _get(self, endpoint, **kwargs): |
63
|
|
|
url = self._prepare_url(endpoint, **kwargs) |
64
|
|
|
print("GET", url) |
65
|
|
|
return requests.get(url, headers=self._get_headers()).json() |
66
|
|
|
|
67
|
|
|
def _post(self, endpoint, data=None, files=None, **kwargs): |
68
|
|
|
url = self._prepare_url(endpoint, **kwargs) |
69
|
|
|
print("POST", url) |
70
|
|
|
headers = self._get_headers() |
71
|
|
|
if files is not None: |
72
|
|
|
headers.pop("Content-Type") |
73
|
|
|
response = requests.post(url, headers=headers, json=data, files=files) |
74
|
|
|
# print(response) |
75
|
|
|
# print(response.text) |
76
|
|
|
try: |
77
|
|
|
return response.json() |
78
|
|
|
except json.decoder.JSONDecodeError: |
79
|
|
|
return response.text |
80
|
|
|
|
81
|
|
|
def _put(self, endpoint, data=None, **kwargs): |
82
|
|
|
url = self._prepare_url(endpoint, **kwargs) |
83
|
|
|
print("PUT", url) |
84
|
|
|
return requests.put(url, headers=self._get_headers(), json=data).json() |
85
|
|
|
|
86
|
|
|
def _delete(self, endpoint, **kwargs): |
87
|
|
|
url = self._prepare_url(endpoint, **kwargs) |
88
|
|
|
print("DELETE", url) |
89
|
|
|
return requests.delete(url, headers=self._get_headers()).json() |
90
|
|
|
|