joshuadunlop commited on
Commit
aa7a7b0
·
verified ·
1 Parent(s): ca7f122

Upload client.py

Browse files
Files changed (1) hide show
  1. client.py +34 -0
client.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from http.client import HTTPSConnection
2
+ from base64 import b64encode
3
+ from json import loads
4
+ from json import dumps
5
+
6
+ class RestClient:
7
+ domain = "api.dataforseo.com"
8
+
9
+ def __init__(self, username, password):
10
+ self.username = username
11
+ self.password = password
12
+
13
+ def request(self, path, method, data=None):
14
+ connection = HTTPSConnection(self.domain)
15
+ try:
16
+ base64_bytes = b64encode(
17
+ ("%s:%s" % (self.username, self.password)).encode("ascii")
18
+ ).decode("ascii")
19
+ headers = {'Authorization' : 'Basic %s' % base64_bytes, 'Content-Encoding' : 'gzip'}
20
+ connection.request(method, path, headers=headers, body=data)
21
+ response = connection.getresponse()
22
+ return loads(response.read().decode())
23
+ finally:
24
+ connection.close()
25
+
26
+ def get(self, path):
27
+ return self.request(path, 'GET')
28
+
29
+ def post(self, path, data):
30
+ if isinstance(data, str):
31
+ data_str = data
32
+ else:
33
+ data_str = dumps(data)
34
+ return self.request(path, 'POST', data_str)