@@ -20,15 +20,15 @@ class APIClient:
20
20
21
21
Attributes
22
22
----------
23
- _address : str
23
+ api_address : str
24
24
default address is localhost, use hostname or ip address
25
- _port : int
25
+ api_port : int
26
26
default port is 8111
27
- _version : int
27
+ api_version : int
28
28
version of api used for endpoints
29
- _apikey : str
29
+ api_key : str
30
30
api_key if needed for most calls
31
- _timeout : int
31
+ timeout : int
32
32
time until Request timeout without getting any response
33
33
34
34
Methods
@@ -38,6 +38,7 @@ class APIClient:
38
38
39
39
"""
40
40
def __init__ (self ,
41
+ api_proto : str = 'http' ,
41
42
api_address : str = 'localhost' ,
42
43
api_port : int = 8111 ,
43
44
api_version : int = 3 ,
@@ -48,6 +49,8 @@ def __init__(self,
48
49
49
50
Parameters
50
51
----------
52
+ api_proto : str
53
+ protocol used with api call, default is http
51
54
api_address : str
52
55
address that will be used to call api
53
56
api_port : int
@@ -59,18 +62,21 @@ def __init__(self,
59
62
timeout: int
60
63
time until Request timeout without getting any response
61
64
"""
65
+ self .proto = api_proto
62
66
self .address = api_address
63
67
self .port = api_port
64
68
self .version = api_version
65
69
self .apikey = api_key
66
70
self .timeout = timeout
67
71
68
- def call (self , url : str = '/' , call_type : APIType = APIType .GET , query = dict () , data = {}, auth : bool = True ):
72
+ def call (self , proto : str = 'http' , url : str = '/' , call_type : APIType = APIType .GET , query : dict = {} , data : dict = {}, auth : bool = True ):
69
73
"""
70
74
call api based on given url, call_type, query, data and auth parameter while using base class attributes
71
75
72
76
Parameters
73
77
----------
78
+ proto: str
79
+ protocol used with api call, default is http
74
80
url : str
75
81
url that will be added to api address and api port to make full endpoint address to call
76
82
call_type : APIType
@@ -84,9 +90,9 @@ def call(self, url: str = '/', call_type: APIType = APIType.GET, query=dict(), d
84
90
"""
85
91
if query :
86
92
query = urlencode (query )
87
- url = f"{ self .address } :{ self .port } { url } ?{ query } &api-version={ self .version } "
93
+ url = f"{ self .proto } :// { self . address } :{ self .port } { url } ?{ query } &api-version={ self .version } "
88
94
else :
89
- url = f"{ self .address } :{ self .port } { url } ?api-version={ self .version } "
95
+ url = f"{ self .proto } :// { self . address } :{ self .port } { url } ?api-version={ self .version } "
90
96
print (f"{ url } " )
91
97
92
98
headers = {
@@ -104,6 +110,7 @@ def call(self, url: str = '/', call_type: APIType = APIType.GET, query=dict(), d
104
110
headers ['api-version' ] = '1.0'
105
111
106
112
# Api Calls
113
+ req = None
107
114
if call_type == APIType .GET :
108
115
print (f"Api.GET === { url } -- -H { headers } -D { data } " )
109
116
req = Request (url , data = data , headers = headers , method = "GET" )
@@ -131,29 +138,18 @@ def call(self, url: str = '/', call_type: APIType = APIType.GET, query=dict(), d
131
138
else :
132
139
print (f"Unknown === { url } " )
133
140
134
- response = urlopen (req , timeout = int (self .timeout ))
135
-
136
- if response .info ().get ('Content-Encoding' ) == 'gzip' :
137
- try :
138
- buf = BytesIO (response .read ())
139
- f = gzip .GzipFile (fileobj = buf )
140
- data = f .read ()
141
- except Exception as e :
142
- print (f"Failed to decompress === { e } " )
143
- else :
144
- data = response .read ()
145
- response .close ()
146
-
147
- return data
148
-
149
- def _responde_helper (response ):
150
- if response .code == 200 :
151
- print ('ok' )
152
- elif response .code == 400 :
153
- print ('bad request' )
154
- elif response .code == 401 :
155
- print ('unauthorized' )
156
- elif response .code == 500 :
157
- print ('shoko is buggy' )
158
- else :
159
- print ('not supported error' )
141
+ if req is not None :
142
+ response = urlopen (req , timeout = int (self .timeout ))
143
+
144
+ if response .info ().get ('Content-Encoding' ) == 'gzip' :
145
+ try :
146
+ buf = BytesIO (response .read ())
147
+ f = gzip .GzipFile (fileobj = buf )
148
+ data = f .read ()
149
+ except Exception as e :
150
+ print (f"Failed to decompress === { e } " )
151
+ else :
152
+ data = response .read ()
153
+ response .close ()
154
+ return data
155
+ return None
0 commit comments