|
12 | 12 | from json.decoder import JSONDecodeError |
13 | 13 | from ..core.api_error import ApiError |
14 | 14 | from ..core.jsonable_encoder import jsonable_encoder |
15 | | -from ..types.metric_response import MetricResponse |
16 | 15 | from ..errors.not_found_error import NotFoundError |
| 16 | +from ..types.metric_response import MetricResponse |
17 | 17 | from .types.users_metric_event_summary_request_aggregation import ( |
18 | 18 | UsersMetricEventSummaryRequestAggregation, |
19 | 19 | ) |
@@ -224,6 +224,113 @@ def get( |
224 | 224 | raise ApiError(status_code=_response.status_code, body=_response.text) |
225 | 225 | raise ApiError(status_code=_response.status_code, body=_response_json) |
226 | 226 |
|
| 227 | + def upsert( |
| 228 | + self, |
| 229 | + id: str, |
| 230 | + *, |
| 231 | + email: typing.Optional[str] = OMIT, |
| 232 | + name: typing.Optional[str] = OMIT, |
| 233 | + tz: typing.Optional[str] = OMIT, |
| 234 | + subscribe_to_emails: typing.Optional[bool] = OMIT, |
| 235 | + request_options: typing.Optional[RequestOptions] = None, |
| 236 | + ) -> User: |
| 237 | + """ |
| 238 | + Upsert a user (create or update). |
| 239 | +
|
| 240 | + Parameters |
| 241 | + ---------- |
| 242 | + id : str |
| 243 | + ID of the user to upsert. |
| 244 | +
|
| 245 | + email : typing.Optional[str] |
| 246 | + The user's email address. Required if subscribeToEmails is true. |
| 247 | +
|
| 248 | + name : typing.Optional[str] |
| 249 | + The name to refer to the user by in emails. |
| 250 | +
|
| 251 | + tz : typing.Optional[str] |
| 252 | + The user's timezone (used for email scheduling). |
| 253 | +
|
| 254 | + subscribe_to_emails : typing.Optional[bool] |
| 255 | + Whether the user should receive Trophy-powered emails. Cannot be false if an email is provided. |
| 256 | +
|
| 257 | + request_options : typing.Optional[RequestOptions] |
| 258 | + Request-specific configuration. |
| 259 | +
|
| 260 | + Returns |
| 261 | + ------- |
| 262 | + User |
| 263 | + Upserted user |
| 264 | +
|
| 265 | + Examples |
| 266 | + -------- |
| 267 | + from trophy import TrophyApi |
| 268 | +
|
| 269 | + client = TrophyApi( |
| 270 | + api_key="YOUR_API_KEY", |
| 271 | + ) |
| 272 | + client.users.upsert( |
| 273 | + id="id", |
| 274 | + email="user@example.com", |
| 275 | + tz="Europe/London", |
| 276 | + ) |
| 277 | + """ |
| 278 | + _response = self._client_wrapper.httpx_client.request( |
| 279 | + f"users/{jsonable_encoder(id)}", |
| 280 | + method="PUT", |
| 281 | + json={ |
| 282 | + "email": email, |
| 283 | + "name": name, |
| 284 | + "tz": tz, |
| 285 | + "subscribeToEmails": subscribe_to_emails, |
| 286 | + }, |
| 287 | + request_options=request_options, |
| 288 | + omit=OMIT, |
| 289 | + ) |
| 290 | + try: |
| 291 | + if 200 <= _response.status_code < 300: |
| 292 | + return typing.cast( |
| 293 | + User, |
| 294 | + parse_obj_as( |
| 295 | + type_=User, # type: ignore |
| 296 | + object_=_response.json(), |
| 297 | + ), |
| 298 | + ) |
| 299 | + if _response.status_code == 400: |
| 300 | + raise BadRequestError( |
| 301 | + typing.cast( |
| 302 | + ErrorBody, |
| 303 | + parse_obj_as( |
| 304 | + type_=ErrorBody, # type: ignore |
| 305 | + object_=_response.json(), |
| 306 | + ), |
| 307 | + ) |
| 308 | + ) |
| 309 | + if _response.status_code == 401: |
| 310 | + raise UnauthorizedError( |
| 311 | + typing.cast( |
| 312 | + ErrorBody, |
| 313 | + parse_obj_as( |
| 314 | + type_=ErrorBody, # type: ignore |
| 315 | + object_=_response.json(), |
| 316 | + ), |
| 317 | + ) |
| 318 | + ) |
| 319 | + if _response.status_code == 422: |
| 320 | + raise UnprocessableEntityError( |
| 321 | + typing.cast( |
| 322 | + ErrorBody, |
| 323 | + parse_obj_as( |
| 324 | + type_=ErrorBody, # type: ignore |
| 325 | + object_=_response.json(), |
| 326 | + ), |
| 327 | + ) |
| 328 | + ) |
| 329 | + _response_json = _response.json() |
| 330 | + except JSONDecodeError: |
| 331 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 332 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
| 333 | + |
227 | 334 | def update( |
228 | 335 | self, |
229 | 336 | id: str, |
@@ -316,6 +423,16 @@ def update( |
316 | 423 | ), |
317 | 424 | ) |
318 | 425 | ) |
| 426 | + if _response.status_code == 404: |
| 427 | + raise NotFoundError( |
| 428 | + typing.cast( |
| 429 | + ErrorBody, |
| 430 | + parse_obj_as( |
| 431 | + type_=ErrorBody, # type: ignore |
| 432 | + object_=_response.json(), |
| 433 | + ), |
| 434 | + ) |
| 435 | + ) |
319 | 436 | if _response.status_code == 422: |
320 | 437 | raise UnprocessableEntityError( |
321 | 438 | typing.cast( |
@@ -1169,6 +1286,121 @@ async def main() -> None: |
1169 | 1286 | raise ApiError(status_code=_response.status_code, body=_response.text) |
1170 | 1287 | raise ApiError(status_code=_response.status_code, body=_response_json) |
1171 | 1288 |
|
| 1289 | + async def upsert( |
| 1290 | + self, |
| 1291 | + id: str, |
| 1292 | + *, |
| 1293 | + email: typing.Optional[str] = OMIT, |
| 1294 | + name: typing.Optional[str] = OMIT, |
| 1295 | + tz: typing.Optional[str] = OMIT, |
| 1296 | + subscribe_to_emails: typing.Optional[bool] = OMIT, |
| 1297 | + request_options: typing.Optional[RequestOptions] = None, |
| 1298 | + ) -> User: |
| 1299 | + """ |
| 1300 | + Upsert a user (create or update). |
| 1301 | +
|
| 1302 | + Parameters |
| 1303 | + ---------- |
| 1304 | + id : str |
| 1305 | + ID of the user to upsert. |
| 1306 | +
|
| 1307 | + email : typing.Optional[str] |
| 1308 | + The user's email address. Required if subscribeToEmails is true. |
| 1309 | +
|
| 1310 | + name : typing.Optional[str] |
| 1311 | + The name to refer to the user by in emails. |
| 1312 | +
|
| 1313 | + tz : typing.Optional[str] |
| 1314 | + The user's timezone (used for email scheduling). |
| 1315 | +
|
| 1316 | + subscribe_to_emails : typing.Optional[bool] |
| 1317 | + Whether the user should receive Trophy-powered emails. Cannot be false if an email is provided. |
| 1318 | +
|
| 1319 | + request_options : typing.Optional[RequestOptions] |
| 1320 | + Request-specific configuration. |
| 1321 | +
|
| 1322 | + Returns |
| 1323 | + ------- |
| 1324 | + User |
| 1325 | + Upserted user |
| 1326 | +
|
| 1327 | + Examples |
| 1328 | + -------- |
| 1329 | + import asyncio |
| 1330 | +
|
| 1331 | + from trophy import AsyncTrophyApi |
| 1332 | +
|
| 1333 | + client = AsyncTrophyApi( |
| 1334 | + api_key="YOUR_API_KEY", |
| 1335 | + ) |
| 1336 | +
|
| 1337 | +
|
| 1338 | + async def main() -> None: |
| 1339 | + await client.users.upsert( |
| 1340 | + id="id", |
| 1341 | + email="user@example.com", |
| 1342 | + tz="Europe/London", |
| 1343 | + ) |
| 1344 | +
|
| 1345 | +
|
| 1346 | + asyncio.run(main()) |
| 1347 | + """ |
| 1348 | + _response = await self._client_wrapper.httpx_client.request( |
| 1349 | + f"users/{jsonable_encoder(id)}", |
| 1350 | + method="PUT", |
| 1351 | + json={ |
| 1352 | + "email": email, |
| 1353 | + "name": name, |
| 1354 | + "tz": tz, |
| 1355 | + "subscribeToEmails": subscribe_to_emails, |
| 1356 | + }, |
| 1357 | + request_options=request_options, |
| 1358 | + omit=OMIT, |
| 1359 | + ) |
| 1360 | + try: |
| 1361 | + if 200 <= _response.status_code < 300: |
| 1362 | + return typing.cast( |
| 1363 | + User, |
| 1364 | + parse_obj_as( |
| 1365 | + type_=User, # type: ignore |
| 1366 | + object_=_response.json(), |
| 1367 | + ), |
| 1368 | + ) |
| 1369 | + if _response.status_code == 400: |
| 1370 | + raise BadRequestError( |
| 1371 | + typing.cast( |
| 1372 | + ErrorBody, |
| 1373 | + parse_obj_as( |
| 1374 | + type_=ErrorBody, # type: ignore |
| 1375 | + object_=_response.json(), |
| 1376 | + ), |
| 1377 | + ) |
| 1378 | + ) |
| 1379 | + if _response.status_code == 401: |
| 1380 | + raise UnauthorizedError( |
| 1381 | + typing.cast( |
| 1382 | + ErrorBody, |
| 1383 | + parse_obj_as( |
| 1384 | + type_=ErrorBody, # type: ignore |
| 1385 | + object_=_response.json(), |
| 1386 | + ), |
| 1387 | + ) |
| 1388 | + ) |
| 1389 | + if _response.status_code == 422: |
| 1390 | + raise UnprocessableEntityError( |
| 1391 | + typing.cast( |
| 1392 | + ErrorBody, |
| 1393 | + parse_obj_as( |
| 1394 | + type_=ErrorBody, # type: ignore |
| 1395 | + object_=_response.json(), |
| 1396 | + ), |
| 1397 | + ) |
| 1398 | + ) |
| 1399 | + _response_json = _response.json() |
| 1400 | + except JSONDecodeError: |
| 1401 | + raise ApiError(status_code=_response.status_code, body=_response.text) |
| 1402 | + raise ApiError(status_code=_response.status_code, body=_response_json) |
| 1403 | + |
1172 | 1404 | async def update( |
1173 | 1405 | self, |
1174 | 1406 | id: str, |
@@ -1269,6 +1501,16 @@ async def main() -> None: |
1269 | 1501 | ), |
1270 | 1502 | ) |
1271 | 1503 | ) |
| 1504 | + if _response.status_code == 404: |
| 1505 | + raise NotFoundError( |
| 1506 | + typing.cast( |
| 1507 | + ErrorBody, |
| 1508 | + parse_obj_as( |
| 1509 | + type_=ErrorBody, # type: ignore |
| 1510 | + object_=_response.json(), |
| 1511 | + ), |
| 1512 | + ) |
| 1513 | + ) |
1272 | 1514 | if _response.status_code == 422: |
1273 | 1515 | raise UnprocessableEntityError( |
1274 | 1516 | typing.cast( |
|
0 commit comments