Skip to content

Commit

Permalink
Support query params in URL for GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Nov 11, 2023
1 parent 7252835 commit b698eba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
30 changes: 19 additions & 11 deletions preston/preston.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
from dataclasses import dataclass
import re
import time
from typing import Optional, Tuple, Any, Union
Expand Down Expand Up @@ -253,23 +254,24 @@ 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:
path: raw ESI URL path
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:
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "preston"
version = "4.2.0"
version = "4.3.0"
description = "EVE ESI API access tool"
authors = ["Celeo <mattboulanger@fastmail.com>"]
license = "MIT"
Expand Down
34 changes: 19 additions & 15 deletions tests/test_preston.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() == {}

0 comments on commit b698eba

Please sign in to comment.