Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
53 lines (46 loc) · 1.85 KB
/
Copy pathapi.py
File metadata and controls
53 lines (46 loc) · 1.85 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
import json
import requests
class Api(object):
def __init__(self, token, headers=None):
self.token = token
self.API_URL = 'https://api.byte.co/'
if headers:
self.headers = headers + {'Authorization': token}
else:
self.headers = {
'User-Agent': 'byte/0.2 (co.byte.video; build:145; '
'iOS 13.3.0) Alamofire/4.9.1',
'Authorization': token
}
@staticmethod
def check_response(response):
if not response:
raise ValueError('empty response from API, check your token')
def get(self, url, params=None, check_response=True):
response = requests.get(self.API_URL + url,
params,
headers=self.headers).text
if check_response:
self.check_response(response)
return json.loads(response)
def post(self, url, data=None, json_data=None, check_response=True):
response = requests.post(self.API_URL + url,
data,
json_data,
headers=self.headers).text
if check_response:
self.check_response(response)
return json.loads(response)
def put(self, url, data=None, check_response=True):
response = requests.put(self.API_URL + url,
data,
headers=self.headers).text
if check_response:
self.check_response(response)
return json.loads(response)
def delete(self, url, check_response=True):
response = requests.delete(self.API_URL + url,
headers=self.headers).text
if check_response:
self.check_response(response)
return json.loads(response)