/
amp_api.py
101 lines (92 loc) · 3.69 KB
/
amp_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
import json
class amp (object):
"""Class to define the AMP for endpoints console.
Attributes
Endpoint: amp api endpoint
id: client ID for amp
key: api key for amp
"""
def __init__(self, endpoint, client_id, key):
"""Return AMP Object whose attributes are endpoint, id and key."""
self.endpoint = endpoint
self.client_id = client_id
self.key = key
self.headers = {
'content-type': 'application/json',
'accept': 'application/json'
}
def get(self, url):
"""GET method for amp."""
try:
response = requests.get(
"https://{}{}".format(self.endpoint, url),
auth=(self.client_id, self.key),
headers=self.headers
)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return "Error: Unexpected response {}".format(response)
try:
return response.json()
except:
return "Error: Non JSON response {}".format(response.text)
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Error: {}".format(e)
def post(self, url, data):
"""POST method for amp."""
try:
response = requests.post(
"https://{}{}".format(self.endpoint, url),
data=json.dumps(data),
auth=(self.client_id, self.key),
headers=self.headers
)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return "Error: Unexpected response {}".format(response)
try:
return response.json()
except:
return "Error: Non JSON response {}".format(response.text)
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Error: {}".format(e)
def patch(self, url, data):
"""PATCH method for amp."""
try:
response = requests.patch(
"https://{}{}".format(self.endpoint, url),
data=json.dumps(data),
auth=(self.client_id, self.key),
headers=self.headers
)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return "Error: Unexpected response {}".format(response)
try:
return response.json()
except:
return "Error: Non JSON response {}".format(response.text)
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Error: {}".format(e)
def delete(self, url):
"""DEL method for amp."""
try:
response = requests.delete(
"https://{}{}".format(self.endpoint, url),
auth=(self.client_id, self.key),
headers=self.headers
)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return "Error: Unexpected response {}".format(response)
try:
return response.json()
except:
return "Error: Non JSON response {}".format(response.text)
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Error: {}".format(e)