Skip to content

Commit 014a2ca

Browse files
committedSep 10, 2021
config.json dummy
1 parent dc05244 commit 014a2ca

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed
 

‎api/common/__init__.py

+30-34
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class APIClient:
2020
2121
Attributes
2222
----------
23-
_address : str
23+
api_address : str
2424
default address is localhost, use hostname or ip address
25-
_port : int
25+
api_port : int
2626
default port is 8111
27-
_version : int
27+
api_version : int
2828
version of api used for endpoints
29-
_apikey : str
29+
api_key : str
3030
api_key if needed for most calls
31-
_timeout : int
31+
timeout : int
3232
time until Request timeout without getting any response
3333
3434
Methods
@@ -38,6 +38,7 @@ class APIClient:
3838
3939
"""
4040
def __init__(self,
41+
api_proto: str = 'http',
4142
api_address: str = 'localhost',
4243
api_port: int = 8111,
4344
api_version: int = 3,
@@ -48,6 +49,8 @@ def __init__(self,
4849
4950
Parameters
5051
----------
52+
api_proto : str
53+
protocol used with api call, default is http
5154
api_address : str
5255
address that will be used to call api
5356
api_port : int
@@ -59,18 +62,21 @@ def __init__(self,
5962
timeout: int
6063
time until Request timeout without getting any response
6164
"""
65+
self.proto = api_proto
6266
self.address = api_address
6367
self.port = api_port
6468
self.version = api_version
6569
self.apikey = api_key
6670
self.timeout = timeout
6771

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):
6973
"""
7074
call api based on given url, call_type, query, data and auth parameter while using base class attributes
7175
7276
Parameters
7377
----------
78+
proto: str
79+
protocol used with api call, default is http
7480
url : str
7581
url that will be added to api address and api port to make full endpoint address to call
7682
call_type : APIType
@@ -84,9 +90,9 @@ def call(self, url: str = '/', call_type: APIType = APIType.GET, query=dict(), d
8490
"""
8591
if query:
8692
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}"
8894
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}"
9096
print(f"{url}")
9197

9298
headers = {
@@ -104,6 +110,7 @@ def call(self, url: str = '/', call_type: APIType = APIType.GET, query=dict(), d
104110
headers['api-version'] = '1.0'
105111

106112
# Api Calls
113+
req = None
107114
if call_type == APIType.GET:
108115
print(f"Api.GET === {url} -- -H {headers} -D {data}")
109116
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
131138
else:
132139
print(f"Unknown === {url}")
133140

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

‎api/shoko/v2/api2.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
# BASED OF :8111/swagger/index.html?urls.primaryName=2.0
44

5-
from api.common import *
5+
try:
6+
from api.common import APIClient, APIType
7+
except:
8+
from ...common import APIClient, APIType
69
from api2models import *
710

811

912
address = "http://192.168.1.2"
10-
port = "8111"
13+
port = 8111
1114
version = 2
1215
# api related
1316
apikey = ''

‎api/shoko/v3/api3.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
# BASED OF :8111/swagger/index.html?urls.primaryName=3.0
44
# Asahara
55

6+
try:
7+
from api.common import APIClient, APIType
8+
except:
9+
from ...common import APIClient, APIType
610
from api3models import *
7-
from ...common import *
811

912

1013
address = "http://10.1.1.100"
11-
port = "8111"
14+
port = 8111
1215
version = 3
1316
# api related
1417
apikey = ''

‎config.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"_comment": "config file for using while testing py",
3+
"proto": "http",
4+
"address": "localhost",
5+
"port": 8111,
6+
"version": 2,
7+
"apikey": "",
8+
"timeout": 120
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.