def handle_api_error(self, rbody, rcode, resp, rheaders): # Rate limits were previously coded as 400's with code 'rate_limit' if rcode == 429: raise error.RateLimitError( resp.get('detail'), rbody, rcode, resp, rheaders) elif rcode in [400, 404]: type = resp.get('type') if type == 'about:blank': type = None raise error.InvalidRequestError( resp.get('detail'), type, rbody, rcode, resp, rheaders) elif rcode == 401: raise error.AuthenticationError( resp.get('detail'), rbody, rcode, resp, rheaders) else: detail = resp.get('detail') # This information will only be returned to developers of # the OpenAI Gym Scoreboard. dev_info = resp.get('dev_info') if dev_info: detail = "{}\n\n<dev_info>\n{}\n</dev_info>".format(detail, dev_info['traceback']) raise error.APIError(detail, rbody, rcode, resp, rheaders)
def interpret_response(self, rbody, rcode, rheaders): content_type = rheaders.get('Content-Type', '') if content_type.startswith('text/plain'): # Pass through plain text resp = rbody if not (200 <= rcode < 300): self.handle_api_error(rbody, rcode, {}, rheaders) else: # TODO: Be strict about other Content-Types try: if hasattr(rbody, 'decode'): rbody = rbody.decode('utf-8') resp = json.loads(rbody) except Exception: raise error.APIError( "Invalid response body from API: %s " "(HTTP response code was %d)" % (rbody, rcode), rbody, rcode, rheaders) if not (200 <= rcode < 300): self.handle_api_error(rbody, rcode, resp, rheaders) return resp