|
| 1 | +import collections.abc |
| 2 | +import types |
| 3 | +import typing |
| 4 | + |
| 5 | +from . import _abc |
| 6 | + |
| 7 | + |
| 8 | +class BaseHeaders(collections.abc.Mapping): |
| 9 | + |
| 10 | + def __init__(self, source=None): |
| 11 | + if source is None: |
| 12 | + self.__http_headers__ = {} |
| 13 | + else: |
| 14 | + self.__http_headers__ = {k.lower(): v for k, v in source.items()} |
| 15 | + |
| 16 | + def __getitem__(self, key: str) -> str: |
| 17 | + return self.__http_headers__[key.lower()] |
| 18 | + |
| 19 | + def __len__(self): |
| 20 | + return len(self.__http_headers__) |
| 21 | + |
| 22 | + def __contains__(self, key: str): |
| 23 | + return key.lower() in self.__http_headers__ |
| 24 | + |
| 25 | + def __iter__(self): |
| 26 | + return iter(self.__http_headers__) |
| 27 | + |
| 28 | + |
| 29 | +class RequestHeaders(BaseHeaders): |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +class ResponseHeaders(BaseHeaders, collections.abc.MutableMapping): |
| 34 | + |
| 35 | + def __setitem__(self, key: str, value: str): |
| 36 | + self.__http_headers__[key.lower()] = value |
| 37 | + |
| 38 | + def __delitem__(self, key: str): |
| 39 | + del self.__http_headers__[key.lower()] |
| 40 | + |
| 41 | + |
| 42 | +class HttpRequest(_abc.HttpRequest): |
| 43 | + """An HTTP request object.""" |
| 44 | + |
| 45 | + def __init__(self, method: str, url: str, |
| 46 | + headers: typing.Mapping[str, str], |
| 47 | + params: typing.Mapping[str, str], |
| 48 | + body): |
| 49 | + self.__method = method |
| 50 | + self.__url = url |
| 51 | + self.__headers = RequestHeaders(headers) |
| 52 | + self.__params = types.MappingProxyType(params) |
| 53 | + self.__body = body |
| 54 | + |
| 55 | + @property |
| 56 | + def url(self): |
| 57 | + return self.__url |
| 58 | + |
| 59 | + @property |
| 60 | + def method(self): |
| 61 | + return self.__method.upper() |
| 62 | + |
| 63 | + @property |
| 64 | + def headers(self): |
| 65 | + return self.__headers |
| 66 | + |
| 67 | + @property |
| 68 | + def params(self): |
| 69 | + return self.__params |
| 70 | + |
| 71 | + def get_body(self): |
| 72 | + return self.__body |
| 73 | + |
| 74 | + |
| 75 | +class HttpResponse(_abc.HttpResponse): |
| 76 | + """An HTTP response object.""" |
| 77 | + |
| 78 | + def __init__(self, body=None, *, |
| 79 | + status_code=None, headers=None, mimetype=None, charset=None): |
| 80 | + if status_code is None: |
| 81 | + status_code = 200 |
| 82 | + self.__status_code = status_code |
| 83 | + |
| 84 | + if mimetype is None: |
| 85 | + mimetype = 'text/plain' |
| 86 | + self.__mimetype = mimetype |
| 87 | + |
| 88 | + if charset is None: |
| 89 | + charset = 'utf-8' |
| 90 | + self.__charset = charset |
| 91 | + |
| 92 | + if headers is None: |
| 93 | + headers = {} |
| 94 | + self.__headers = ResponseHeaders(headers) |
| 95 | + |
| 96 | + if body is not None: |
| 97 | + self.__set_body(body) |
| 98 | + else: |
| 99 | + self.__body = None |
| 100 | + |
| 101 | + @property |
| 102 | + def mimetype(self): |
| 103 | + return self.__mimetype |
| 104 | + |
| 105 | + @property |
| 106 | + def charset(self): |
| 107 | + return self.__charset |
| 108 | + |
| 109 | + @property |
| 110 | + def headers(self): |
| 111 | + return self.__headers |
| 112 | + |
| 113 | + @property |
| 114 | + def status_code(self): |
| 115 | + return self.__status_code |
| 116 | + |
| 117 | + def __set_body(self, body): |
| 118 | + if isinstance(body, str): |
| 119 | + body = body.encode(self.__charset) |
| 120 | + |
| 121 | + if not isinstance(body, (bytes, bytearray)): |
| 122 | + raise TypeError( |
| 123 | + f'reponse is expected to be either of ' |
| 124 | + f'str, bytes, or bytearray, got {type(body).__name__}') |
| 125 | + |
| 126 | + self.__body = body |
| 127 | + |
| 128 | + def get_body(self): |
| 129 | + return self.__body |
0 commit comments