Encapsulate the context for an HTTP resource. Context for an HTTP resource may include properties such as the URL, default timeout for connections, default headers to be included in each request, and auth.
| 77 | |
| 78 | |
| 79 | class Resource(): |
| 80 | """ |
| 81 | Encapsulate the context for an HTTP resource. |
| 82 | |
| 83 | Context for an HTTP resource may include properties such as the URL, |
| 84 | default timeout for connections, default headers to be included in each |
| 85 | request, and auth. |
| 86 | """ |
| 87 | SUCCESS_CODES = frozenset(range(200, 300)) |
| 88 | ERROR_CODE_MAP = {c.STATUS_CODE: c for c in ( |
| 89 | MesosBadRequestException, |
| 90 | MesosAuthenticationException, |
| 91 | MesosAuthorizationException, |
| 92 | MesosUnprocessableException, |
| 93 | MesosInternalServerErrorException, |
| 94 | MesosServiceUnavailableException)} |
| 95 | |
| 96 | def __init__(self, |
| 97 | url, |
| 98 | default_headers=None, |
| 99 | default_timeout=DEFAULT_TIMEOUT, |
| 100 | default_auth=DEFAULT_AUTH, |
| 101 | default_use_gzip_encoding=DEFAULT_USE_GZIP_ENCODING, |
| 102 | default_max_attempts=DEFAULT_MAX_ATTEMPTS): |
| 103 | """ |
| 104 | :param url: URL identifying the resource |
| 105 | :type url: str |
| 106 | :param default_headers: headers to attache to requests |
| 107 | :type default_headers: dict[str, str] |
| 108 | :param default_timeout: timeout in seconds |
| 109 | :type default_timeout: float |
| 110 | :param default_auth: auth scheme |
| 111 | :type default_auth: requests.auth.AuthBase |
| 112 | :param default_use_gzip_encoding: use gzip encoding by default or not |
| 113 | :type default_use_gzip_encoding: bool |
| 114 | :param default_max_attempts: max number of attempts when retrying |
| 115 | :type default_max_attempts: int |
| 116 | """ |
| 117 | self.url = urlparse(url) |
| 118 | self.default_timeout = default_timeout |
| 119 | self.default_auth = default_auth |
| 120 | self.default_use_gzip_encoding = default_use_gzip_encoding |
| 121 | self.default_max_attempts = default_max_attempts |
| 122 | |
| 123 | if default_headers is None: |
| 124 | self._default_headers = {} |
| 125 | else: |
| 126 | self._default_headers = deepcopy(default_headers) |
| 127 | |
| 128 | def default_headers(self): |
| 129 | """ |
| 130 | Return a copy of the default headers. |
| 131 | |
| 132 | :rtype: dict[str, str] |
| 133 | """ |
| 134 | return deepcopy(self._default_headers) |
| 135 | |
| 136 | def subresource(self, subpath): |