30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
import requests
|
|
import requests.packages
|
|
from typing import List, Dict
|
|
|
|
|
|
class PdnsRestAdapter:
|
|
def __init__(self, hostname: str, api_key: str = '', ver: str = 'v1'):
|
|
self.url = f"http://{hostname}/api/{ver}/"
|
|
self._api_key = api_key
|
|
|
|
def get(self, endpoint: str) -> List[Dict]:
|
|
full_url = self.url + endpoint
|
|
headers = {'X-API-key': self._api_key}
|
|
response = requests.get(url=full_url, headers=headers)
|
|
data_out = response.json()
|
|
return data_out
|
|
if response.status.code >= 200 and response.status.code <= 299:
|
|
return data_out
|
|
raise Exception(data_out['message']) # TODO: raise custom exception later
|
|
|
|
def post(self, endpoint: str, params: Dict = None, data: Dict = None):
|
|
full_url = self.url + endpoint
|
|
headers = {'X-API-key': self._api_key}
|
|
response = requests.post(url=full_url, params=params, headers=headers, json=data)
|
|
data_out = response.json()
|
|
return data_out
|
|
if response.status.code >= 200 and response.status.code <= 299:
|
|
return
|
|
raise Exception(data_out['message']) # TODO: raise custom exception later
|