Add support for query parameters

This commit is contained in:
Micke Nordin 2022-10-27 15:47:54 +02:00
parent ee215d2ebb
commit a344633bbb
Signed by untrusted user who does not match committer: micke
GPG key ID: 0DA0A7A5708FE257
2 changed files with 22 additions and 6 deletions

View file

@ -5,7 +5,7 @@ import getpass
import json import json
import os import os
import sys import sys
from collections.abc import Sequence import urllib.parse
from os import environ, mkdir from os import environ, mkdir
from os.path import isdir, isfile, join from os.path import isdir, isfile, join
from typing import Union from typing import Union
@ -19,7 +19,7 @@ from simplejson.errors import JSONDecodeError as SimplejsonJSONDecodeError
# Helper functions # Helper functions
def error(description: str, error: str) -> Sequence[dict]: def error(description: str, error: str) -> list[dict]:
response = [] response = []
reply = {} reply = {}
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406 # https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406
@ -49,7 +49,7 @@ def nested_out(input, tabs="") -> str:
return string return string
def output(response: Sequence[dict], jsonout: bool = False): def output(response: list[dict], jsonout: bool = False):
try: try:
if jsonout: if jsonout:
print(json.dumps(response)) print(json.dumps(response))
@ -128,6 +128,7 @@ def run_update(url: str, jsonout: bool, headers: dict):
# Set up the url # Set up the url
def setup_url( def setup_url(
baseurl: str, baseurl: str,
arguments: Union[None, list[str]],
data: Union[None, str], data: Union[None, str],
name: Union[None, str], name: Union[None, str],
rtype: Union[None, str], rtype: Union[None, str],
@ -147,6 +148,13 @@ def setup_url(
url += "/{}".format(data) url += "/{}".format(data)
if ttl and data and zone and name and rtype: if ttl and data and zone and name and rtype:
url += "/{}".format(ttl) url += "/{}".format(ttl)
if data and zone and name and rtype and arguments:
url += "?"
for arg in arguments:
if not url.endswith("?"):
url += "&"
key, value = arg.split("=")
url += key + "=" + urllib.parse.quote_plus(value)
if ttl and (not rtype or not name or not zone): if ttl and (not rtype or not name or not zone):
output( output(
@ -198,10 +206,17 @@ def main() -> int:
listcmd.add_argument("-z", "--zone") listcmd.add_argument("-z", "--zone")
updatecmd = subparsers.add_parser("update") updatecmd = subparsers.add_parser("update")
updatecmd.add_argument(
"-a",
"--argument",
nargs="*",
help="Specify key - value pairs to be updated: name=dns1.example.com.",
required=True,
)
updatecmd.add_argument("-d", "--data", required=True) updatecmd.add_argument("-d", "--data", required=True)
updatecmd.add_argument("-n", "--name", required=True) updatecmd.add_argument("-n", "--name", required=True)
updatecmd.add_argument("-r", "--rtype", required=True) updatecmd.add_argument("-r", "--rtype", required=True)
updatecmd.add_argument("-t", "--ttl", required=True) updatecmd.add_argument("-t", "--ttl")
updatecmd.add_argument("-z", "--zone", required=True) updatecmd.add_argument("-z", "--zone", required=True)
argcomplete.autocomplete(parser) argcomplete.autocomplete(parser)
@ -244,7 +259,8 @@ def main() -> int:
ttl = None ttl = None
if 'ttl' in args: if 'ttl' in args:
ttl = args.ttl ttl = args.ttl
url = setup_url(baseurl, args.data, args.name, args.rtype, ttl, args.zone) url = setup_url(baseurl, args.argument, args.data, args.name, args.rtype,
ttl, args.zone)
try: try:
if args.command == "add": if args.command == "add":
run_add(url, args.json, headers) run_add(url, args.json, headers)

View file

@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
setuptools.setup( setuptools.setup(
name="knotctl", name="knotctl",
version="0.0.1", version="0.0.2",
packages = setuptools.find_packages(), packages = setuptools.find_packages(),
author="Micke Nordin", author="Micke Nordin",
author_email="hej@mic.ke", author_email="hej@mic.ke",