start writing my own API wrapper

This commit is contained in:
Michael Hinz 2025-04-29 16:35:52 +02:00
parent 1b2cf55857
commit fe1077bfce
3 changed files with 73 additions and 0 deletions

5
pdnsapi/README.md Normal file
View 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
View 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

39
testapi.py Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
# import requests # unused for now
from pdnsapi import PdnsRestAdapter
api = PdnsRestAdapter('localhost:8081/', 'pdns-supersecret')
output = api.get('servers/localhost/zones/test1.nhn.no')
print(f"{output}")
# NOTE: doesn't work yet. Maybe wrong arguments?
# api.post('servers/localhost/zones/test1.nhn.no/test.txt', {'type': 'A'}, {'type': 'A'})
# copy of error message:
# Traceback (most recent call last):
# File "/home/michael/projects/pdns_dnssec/.venv/lib/python3.12/site-packages/requests/models.py", line 974, in json
# return complexjson.loads(self.text, **kwargs)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "/home/michael/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/json/__init__.py", line 346, in loads
# return _default_decoder.decode(s)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "/home/michael/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/json/decoder.py", line 338, in decode
# obj, end = self.raw_decode(s, idx=_w(s, 0).end())
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "/home/michael/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/json/decoder.py", line 356, in raw_decode
# raise JSONDecodeError("Expecting value", s, err.value) from None
# json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
#
# During handling of the above exception, another exception occurred:
#
# Traceback (most recent call last):
# File "/home/michael/projects/pdns_dnssec/testapi.py", line 11, in <module>
# api.post('servers/localhost/zones/test1.nhn.no/test.txt', {'type': 'A'}, {'type': 'A'})
# File "/home/michael/projects/pdns_dnssec/pdnsapi/__init__.py", line 25, in post
# data_out = response.json()
# ^^^^^^^^^^^^^^^
# File "/home/michael/projects/pdns_dnssec/.venv/lib/python3.12/site-packages/requests/models.py", line 978, in json
# raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
# requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)