- def _fetch(self, payload):
- """
- Prepare http request
- Use cached elements or proceed http request
- """
- req = Request('GET', SimaFM.root_url, params=payload,
- ).prepare()
- if self.cache:
- cached_response = self.controller.cached_request(req.url, req.headers)
- if cached_response:
- return cached_response.json()
- try:
- return self._fetch_ws(req)
- except Timeout:
- raise WSTimeout('Failed to reach server within {0}s'.format(
- SOCKET_TIMEOUT))
- except ConnectionError as err:
- raise WSError(err)
-
- @Throttle(WAIT_BETWEEN_REQUESTS)
- def _fetch_ws(self, prepreq):
- """fetch from web service"""
- sess = Session()
- resp = sess.send(prepreq, timeout=SOCKET_TIMEOUT)
- #self.__class__.ratelimit = resp.headers.get('x-ratelimit-remaining', None)
- if resp.status_code is not 200:
- raise WSHTTPError('{0.status_code}: {0.reason}'.format(resp))
- ans = resp.json()
- self._controls_answer(ans)
- if self.cache:
- self.controller.cache_response(resp.request, resp)
- return ans