def _create_response(self, status, headers, output, app_iter, trusted): content_type = header_value(headers, 'content-type') if app_iter is None: # @@: Can this really happen? # What happens with a 204 No Content? return self._make_response( status, headers, data=None) if not output: # Easy, return plain output # @@: Check charset? return self._make_response( status, headers, data=''.join(app_iter)) if isinstance(output, basestring): if output.startswith('name '): output = get_format(output[5:].strip()) else: # Must be a Python type output = find_format_match( output, content_type) elif isinstance(output, Format): pass else: raise TypeError( "Invalid value for output: %r" % output) data = output.parse_wsgi_response( status, headers, app_iter, trusted=trusted) return self._make_response( status, headers, data=data)
def __init__(self, cache=None, default_encoding=None, default_input_content_type=None, default_input_format=None, prefer_input_mimetypes=None, default_post_input_type=None, redirections=None): self.httplib2 = httplib2.Http(cache) self.cache = cache if default_encoding is not None: self.default_encoding = default_encoding if default_input_content_type is not None: self.default_input_content_type = default_input_content_type if default_input_format is not None: if isinstance(default_input_format, basestring): default_input_format = get_format(default_input_format) self.default_input_format = default_input_format if prefer_input_mimetypes is not None: if isinstance(prefer_input_mimetypes, basestring): raise TypeError( "prefer_input_mimetypes must be a list (not %r)" % prefer_input_mimetypes) self.prefer_input_mimetypes = prefer_input_mimetypes if default_post_input_type is not None: self.default_post_input_type = default_post_input_type if redirections is not None: self.redirections = redirections for name in ['add_credentials', 'clear_credentials']: setattr(self, name, getattr(self.httplib2, name)) self._raw_request = self.httplib2.request
def request(self, uri, method="GET", body=None, headers=None, wsgi_request=None, input=None, output=None, trusted=False): method = method.upper() wsgi_request = self._coerce_wsgi_request(wsgi_request) headers = self._coerce_headers(headers) if isinstance(output, basestring) and output.startswith('name '): output = get_format(output[5:].strip()) input, body, headers = self._coerce_input( input, body, headers) if body and not header_value(headers, 'content-type'): # We have to add a content type... content_type = input.choose_mimetype(headers, body) replace_header(headers, 'content-type', content_type) headers = self._set_accept(headers, output) if wsgi_request is not None: uri = self._resolve_uri(uri, wsgi_request) if self._internally_resolvable(uri, wsgi_request): return self._internal_request( uri, method=method, body=body, headers=headers, wsgi_request=wsgi_request, input=input, output=output, trusted=trusted) else: if not scheme_re.search(uri): raise ValueError( 'You gave a non-absolute URI (%r) and no wsgi_request to ' 'normalize it against' % uri) return self._external_request( uri, method=method, body=body, headers=headers, wsgi_request=wsgi_request, input=input, output=output, trusted=trusted)
def __init__(self, data, type=None, format=None, headers=None, content_type=None, default_format=None): if type is None and format is None: raise TypeError("You must provide a type or format argument") if type is not None and format is not None: assert format.type == type, ( "type argument (%r) and format (%r; type=%r) do not match" % (type, format, format.type)) self.data = data self.type = type if isinstance(format, basestring): format = get_format(format) self.format = format if isinstance(default_format, basestring): default_format = get_format(default_format) self.default_format = default_format self.headers = headers self.content_type = content_type
def _coerce_input(self, input, body, headers): # Case when there's no request body: if body is None or body == '': return None, '', headers if isinstance(input, Format): # We've got an explicit format return input, body, headers if isinstance(input, basestring) and input.startswith('name '): # A named format input = get_format(input[5:].strip()) return input, body, headers if not input and isinstance(body, basestring): if isinstance(body, unicode): if not self.default_input_encoding: raise ValueError( "There is no default_input_encoding, and you gave a unicode request body") input = input.encode(self.default_input_encoding) # Should we set charset in the Content-type at this # time? return input, body, headers if not input: # @@: Should this perhaps default to 'python'? # Or should we autodetect dict and list as 'python'? if self.default_input_format: input = self.default_input_format else: raise ValueError( "You gave a non-string body (%r) and no input " "(nor is there a default_input_format)" % body) return input, body, headers if isinstance(input, basestring): # Must be a type of Python object input = find_format_by_type( input, self.prefer_input_mimetypes) else: # I don't know what it is...? raise TypeError( "Invalid value for input: %r" % input) return input, body, headers