Skip to content

Commit

Permalink
client: use apikey in _expand_params but don't use for hashkey
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Nov 28, 2023
1 parent efa92d3 commit bca4b46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mpcontribs-client/mpcontribs/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
from pint.errors import DimensionalityError
from tempfile import gettempdir
from plotly.express._chart_types import line as line_chart
from cachetools import cached, LRUCache
from cachetools.keys import hashkey

RETRIES = 3
MAX_WORKERS = 3
Expand Down Expand Up @@ -680,7 +682,9 @@ def _load(protocol, host, headers_json, project, version):
projects = sorted(d["name"] for d in resp["data"])
projects_json = ujson.dumps(projects)
# expand regex-based query parameters for `data` columns
return _expand_params(protocol, host, version, projects_json)
spec = _expand_params(protocol, host, version, projects_json, apikey=headers["x-api-key"])
spec.http_client.session.headers.update(headers)
return spec


@functools.lru_cache(maxsize=1)
Expand Down Expand Up @@ -714,15 +718,22 @@ def _raw_specs(protocol, host, version):
return spec_dict


@functools.lru_cache(maxsize=100)
def _expand_params(protocol, host, version, projects_json):
@cached(
cache=LRUCache(maxsize=100),
key=lambda protocol, host, version, projects_json, **kwargs: hashkey(
protocol, host, version, projects_json
)
)
def _expand_params(protocol, host, version, projects_json, apikey=None):
columns = {"string": [], "number": []}
projects = ujson.loads(projects_json)
query = {"project__in": ",".join(projects)}
query["_fields"] = ["columns"]
url = f"{protocol}://{host}"
http_client = RequestsClient()
http_client.session.headers["Content-Type"] = "application/json"
if apikey:
http_client.session.headers["X-Api-Key"] = apikey
resp = http_client.session.get(f"{url}/projects/", params=query).json()

for proj in resp["data"]:
Expand Down
1 change: 1 addition & 0 deletions mpcontribs-client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def local_version(version):
"tqdm",
"ujson",
"semantic-version",
"cachetools",
],
extras_require={
"dev": [
Expand Down

0 comments on commit bca4b46

Please sign in to comment.