start writing my own API wrapper
This commit is contained in:
5
pdnsapi/README.md
Normal file
5
pdnsapi/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# PowerDNS API wrapper library
|
||||
|
||||
Following [this tutorial](https://www.pretzellogix.net/2021/12/08/how-to-write-a-python3-sdk-library-module-for-a-json-rest-api/)
|
||||
|
||||
Michael Hinz, 2025-04-29
|
29
pdnsapi/__init__.py
Normal file
29
pdnsapi/__init__.py
Normal file
@ -0,0 +1,29 @@
|
||||
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
|
Reference in New Issue
Block a user