mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-08 16:53:23 +00:00
63 lines
1.3 KiB
Python
63 lines
1.3 KiB
Python
from six.moves.urllib.parse import urlparse, parse_qsl
|
|
|
|
|
|
class Request(object):
|
|
|
|
def __init__(self, method, uri, body, headers):
|
|
self.method = method
|
|
self.uri = uri
|
|
self.body = body
|
|
self.headers = headers
|
|
|
|
def add_header(self, key, value):
|
|
self.headers[key] = value
|
|
|
|
@property
|
|
def scheme(self):
|
|
return urlparse(self.uri).scheme
|
|
|
|
@property
|
|
def host(self):
|
|
return urlparse(self.uri).hostname
|
|
|
|
@property
|
|
def port(self):
|
|
return urlparse(self.uri).port
|
|
|
|
@property
|
|
def path(self):
|
|
return urlparse(self.uri).path
|
|
|
|
@property
|
|
def query(self):
|
|
q = urlparse(self.uri).query
|
|
return sorted(parse_qsl(q))
|
|
|
|
# alias for backwards compatibility
|
|
@property
|
|
def url(self):
|
|
return self.uri
|
|
|
|
# alias for backwards compatibility
|
|
@property
|
|
def protocol(self):
|
|
return self.scheme
|
|
|
|
def __str__(self):
|
|
return "<Request ({0}) {1}>".format(self.method, self.uri)
|
|
|
|
def __repr__(self):
|
|
return self.__str__()
|
|
|
|
def _to_dict(self):
|
|
return {
|
|
'method': self.method,
|
|
'uri': self.uri,
|
|
'body': self.body,
|
|
'headers': dict(self.headers),
|
|
}
|
|
|
|
@classmethod
|
|
def _from_dict(cls, dct):
|
|
return Request(**dct)
|