Skip to content

Commit e05a296

Browse files
type annotations for server resters
1 parent 1fe36cf commit e05a296

1 file changed

Lines changed: 39 additions & 34 deletions

File tree

mp_api/client/routes/_server.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
from emmet.core._general_store import GeneralStoreDoc
88
from emmet.core._messages import MessagesDoc, MessageType
9-
from emmet.core._user_settings import UserSettingsDoc
9+
from emmet.core._user_settings import UserSettings, UserSettingsDoc
1010

1111
from mp_api.client.core import BaseRester
1212

1313
if TYPE_CHECKING:
1414
from datetime import datetime
15+
from typing import Any
1516

1617

1718
class GeneralStoreRester(BaseRester): # pragma: no cover
@@ -133,7 +134,9 @@ class UserSettingsRester(BaseRester): # pragma: no cover
133134
primary_key = "consumer_id"
134135
use_document_model = False
135136

136-
def create_user_settings(self, consumer_id, settings):
137+
def create_user_settings(
138+
self, consumer_id: str, settings: dict[str, Any]
139+
) -> dict[str, Any]:
137140
"""Create user settings.
138141
139142
Args:
@@ -147,48 +150,49 @@ def create_user_settings(self, consumer_id, settings):
147150
body=settings, params={"consumer_id": consumer_id}
148151
).get("data")
149152

150-
def patch_user_settings(self, consumer_id, settings): # pragma: no cover
153+
def patch_user_settings(
154+
self, consumer_id: str, settings: dict[str, Any]
155+
) -> UserSettingsDoc:
151156
"""Patch user settings.
152157
153158
Args:
154-
consumer_id: Consumer ID for the user
159+
consumer_id (str): Consumer ID for the user
155160
settings: Dictionary with user settings
156161
Returns:
157-
Dictionary with consumer_id and write status.
162+
UserSettingsDoc with consumer_id and write status.
158163
159164
160165
Raises:
161166
MPRestError.
162167
"""
163-
body = dict()
164-
valid_fields = [
165-
"institution",
166-
"sector",
167-
"job_role",
168-
"is_email_subscribed",
169-
"agreed_terms",
170-
"message_last_read",
171-
]
172-
for key in settings:
173-
if key not in valid_fields:
174-
raise ValueError(
175-
f"Invalid setting key {key}. Must be one of {valid_fields}"
176-
)
177-
body[f"settings.{key}"] = settings[key]
178-
179-
return self._patch_resource(body=body, params={"consumer_id": consumer_id}).get(
180-
"data"
181-
)
168+
if (
169+
len(
170+
invalid_keys := [
171+
key for key in settings if key not in UserSettings.model_fields
172+
]
173+
)
174+
> 0
175+
):
176+
raise ValueError(
177+
f"Invalid setting key(s): {', '.join(invalid_keys)}. "
178+
f"Valid keys: {', '.join(UserSettings.model_fields)}"
179+
)
180+
181+
return self._patch_resource(
182+
body={f"settings.{key}": v for key, v in settings.items()},
183+
params={"consumer_id": consumer_id},
184+
).get("data")
182185

183-
def patch_user_time_settings(self, consumer_id, time): # pragma: no cover
186+
def patch_user_time_settings(
187+
self, consumer_id: str, time: datetime
188+
) -> UserSettingsDoc:
184189
"""Set user settings last_read_message field.
185190
186191
Args:
187-
consumer_id: Consumer ID for the user
188-
time: utc datetime object for when the user last see messages
192+
consumer_id (str): Consumer ID for the user
193+
time (datetime): UTC datetime object for when the user last see messages
189194
Returns:
190-
Dictionary with consumer_id and write status.
191-
195+
UserSettingsDoc
192196
193197
Raises:
194198
MPRestError.
@@ -198,15 +202,16 @@ def patch_user_time_settings(self, consumer_id, time): # pragma: no cover
198202
params={"consumer_id": consumer_id},
199203
).get("data")
200204

201-
def get_user_settings(self, consumer_id, fields): # pragma: no cover
205+
def get_user_settings(
206+
self, consumer_id: str, fields: list[str]
207+
) -> list[UserSettingsDoc]:
202208
"""Get user settings.
203209
204210
Args:
205-
consumer_id: Consumer ID for the user
206-
fields: List of fields to project
211+
consumer_id (str): Consumer ID for the user
212+
fields (list of str): List of fields to project
207213
Returns:
208-
Dictionary with consumer_id and settings.
209-
214+
list of UserSettingsDoc, with consumer_id and settings.
210215
211216
Raises:
212217
MPRestError.

0 commit comments

Comments
 (0)