- xxx_JDK
- - __init__.py
- - base.py
- - test.py
is_pro = False
if is_pro:
xxx_HOST_URL = '127.0.0.1:8080'
URL_PREFIX = 'http://'
else:
xxx_HOST_URL = '127.0.0.1:8081'
URL_PREFIX = 'http://'
import json
from urllib.parse import urljoin
from requests import Request, Session
from xxx_JDK import URL_PREFIX, xxx_HOST_URL
class ExtendAPIBase(object):
"""
外部接口访问基础类
"""
_APP_KEY = None
_APP_SECRET = None
_HOST_URL = '{}{}'.format(URL_PREFIX, xxx_HOST_URL)
def __init__(self):
pass
def execute(self, uri, method, body=None):
req = self.build_request(uri, method, body)
prepped = req.prepare()
s = Session()
resp = s.send(prepped)
resp = self.build_response(resp)
return resp
def build_request(self, uri, method, body):
method = method.upper()
url = urljoin(self._HOST_URL, uri)
req = Request(method, url)
if body:
if req.method in ["POST", "PUT", "PATH"]:
req.json = body
else:
req.params = body
return req
def build_response(self, resp):
try:
return resp.json()
except (ValueError, json.JSONDecodeError) as e:
raise resp.raise_for_status()
from xxx_JDK.base import ExtendAPIBase
class xxxTest(ExtendAPIBase):
def __init__(self):
super(xxxTest, self).__init__()
def test_api(self, request_param):
"""
测试接口
"""
return self.execute(
'/api/v1/system/test/',
'GET',
request_param
)