Skip to content

Commit

Permalink
Default API key value to None in MPRester (#743)
Browse files Browse the repository at this point in the history
* api_key default to None

* Fix default key in base rester

* Update mpcontribs client in reqs

* Fix pytest stripping api_key

* Fix api_key setting in session

* Fix alloy key

* Fix alloys sort fields

* Remove print statements

* Linting
  • Loading branch information
Jason Munro authored Feb 14, 2023
1 parent 2ec98bd commit 858409f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
12 changes: 8 additions & 4 deletions mp_api/client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BaseRester(Generic[T]):

def __init__(
self,
api_key: Union[str, None] = DEFAULT_API_KEY,
api_key: Union[str, None] = None,
endpoint: str = DEFAULT_ENDPOINT,
include_user_agent: bool = True,
session: Optional[requests.Session] = None,
Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(
headers (dict): Custom headers for localhost connections.
"""

self.api_key = api_key
self.api_key = api_key or DEFAULT_API_KEY
self.base_endpoint = endpoint
self.endpoint = endpoint
self.debug = debug
Expand Down Expand Up @@ -845,8 +845,12 @@ def get_data_by_id(
from mp_api.client.routes.materials import MaterialsRester

with MaterialsRester(
api_key=self.api_key, endpoint=self.base_endpoint, use_document_model=False, monty_decode=False,
session=self.session, headers=self.headers
api_key=self.api_key,
endpoint=self.base_endpoint,
use_document_model=False,
monty_decode=False,
session=self.session,
headers=self.headers,
) as mpr:
docs = mpr.search(task_ids=[document_id], fields=["material_id"])

Expand Down
6 changes: 3 additions & 3 deletions mp_api/client/mprester.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MPRester:

def __init__(
self,
api_key: Optional[str] = DEFAULT_API_KEY,
api_key: Optional[str] = None,
endpoint: str = DEFAULT_ENDPOINT,
notify_db_version: bool = False,
include_user_agent: bool = True,
Expand Down Expand Up @@ -128,11 +128,11 @@ def __init__(
"API are 16 characters."
)

self.api_key = api_key
self.api_key = api_key or DEFAULT_API_KEY
self.endpoint = endpoint
self.headers = headers or {}
self.session = session or BaseRester._create_session(
api_key=api_key, include_user_agent=include_user_agent, headers=self.headers
api_key=self.api_key, include_user_agent=include_user_agent, headers=self.headers
)
self.use_document_model = use_document_model
self.monty_decode = monty_decode
Expand Down
11 changes: 5 additions & 6 deletions mp_api/client/routes/alloys.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AlloysRester(BaseRester[AlloyPairDoc]):

suffix = "alloys"
document_model = AlloyPairDoc # type: ignore
primary_key = "material_id"
primary_key = "pair_id"

def search(
self,
Expand Down Expand Up @@ -45,18 +45,17 @@ def search(

query_params = defaultdict(dict) # type: dict

query_params = {
entry: query_params[entry]
for entry in query_params
if query_params[entry] is not None
}
query_params = {entry: query_params[entry] for entry in query_params if query_params[entry] is not None}

if material_ids:
if isinstance(material_ids, str):
material_ids = [material_ids]

query_params.update({"material_ids": ",".join(validate_ids(material_ids))})

if sort_fields:
query_params.update({"_sort_fields": ",".join([s.strip() for s in sort_fields])})

return super()._search(
formulae=formulae,
num_chunks=num_chunks,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pymatgen-analysis-alloys>=0.0.3
typing-extensions==4.1.1
requests==2.27.1
monty==2022.3.12
mpcontribs-client>=4.2.9
mpcontribs-client>=5.0.10
custodian
boto3
msgpack
24 changes: 8 additions & 16 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,36 @@
ignore_generic = [
"_user_settings",
"_general_store",
"tasks",
"bonds",
# "tasks",
# "bonds",
"xas",
"elasticity",
"fermi",
"alloys",
"summary",
# "alloys",
# "summary",
] # temp


mpr = MPRester()


@pytest.mark.skipif(
os.environ.get("MP_API_KEY", None) is None, reason="No API key found."
)
@pytest.mark.skipif(os.environ.get("MP_API_KEY", None) is None, reason="No API key found.")
@pytest.mark.parametrize("rester", mpr._all_resters)
def test_generic_get_methods(rester):

# -- Test generic search and get_data_by_id methods
name = rester.suffix.replace("/", "_")
if name not in ignore_generic:
if name not in key_only_resters:
doc = rester._query_resource_data(
{"_limit": 1}, fields=[rester.primary_key]
)[0]
doc = rester._query_resource_data({"_limit": 1}, fields=[rester.primary_key])[0]
assert isinstance(doc, rester.document_model)

if name not in search_only_resters:
doc = rester.get_data_by_id(
doc.dict()[rester.primary_key], fields=[rester.primary_key]
)
doc = rester.get_data_by_id(doc.dict()[rester.primary_key], fields=[rester.primary_key])
assert isinstance(doc, rester.document_model)

elif name not in special_resters:
doc = rester.get_data_by_id(
key_only_resters[name], fields=[rester.primary_key]
)
doc = rester.get_data_by_id(key_only_resters[name], fields=[rester.primary_key])
assert isinstance(doc, rester.document_model)


Expand Down
2 changes: 0 additions & 2 deletions tests/test_oxidation_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def test_client(rester):
"num_chunks": 1,
}

print(q)

doc = search_method(**q)[0].dict()
for sub_field in sub_doc_fields:
if sub_field in doc:
Expand Down
1 change: 0 additions & 1 deletion tests/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def test_client(rester):
# Query API for each numeric and boolean parameter and check if returned
for entry in param_tuples:
param = entry[0]
print(param)
if param not in excluded_params:
param_type = entry[1].__args__[0]
q = None
Expand Down

0 comments on commit 858409f

Please sign in to comment.