M
Size: a a a
M
M
IS
BK
M
BK
BK
class ApiClient:
def __init__(self, base_url):
self.base_url = base_url
@step
def get(self, path="/", params=None, headers=None):
url = f"{self.base_url}{path}"
res = requests.get(url=url, params=params, headers=headers)
allure.attach(json.dumps(res.json(), indent=4), 'RESPONSE')
return res
@step
def post(self, path="/", params=None, data=None, json=None, headers=None):
url = f"{self.base_url}{path}"
res = requests.post(url=url, params=params, data=data, json=json, headers=headers)
allure.attach(json.dumps(res.json(), indent=4), 'RESPONSE')
return res
@pytest.fixture
def api_v2():
return ApiClient(base_url=URI)
СС
class ApiClient:
def __init__(self, base_url):
self.base_url = base_url
@step
def get(self, path="/", params=None, headers=None):
url = f"{self.base_url}{path}"
res = requests.get(url=url, params=params, headers=headers)
allure.attach(json.dumps(res.json(), indent=4), 'RESPONSE')
return res
@step
def post(self, path="/", params=None, data=None, json=None, headers=None):
url = f"{self.base_url}{path}"
res = requests.post(url=url, params=params, data=data, json=json, headers=headers)
allure.attach(json.dumps(res.json(), indent=4), 'RESPONSE')
return res
@pytest.fixture
def api_v2():
return ApiClient(base_url=URI)
СС
BK
СС
IS
IS
from requests import Session, Response
from allure import attach, attachment_type
class BaseSession(Session):
def __init__(self, base_url: str = None, **kwargs):
super().__init__(**kwargs)
self._base_url = base_url
@property
def base_url(self):
return self._base_url
@base_url.setter
def base_url(self, val):
self._base_url = val
def request(self, method, url, **kwargs) -> Response:
url = f'{self.base_url}{url}'
headers = {'Accept': 'application/json'}
response = super().request(method, url, headers=headers, **kwargs)
msg = f'{method} {response.status_code} {response.reason}.\n' \
f'\nHeaders: {response.headers}' \
f'\nURL requested: {url}' \
f'\nParams {kwargs}' \
f'\nCookies: {response.cookies.get_dict()}'
try:
attach(
body=json.dumps(response.json(), indent=4, ensure_ascii=False).encode('utf8'),
name=f'API Response from {method} {url}. \nParams: {kwargs}',
attachment_type=attachment_type.JSON,
extension='json')
except json.JSONDecodeError:
logger.debug(f'Warning: Response has no json-body. \n{msg}', exc_info=True)
finally:
return response
s = BaseSession(base_url=<URL>)
s.get('/<method>', params=<params>, headers=<headers>)
s.post('/<method>, params=<params>, headers=<headers>)
СС
from requests import Session, Response
from allure import attach, attachment_type
class BaseSession(Session):
def __init__(self, base_url: str = None, **kwargs):
super().__init__(**kwargs)
self._base_url = base_url
@property
def base_url(self):
return self._base_url
@base_url.setter
def base_url(self, val):
self._base_url = val
def request(self, method, url, **kwargs) -> Response:
url = f'{self.base_url}{url}'
headers = {'Accept': 'application/json'}
response = super().request(method, url, headers=headers, **kwargs)
msg = f'{method} {response.status_code} {response.reason}.\n' \
f'\nHeaders: {response.headers}' \
f'\nURL requested: {url}' \
f'\nParams {kwargs}' \
f'\nCookies: {response.cookies.get_dict()}'
try:
attach(
body=json.dumps(response.json(), indent=4, ensure_ascii=False).encode('utf8'),
name=f'API Response from {method} {url}. \nParams: {kwargs}',
attachment_type=attachment_type.JSON,
extension='json')
except json.JSONDecodeError:
logger.debug(f'Warning: Response has no json-body. \n{msg}', exc_info=True)
finally:
return response
s = BaseSession(base_url=<URL>)
s.get('/<method>', params=<params>, headers=<headers>)
s.post('/<method>, params=<params>, headers=<headers>)
СС
def request(self, method, url, **kwargs)СС
IS
from requests import Session, Response
from allure import attach, attachment_type
class BaseSession(Session):
def __init__(self, base_url: str = None, **kwargs):
super().__init__(**kwargs)
self._base_url = base_url
@property
def base_url(self):
return self._base_url
@base_url.setter
def base_url(self, val):
self._base_url = val
def request(self, method, url, **kwargs) -> Response:
url = f'{self.base_url}{url}'
headers = {'Accept': 'application/json'}
response = super().request(method, url, headers=headers, **kwargs)
msg = f'{method} {response.status_code} {response.reason}.\n' \
f'\nHeaders: {response.headers}' \
f'\nURL requested: {url}' \
f'\nParams {kwargs}' \
f'\nCookies: {response.cookies.get_dict()}'
try:
attach(
body=json.dumps(response.json(), indent=4, ensure_ascii=False).encode('utf8'),
name=f'API Response from {method} {url}. \nParams: {kwargs}',
attachment_type=attachment_type.JSON,
extension='json')
except json.JSONDecodeError:
logger.debug(f'Warning: Response has no json-body. \n{msg}', exc_info=True)
finally:
return response
s = BaseSession(base_url=<URL>)
s.get('/<method>', params=<params>, headers=<headers>)
s.post('/<method>, params=<params>, headers=<headers>)
BK
СС
IS