Add support for query parameters

pull/7/head
Micke Nordin 2 years ago
parent ee215d2ebb
commit a344633bbb
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -5,7 +5,7 @@ import getpass
import json
import os
import sys
from collections.abc import Sequence
import urllib.parse
from os import environ, mkdir
from os.path import isdir, isfile, join
from typing import Union
@ -19,7 +19,7 @@ from simplejson.errors import JSONDecodeError as SimplejsonJSONDecodeError
# Helper functions
def error(description: str, error: str) -> Sequence[dict]:
def error(description: str, error: str) -> list[dict]:
response = []
reply = {}
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406
@ -49,7 +49,7 @@ def nested_out(input, tabs="") -> str:
return string
def output(response: Sequence[dict], jsonout: bool = False):
def output(response: list[dict], jsonout: bool = False):
try:
if jsonout:
print(json.dumps(response))
@ -128,6 +128,7 @@ def run_update(url: str, jsonout: bool, headers: dict):
# Set up the url
def setup_url(
baseurl: str,
arguments: Union[None, list[str]],
data: Union[None, str],
name: Union[None, str],
rtype: Union[None, str],
@ -147,6 +148,13 @@ def setup_url(
url += "/{}".format(data)
if ttl and data and zone and name and rtype:
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):
output(
@ -198,10 +206,17 @@ def main() -> int:
listcmd.add_argument("-z", "--zone")
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("-n", "--name", 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)
argcomplete.autocomplete(parser)
@ -244,7 +259,8 @@ def main() -> int:
ttl = None
if 'ttl' in args:
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:
if args.command == "add":
run_add(url, args.json, headers)

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

Loading…
Cancel
Save