Skip to content

Commit

Permalink
Merge pull request #1677 from materialsproject/dev
Browse files Browse the repository at this point in the history
client: use comma-separated list of projects
  • Loading branch information
tschaume authored Nov 28, 2023
2 parents 00a51a0 + bca4b46 commit c091faf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 15 additions & 4 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": projects}
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 c091faf

Please sign in to comment.