-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiHandler.py
More file actions
63 lines (55 loc) · 2.5 KB
/
ApiHandler.py
File metadata and controls
63 lines (55 loc) · 2.5 KB
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
import urllib.request
import json
"""
Class ApiHandler
This class acts as a middleware between the client and
the provided API JSONplaceholder.
A client can get, post, and delete posts, albums, and comments from/to the API.
"""
class ApiHandler:
def __init__(self):
self.__url = 'https://jsonplaceholder.typicode.com/'
def get(self, route, id):
try:
# Forming the complete url for request
url = self.__url+route+'/'+str(id)
with urllib.request.urlopen(url) as response:
html = response.read()
data = str(html, "utf-8") # Converting data to string
return json.loads(data)
except urllib.request.URLError as url_Error:
return {"title": "N/A"}
except urllib.request.HTTPError as http_Error:
return {"title": "N/A"}
def post(self, route, values):
try:
# Converting dictionary data to string
data = urllib.parse.urlencode(values)
# Converting string data to bytes
data = data.encode('utf-8')
req = urllib.request.Request(self.__url+route, data)
with urllib.request.urlopen(req) as response:
html = response.read()
json1 = json.loads(str(html, "utf-8"))
info = response.info()
# Searching for the "X-Powered-By" headers in response headers
for header in info._headers:
if(header[0] == "X-Powered-By"):
return (json1["id"], response.getcode(), header[1],)
except urllib.request.URLError as url_Error:
return ("Error", url_Error.reason,)
except urllib.request.HTTPError as http_Error:
return ("Error", http_Error.code,)
def delete(self, route, id):
try:
req = urllib.request.Request(self.__url+route+'/'+str(id))
req.get_method = lambda: "DELETE"
with urllib.request.urlopen(req) as response:
for header in response.info()._headers:
# Searching for the "X-Content-Type-Options" header
if(header[0] == 'X-Content-Type-Options'):
return (response.getcode(), header[1],)
except urllib.request.URLError as url_Error:
return ("Error", url_Error.reason,)
except urllib.request.HTTPError as http_Error:
return ("Error", http_Error.code,)