- self.controller = CacheController(self.cache)
-
- def _fetch(self, ressource, payload):
- """
- Prepare http request
- Use cached elements or proceed http request
- """
- req = Request('GET', ressource, params=payload,
- ).prepare()
- SimaEch.stats.update(total=SimaEch.stats.get('total')+1)
- if self.cache:
- cached_response = self.controller.cached_request(req.url, req.headers)
- if cached_response:
- SimaEch.stats.update(ccontrol=SimaEch.stats.get('ccontrol')+1)
- 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)
- if resp.status_code == 304:
- SimaEch.stats.update(etag=SimaEch.stats.get('etag')+1)
- resp = self.controller.update_cached_response(prepreq, resp)
- elif resp.status_code != 200:
- raise WSHTTPError('{0.status_code}: {0.reason}'.format(resp))
- ans = resp.json()
- self._controls_answer(ans)
- SimaEch.ratelimit = resp.headers.get('x-ratelimit-remaining', None)
- minrl = min(int(SimaEch.ratelimit), SimaEch.stats.get('minrl'))
- SimaEch.stats.update(minrl=minrl)
- if self.cache:
- self.controller.cache_response(resp.request, resp)
- return ans