got PATCH method working. Needs complicated input.
This commit is contained in:
8
dsc.py
8
dsc.py
@ -1,8 +1,8 @@
|
||||
from __future__ import print_function
|
||||
import time
|
||||
# import time
|
||||
import powerdns_client
|
||||
from powerdns_client.rest import ApiException
|
||||
from pprint import pprint
|
||||
# from pprint import pprint
|
||||
from decouple import config
|
||||
|
||||
# Configure API key authorization: APIKeyHeader
|
||||
@ -25,3 +25,7 @@ try:
|
||||
# pprint(api_response)
|
||||
except ApiException as e:
|
||||
print("Exception when calling ConfigApi->get_config: %s\n" % e)
|
||||
|
||||
# må få skriving/endring til å fungere konsistent først...
|
||||
#try:
|
||||
# api_response = api_instance.
|
||||
|
@ -13,17 +13,39 @@ class PdnsRestAdapter:
|
||||
headers = {'X-API-key': self._api_key}
|
||||
response = requests.get(url=full_url, headers=headers)
|
||||
data_out = response.json()
|
||||
# uhm...
|
||||
# if response.status.code >= 200 and response.status.code <= 299:
|
||||
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
|
||||
# raise Exception(data_out['error']) # TODO: raise custom exception later
|
||||
|
||||
def post(self, endpoint: str, params: Dict = None, data: Dict = None):
|
||||
def post(self, endpoint: str, ep_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:
|
||||
response = requests.post(url=full_url, headers=headers, params=ep_params, json=data)
|
||||
if response.status_code >= 200 and response.status_code <= 299: # OK
|
||||
return
|
||||
raise Exception(data_out['message']) # TODO: raise custom exception later
|
||||
print("PdnsRestAdapter::post - error")
|
||||
data_out = response.json()
|
||||
raise Exception(data_out['error'])
|
||||
|
||||
def patch(self, endpoint: str, ep_params: Dict = None, data: Dict = None):
|
||||
full_url = self.url + endpoint
|
||||
headers = {'X-API-key': self._api_key}
|
||||
response = requests.patch(url=full_url, headers=headers, params=ep_params, json=data)
|
||||
if response.status_code >= 200 and response.status_code <= 299: # OK
|
||||
return
|
||||
if response.status_code < 500:
|
||||
data_out = response.json()
|
||||
else:
|
||||
data_out = {"error": f"{response.status_code}"}
|
||||
raise Exception(data_out['error'])
|
||||
|
||||
# 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 response
|
||||
# if response.status.code >= 200 and response.status.code <= 299:
|
||||
# return
|
||||
# raise Exception(data_out['message']) # TODO: raise custom exception later
|
||||
|
47
testapi.py
47
testapi.py
@ -2,14 +2,57 @@
|
||||
|
||||
# import requests # unused for now
|
||||
from pdnsapi import PdnsRestAdapter
|
||||
# import json
|
||||
import datetime
|
||||
|
||||
api = PdnsRestAdapter('localhost:8081/', 'pdns-supersecret')
|
||||
|
||||
# test1.nhn.no zone must exist for this to work
|
||||
output = api.get('servers/localhost/zones/test1.nhn.no')
|
||||
|
||||
print("GET zones/test1.nhn.no:")
|
||||
print(f"{output}")
|
||||
print()
|
||||
|
||||
# NOTE: doesn't work yet. Maybe wrong arguments?
|
||||
# api.post('servers/localhost/zones/test1.nhn.no/test.txt', {'type': 'A'}, {'type': 'A'})
|
||||
# NOTE: this one puts two records into powerdns
|
||||
input = {
|
||||
"rrsets": [
|
||||
{
|
||||
"name": "test.test1.nhn.no.",
|
||||
"type": "TXT",
|
||||
"ttl": 60,
|
||||
"changetype": "REPLACE",
|
||||
"records": [
|
||||
{
|
||||
"content": f'"tester tekst-record {datetime.datetime.now().strftime('%d.%m.%Y %H:%M')}"',
|
||||
"disabled": False,
|
||||
"name": "test.test1.nhn.no",
|
||||
"ttl": 60,
|
||||
"type": "TXT"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "testa.test1.nhn.no.",
|
||||
"type": "A",
|
||||
"ttl": 60,
|
||||
"changetype": "REPLACE",
|
||||
"records": [
|
||||
{
|
||||
"content": "127.0.0.1",
|
||||
"disabled": False,
|
||||
"name": "test.test1.nhn.no",
|
||||
"ttl": 60,
|
||||
"type": "A"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
api.patch('servers/localhost/zones/test1.nhn.no', {}, input)
|
||||
|
||||
print("PATCH doesn't have output per now, should it? If you can see this it worked.")
|
||||
|
||||
# copy of error message:
|
||||
# Traceback (most recent call last):
|
||||
|
Reference in New Issue
Block a user