""" Integration with User Data API. User Data API: https://github.com/filtr/apollo-user-data. """ from apollo_utils.service.exceptions import APIInvalidResponse from http import HTTPStatus from typing import Tuple from server import config from server.client.base.client import ApiKeyClient from server.client.base.config import ApiKeyClientConfig from server.constants import Service class UserDataApiConfig(ApiKeyClientConfig): """Configuration class for User Data api client.""" service = Service.USER_DATA_API.value uri = config.USER_DATA_URI auth_secret = config.USERDATAAPI_APPKEY class UserDataApiClient(ApiKeyClient): """Client for sending requests to User Data api app.""" async def add_user_favorites( self, headers, body, allowed_status_codes: Tuple = (HTTPStatus.BAD_REQUEST,), with_status_code: bool = False, **params, ): try: result = await self.send_request( "api/users/favorites/", headers=headers, method="POST", body=body, params=params, include_original_status_code=with_status_code, ) except APIInvalidResponse as ud_error: if allowed_status_codes and ud_error.original_status_code in allowed_status_codes: result = ud_error.original_response if with_status_code: result = ud_error.original_status_code, result else: raise ud_error return result async def get_user_favorites(self, headers=None, **params): return await self.send_request("api/users/favorites/", headers=headers, params=params) async def get_v1_favorites_playlists(self, headers, **params): return await self.send_request("api/v1/favorites/playlists/", headers=headers, params=params) async def get_v1_user_settings(self, headers, **params): return await self.send_request("api/v1/settings/", headers=headers, params=params) async def register_device(self, headers, **params): return await self.send_request( "api/users/devices/register/", headers=headers, method="POST", body=params, status_as_result=True ) async def remove_user_favorites( self, headers, allowed_status_codes: Tuple = (HTTPStatus.BAD_REQUEST,), with_status_code: bool = False, **params ): try: result = await self.send_request( "api/users/favorites/", headers=headers, method="DELETE", params=params, include_original_status_code=with_status_code, ) except APIInvalidResponse as ud_error: if allowed_status_codes and ud_error.original_status_code in allowed_status_codes: result = ud_error.original_response if with_status_code: result = ud_error.original_status_code, result else: raise ud_error return result async def unregister_device(self, headers, **params): return await self.send_request( "api/users/devices/unregister/", headers=headers, method="POST", body=params, status_as_result=True ) async def user_favorites_list(self, headers, **params): return await self.send_request("api/service/users/favorites/list/", headers=headers, method="POST", body=params) async def user_settings_list(self, headers, **params): return await self.send_request("api/service/users/settings/list/", headers=headers, method="POST", body=params) async def users_messages_feed_view(self, **params): return await self.send_request( "api/users/messages/feed/view/", method="POST", body=params, status_as_result=True )