From 063a78cd6833b325832b8685707585548c107b50 Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:18:32 -0500 Subject: [PATCH 1/6] allow overriding route defaults on call --- src/kinpy/routes.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index 8970ded..06856f4 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -55,24 +55,27 @@ class SyncRoute(Route): >>> routes = [SyncRoute('GET', '/k/v1/app.json', handler, params={'id': i}) for i in range(1, 11)] >>> responses = [route() for route in routes] # Run requests synchronously """ - def __call__(self) -> Response: + def __call__(self, **call_options) -> Response: + route_options = self.route_options.copy() + call_options = route_options.update(call_options) + if not isinstance(self.handler, HTTPX_Sync): raise AttributeError("Sync Routing requires a Sync Handler") if self.method == 'GET': - return self.handler.get(self.url, **self.opts) + return self.handler.get(self.url, **call_options) elif self.method == 'POST': - return self.handler.post(self.url, **self.opts) + return self.handler.post(self.url, **call_options) elif self.method == 'PATCH': - return self.handler.patch(self.url, **self.opts) + return self.handler.patch(self.url, **call_options) elif self.method == 'PUT': - return self.handler.put(self.url, **self.opts) + return self.handler.put(self.url, **call_options) elif self.method == 'DELETE': - return self.handler.delete(self.url, **self.opts) + return self.handler.delete(self.url, **call_options) return None class AsyncRoute(Route): @@ -98,24 +101,27 @@ class AsyncRoute(Route): >>> routes = [AsyncRoute('GET', '/k/v1/app.json', handler, params={'id': i}) for i in range(1, 11)] >>> responses = [await route() for route in routes] # Run requests concurrently """ - async def __call__(self) -> Coroutine[Any, Any, Response]: + async def __call__(self, **call_options) -> Coroutine[Any, Any, Response]: + route_options = self.route_options.copy() + call_options = route_options.update(call_options) + if not isinstance(self.handler, HTTPX_Async): raise AttributeError("Async Routing requires an Async Handler") if self.method == 'GET': - return await self.handler.get(self.url, **self.opts) + return await self.handler.get(self.url, **call_options) elif self.method == 'POST': - return await self.handler.post(self.url, **self.opts) + return await self.handler.post(self.url, **call_options) elif self.method == 'PATCH': - return await self.handler.patch(self.url, **self.opts) + return await self.handler.patch(self.url, **call_options) elif self.method == 'PUT': - return await self.handler.put(self.url, **self.opts) + return await self.handler.put(self.url, **call_options) elif self.method == 'DELETE': - return await self.handler.delete(self.url, **self.opts) + return await self.handler.delete(self.url, **call_options) return None class Routes: From 6e62c507ad161e46d35aa591730c69b7c2c75387 Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:19:05 -0500 Subject: [PATCH 2/6] opts is nou `route_opts` --- src/kinpy/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index 06856f4..59506fc 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -20,7 +20,7 @@ def __init__(self, method: RequestType, endpoint: str, handler: HTTPX_Async | HT self.handler = handler self.method = method self.endpoint = endpoint - self.opts = opts + self.route_options = opts @property def url(self): From b82783b47cfcbce2a1d154b6237c3a479d61f936 Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:19:32 -0500 Subject: [PATCH 3/6] update docs and variadic name --- src/kinpy/routes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index 59506fc..0b2da8b 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -151,8 +151,8 @@ def __init__(self, handler: HTTPX_Async | HTTPX_Sync) -> None: def register_route(method: Route.RequestType, endpoint: str, *, required: list[str] = None, optional: list[str] = None, - json_content: bool = False, - **opts) -> Route: + json_request: bool = False, + **overrides) -> Route: """Define a route using a function header and type hints Args: @@ -160,7 +160,8 @@ def register_route(method: Route.RequestType, endpoint: str, *, endpoint: The api endpoint minus the base url of the handler required: Required parameter keys optional: Optional parameter keys - opts: Optional parameters to pass to the Handler request method + json_request: If True, send the request parameters as a JSON body, rather than query string + **overrides: Optional parameters to pass to the Handler request method Raises: ValueError: If required parameters are not passed From 6e2bb140a74ff4857ab06e5ea6ddc1cd9fb7fe28 Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:20:32 -0500 Subject: [PATCH 4/6] json_request flag now just changes which mapped parameter in overrides gets the parameters --- src/kinpy/routes.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index 0b2da8b..ec76416 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -239,14 +239,16 @@ def _wrapped(self, *args, **kwargs): f"Expected {param} to be of type {_wrapped.__annotations__[param]}" ) - # TODO: Come up with a more elegant solution here; get requests to not accept a json body, but some put requests require it. - if isinstance(self.handler, HTTPX_Sync) and json_content: - return SyncRoute(method, endpoint, self.handler, json=params, **opts) - elif isinstance(self.handler, HTTPX_Sync): - return SyncRoute(method, endpoint, self.handler, params=params, **opts) + if json_request: + overrides['json'] = params + else: + overrides['params'] = params + + if isinstance(self.handler, HTTPX_Sync) and json_request: + return SyncRoute(method, endpoint, self.handler, **overrides) if isinstance(self.handler, HTTPX_Async): - return AsyncRoute(method, endpoint, self.handler, params=params, **opts) + return AsyncRoute(method, endpoint, self.handler, **overrides) raise AttributeError("Invalid Handler type, must be `HTTPX_Sync` or `HTTPX_Async`") From 72112872d5aea6637cefab4d579ce677168a2fbc Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:20:48 -0500 Subject: [PATCH 5/6] update PUT route with new flag name --- src/kinpy/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index ec76416..1c9cf57 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -328,7 +328,7 @@ def add_records(self, app: int | str, record: str) -> Route: """ ... - @register_route('PUT', '/k/v1/record.json', required=[], optional=['app', 'record', 'id', 'updateKey', 'revision'], json_content=True) + @register_route('PUT', '/k/v1/record.json', required=[], optional=['app', 'record', 'id', 'updateKey', 'revision'], json_request=True) def update_record(self, app: str | int, record: dict, id: str | int, updateKey, revision) -> Route: """Updates specified record within app database Args: From 65766b9e3aba7d0827164f4564be68c9765f1642 Mon Sep 17 00:00:00 2001 From: Hayden Welch Date: Sun, 2 Mar 2025 13:20:57 -0500 Subject: [PATCH 6/6] remove unused import --- src/kinpy/routes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/kinpy/routes.py b/src/kinpy/routes.py index 1c9cf57..d67e174 100644 --- a/src/kinpy/routes.py +++ b/src/kinpy/routes.py @@ -8,8 +8,6 @@ from functools import wraps from httpx import Response -import json - from handlers import HTTPX_Async, HTTPX_Sync from utils import QueryString