From b698eba5c65a5a44e5ea217ab4acf67ad560dbff Mon Sep 17 00:00:00 2001 From: Celeo Date: Fri, 10 Nov 2023 16:58:16 -0800 Subject: [PATCH] Support query params in URL for GET requests --- preston/preston.py | 30 +++++++++++++++++++----------- pyproject.toml | 2 +- tests/test_preston.py | 34 +++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/preston/preston.py b/preston/preston.py index cf2a607..9291a40 100644 --- a/preston/preston.py +++ b/preston/preston.py @@ -1,4 +1,5 @@ import base64 +from dataclasses import dataclass import re import time from typing import Optional, Tuple, Any, Union @@ -253,7 +254,7 @@ def _get_path_for_op_id(self, id: str) -> Optional[str]: return path_key return None - def _insert_vars(self, path: str, data: dict) -> str: + def _insert_vars(self, path: str, data: dict) -> [str, dict]: """Inserts variables into the ESI URL path. Args: @@ -261,15 +262,16 @@ def _insert_vars(self, path: str, data: dict) -> str: data: data to insert into the URL Returns: - path with variables filled + tuple of the path with variables filled, and + and remaining, unused dict items """ data = data.copy() while True: match = re.search(self.VAR_REPLACE_REGEX, path) if not match: - return path + return path, data replace_from = match.group(0) - replace_with = str(data.get(match.group(1))) + replace_with = str(data.pop(match.group(1), "")) path = path.replace(replace_from, replace_with) def whoami(self) -> dict: @@ -303,13 +305,19 @@ def get_path(self, path: str, data: dict) -> Tuple[dict, dict]: Returns: ESI data """ - path = self._insert_vars(path, data) - path = self.BASE_URL + path - data = self.cache.check(path) - if data: - return data + var_insert = self._insert_vars(path, data) + path = var_insert[0] + target_url = self.BASE_URL + path + if len(var_insert[1]) > 0: + req = requests.models.PreparedRequest() + req.prepare_url(target_url, var_insert[1]) + target_url = req.url + + cached_data = self.cache.check(target_url) + if cached_data: + return cached_data self._try_refresh_access_token() - r = self.session.get(path) + r = self.session.get(target_url) self.cache.set(r) return r.json() @@ -347,7 +355,7 @@ def post_path( Returns: ESI data """ - path = self._insert_vars(path, path_data or {}) + path = self._insert_vars(path, path_data or {})[0] path = self.BASE_URL + path self._try_refresh_access_token() return self.session.post(path, json=post_data).json() diff --git a/pyproject.toml b/pyproject.toml index b951424..1d611be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "preston" -version = "4.2.0" +version = "4.3.0" description = "EVE ESI API access tool" authors = ["Celeo "] license = "MIT" diff --git a/tests/test_preston.py b/tests/test_preston.py index 61352b6..0a151d8 100644 --- a/tests/test_preston.py +++ b/tests/test_preston.py @@ -91,19 +91,23 @@ def test_update_access_token_header(sample): def test_insert_vars(empty): data = dict(foo="bar", bar="baz") - path = "/foo/bar" - p = empty._insert_vars(path, data) - assert p == path - path = "/foo/{bar}" - p = empty._insert_vars(path, data) - assert p == "/foo/baz" - path = "/{foo}/bar" - p = empty._insert_vars(path, data) - assert p == "/bar/bar" - path = "/{foo}/{bar}" - p = empty._insert_vars(path, data) - assert p == "/bar/baz" - - -def test_whoami_unauth(empty): + test_cases = [ + ["/foo/bar", "/foo/bar", data], + ["/foo/{bar}", "/foo/baz", {"foo": "bar"}], + ["/{foo}/bar", "/bar/bar", {"bar": "baz"}], + ["/{foo}/{bar}", "/bar/baz", {}], + ] + for case in test_cases: + res = empty._insert_vars(case[0], data) + assert res[0] == case[1] + assert res[1] == case[2] + + +def test_insert_vars_missing(empty): + res = empty._insert_vars("/foo/{bar}", {}) + assert res[0] == "/foo/" + assert res[1] == {} + + +def test_whoami_unauthorized(empty): assert empty.whoami() == {}