2025-04-30 17:07:16 +03:00

56 lines
1.6 KiB
Python

from urllib.error import HTTPError
import jsonschema
import json
import requests
class Entity:
def __init__(self, config):
self.config = config
self.endpoint = config['PIAPI_URL']
self.token = config['PIAPI_TOKEN']
def raw_request(self, method, params=None, data=None, headers=None):
if data is None:
data = {}
if params is None:
params = {}
if headers is None:
headers = {}
headers['X-API-Key'] = self.token
try:
response = requests.request(method=method,
url="{url}".format(url=self.endpoint),
params=params,
json=data,
headers=headers
)
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
json_response = response.json()
return json_response
def validate_dict(self, dictionary, schema):
# schema = {
# 'type': 'object',
# 'properties': {
# 'key1': {'type': 'integer'},
# 'key2': {'type': 'string'}
# },
# 'required': ['key1', 'key2']
# }
try:
jsonschema.validate(dictionary, schema)
return True
except jsonschema.ValidationError as ex:
print(ex)
return False