Wrapper method for requests, to ease logging and mocking. Parameters should be the same as for ``requests.request``, except: :param session: A requests session object to use. :param verify_fingerprint: Optional. SHA1 or MD5 fingerprint of the expected server certificate.
(method, url, session=None, latin1_fallback=True,
verify_fingerprint=None, **kwargs)
| 107 | |
| 108 | |
| 109 | def request(method, url, session=None, latin1_fallback=True, |
| 110 | verify_fingerprint=None, **kwargs): |
| 111 | ''' |
| 112 | Wrapper method for requests, to ease logging and mocking. Parameters should |
| 113 | be the same as for ``requests.request``, except: |
| 114 | |
| 115 | :param session: A requests session object to use. |
| 116 | :param verify_fingerprint: Optional. SHA1 or MD5 fingerprint of the |
| 117 | expected server certificate. |
| 118 | :param latin1_fallback: RFC-2616 specifies the default Content-Type of |
| 119 | text/* to be latin1, which is not always correct, but exactly what |
| 120 | requests is doing. Setting this parameter to False will use charset |
| 121 | autodetection (usually ending up with utf8) instead of plainly falling |
| 122 | back to this silly default. See |
| 123 | https://github.com/kennethreitz/requests/issues/2042 |
| 124 | ''' |
| 125 | |
| 126 | if session is None: |
| 127 | session = requests.Session() |
| 128 | |
| 129 | if verify_fingerprint is not None: |
| 130 | _install_fingerprint_adapter(session, verify_fingerprint) |
| 131 | |
| 132 | session.hooks = dict(response=_fix_redirects) |
| 133 | |
| 134 | func = session.request |
| 135 | |
| 136 | logger.debug('{} {}'.format(method, url)) |
| 137 | logger.debug(kwargs.get('headers', {})) |
| 138 | logger.debug(kwargs.get('data', None)) |
| 139 | logger.debug('Sending request...') |
| 140 | |
| 141 | assert isinstance(kwargs.get('data', b''), bytes) |
| 142 | |
| 143 | r = func(method, url, **kwargs) |
| 144 | |
| 145 | # See https://github.com/kennethreitz/requests/issues/2042 |
| 146 | content_type = r.headers.get('Content-Type', '') |
| 147 | if not latin1_fallback and \ |
| 148 | 'charset' not in content_type and \ |
| 149 | content_type.startswith('text/'): |
| 150 | logger.debug('Removing latin1 fallback') |
| 151 | r.encoding = None |
| 152 | |
| 153 | logger.debug(r.status_code) |
| 154 | logger.debug(r.headers) |
| 155 | logger.debug(r.content) |
| 156 | |
| 157 | if r.status_code == 412: |
| 158 | raise exceptions.PreconditionFailed(r.reason) |
| 159 | if r.status_code in (404, 410): |
| 160 | raise exceptions.NotFoundError(r.reason) |
| 161 | |
| 162 | r.raise_for_status() |
| 163 | return r |
| 164 | |
| 165 | |
| 166 | def _fix_redirects(r, *args, **kwargs): |
no test coverage detected