def f(): try: resp = yield from asyncio.wait_for(aiohttp.request( 'POST', self._url, data=data, compress=False, headers=headers, connector=self._connector, loop=self._loop), self.timeout, loop=self._loop) yield from resp.release() code = resp.status if code != 200: msg = resp.headers.get('x-sentry-error') if code == 429: try: retry_after = int(resp.headers.get('retry-after')) except (ValueError, TypeError): retry_after = 0 failure_cb(RateLimited(msg, retry_after)) else: failure_cb(APIError(msg, code)) else: success_cb() except Exception as exc: failure_cb(exc)
def send(self, url, data, headers): """ Sends a request to a remote webserver using HTTP POST. """ try: response = requests.post( url=url, data=data, timeout=self.timeout, verify=self.verify_ssl, headers=headers, ) except urllib2.HTTPError as exc: msg = exc.headers.get('x-sentry-error') code = exc.getcode() if code == 429: try: retry_after = int(exc.headers.get('retry-after')) except (ValueError, TypeError): retry_after = 0 raise RateLimited(msg, retry_after) elif msg: raise APIError(msg, code) else: raise return response
def f(): session = self.client_session() try: resp = yield from asyncio.wait_for(session.post( url, data=data, compress=False, headers=headers), self.timeout, loop=self._loop) yield from resp.release() code = resp.status if code != 200: msg = resp.headers.get('x-sentry-error') if code == 429: try: retry_after = int(resp.headers.get('retry-after')) except (ValueError, TypeError): retry_after = 0 failure_cb(RateLimited(msg, retry_after)) else: failure_cb(APIError(msg, code)) else: success_cb() except Exception as exc: failure_cb(exc) finally: if not self.keepalive: yield from session.close()
def test_send_remote_failover_with_retry_after(self, should_try, send): should_try.return_value = True client = Client(dsn='sync+http://public:[email protected]/1') # test error send.side_effect = RateLimited('foo', 5) client.send_remote('sync+http://example.com/api/store', 'foo') self.assertEquals(client.state.status, client.state.ERROR) self.assertEqual(client.state.retry_after, 5) # test recovery send.side_effect = None client.send_remote('sync+http://example.com/api/store', 'foo') self.assertEquals(client.state.status, client.state.ONLINE) self.assertEqual(client.state.retry_after, 0)
def _do_send(self, url, data, headers, success_cb, failure_cb): if self.keepalive: session = self._client_session else: session = self._client_session_factory() resp = None try: resp = yield from session.post( url, data=data, compress=False, headers=headers, timeout=self.timeout ) code = resp.status if code != 200: msg = resp.headers.get('x-sentry-error') if code == 429: try: retry_after = resp.headers.get('retry-after') retry_after = int(retry_after) except (ValueError, TypeError): retry_after = 0 failure_cb(RateLimited(msg, retry_after)) else: failure_cb(APIError(msg, code)) else: success_cb() except asyncio.CancelledError: # do not mute asyncio.CancelledError raise except Exception as exc: failure_cb(exc) finally: if resp is not None: resp.release() if not self.keepalive: yield from session.close()
def send(self, data, headers): if self.verify_ssl: # If SSL verification is enabled use the provided CA bundle to # perform the verification. self.verify_ssl = self.ca_certs response = requests.post(self._url, data=data, headers=headers, verify=self.verify_ssl, timeout=self.timeout) if response.status_code >= 400: msg = response.headers.get('x-sentry-error') if response.status_code == 429: try: retry_after = int(response.headers.get('retry-after')) except (ValueError, TypeError): retry_after = 0 raise RateLimited(msg, retry_after) elif msg: raise APIError(msg, response.status_code)
def send(self, data, headers): req = urllib2.Request(self._url, headers=headers) try: response = urlopen(url=req, data=data, timeout=self.timeout, verify_ssl=self.verify_ssl, ca_certs=self.ca_certs) except urllib2.HTTPError as exc: msg = exc.headers.get('x-sentry-error') code = exc.getcode() if code == 429: try: retry_after = int(exc.headers.get('retry-after')) except (ValueError, TypeError): retry_after = 0 raise RateLimited(msg, retry_after) elif msg: raise APIError(msg, code) else: raise return response