123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- from .session import ClientSession
- from .endpoints import (
- LiveEndpointsMixin,
- VREndpointsMixin,
- RoomEndpointsMixin,
- UserEndpointsMixin,
- OtherEndpointsMixin
- )
- from json import JSONDecodeError
- import time
- from showroom.api.utils import get_csrf_token
- from requests.exceptions import HTTPError
- import logging
- _base_url = 'https://www.showroom-live.com'
- # TODO: logging, warnings
- # TODO: load auth or credentials from file or dict
- # TODO: save auth or credentials to file
- client_logger = logging.getLogger('showroom.client')
- class ShowroomClient(
- LiveEndpointsMixin,
- UserEndpointsMixin,
- RoomEndpointsMixin,
- VREndpointsMixin,
- OtherEndpointsMixin
- ):
- """
- Client for interacting with the Showroom API.
-
- :param cookies: dict containing stored cookies
-
- :ivar cookies: Reference to the underlying session's cookies.
- """
- def __init__(self, cookies=None):
- self._session = ClientSession()
- self._auth = None
- self.cookies = self._session.cookies
- if cookies:
- self.cookies.update(cookies)
- expiry = self.cookies.expires_earliest
- if expiry and int(time.time()) >= expiry:
- # TODO: more information, more specific error
- raise ValueError('A cookie has expired')
- # TODO: does this actually mean we're logged in? if no, how do I check?
- self._auth = self.cookies.get('sr_id')
- # TODO: request responses in different languages
- # to force japanese text in responses:
- # self.session.cookies.update({'lang': 'ja'})
- # this doesn't always seem to work? it worked until i manually set lang:en, then switching back failed
- self.__csrf_token = None
- self._last_response = None
- @property
- def _csrf_token(self):
- if not self.__csrf_token:
- self._update_csrf_token(_base_url)
- return self.__csrf_token
- def _update_csrf_token(self, url):
- r = self._session.get(url)
- self.__csrf_token = get_csrf_token(r.text)
- def _api_get(self, endpoint, params=None, return_response=False, default=None, raise_error=True):
- try:
- r = self._session.get(_base_url + endpoint, params=params)
- except HTTPError as e:
- r = e.response
- if raise_error:
- raise
- self._last_response = r
- if return_response:
- return r
- else:
- try:
- return r.json()
- except JSONDecodeError as e:
- client_logger.error('JSON decoding error while getting {}: {}'.format(r.request.url, e))
- return default or {}
- def _api_post(self, endpoint, params=None, data=None, return_response=None, default=None):
- try:
- r = self._session.post(_base_url + endpoint, params=params, data=data)
- except HTTPError as e:
- r = e.response
- self._last_response = r
- # TODO: check for expired csrf_token
- if return_response:
- return r
- else:
- try:
- return r.json()
- except JSONDecodeError as e:
- client_logger.error('JSON decoding error while posting to {}: {}'.format(r.request.url, e))
- return default or {}
|