Python requests.structures.CaseInsensitiveDict() Examples
The following are 30
code examples of requests.structures.CaseInsensitiveDict().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
requests.structures
, or try the search function
.
Example #1
Source File: Pinterest.py From py3-pinterest with MIT License | 7 votes |
def request(self, method, url, data=None, files=None, extra_headers=None): headers = CaseInsensitiveDict([ ('Referer', HOME_PAGE), ('X-Requested-With', 'XMLHttpRequest'), ('Accept', 'application/json'), ('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), ('User-Agent', self.user_agent)]) csrftoken = self.http.cookies.get('csrftoken') if csrftoken: headers.update([('X-CSRFToken', csrftoken)]) if extra_headers is not None: for h in extra_headers: headers.update([(h, extra_headers[h])]) response = self.http.request(method, url, data=data, headers=headers, files=files, proxies=self.proxies) response.raise_for_status() self.registry.update(Registry.Key.COOKIES, response.cookies) return response
Example #2
Source File: _api_client.py From cognite-sdk-python with Apache License 2.0 | 6 votes |
def _configure_headers(self, additional_headers): headers = CaseInsensitiveDict() headers.update(requests.utils.default_headers()) if self._config.token is None: headers["api-key"] = self._config.api_key elif isinstance(self._config.token, str): headers["Authorization"] = "Bearer {}".format(self._config.token) elif isinstance(self._config.token, Callable): headers["Authorization"] = "Bearer {}".format(self._config.token()) else: raise TypeError("'token' must be str, Callable, or None.") headers["content-type"] = "application/json" headers["accept"] = "application/json" headers["x-cdp-sdk"] = "CognitePythonSDK:{}".format(utils._auxiliary.get_current_sdk_version()) headers["x-cdp-app"] = self._config.client_name if "User-Agent" in headers: headers["User-Agent"] += " " + utils._auxiliary.get_user_agent() else: headers["User-Agent"] = utils._auxiliary.get_user_agent() headers.update(additional_headers) return headers
Example #3
Source File: http.py From oss-ftp with MIT License | 6 votes |
def __init__(self, method, url, data=None, params=None, headers=None, app_name=''): self.method = method self.url = url self.data = _convert_request_body(data) self.params = params or {} if not isinstance(headers, CaseInsensitiveDict): self.headers = CaseInsensitiveDict(headers) else: self.headers = headers # tell requests not to add 'Accept-Encoding: gzip, deflate' by default if 'Accept-Encoding' not in self.headers: self.headers['Accept-Encoding'] = None if 'User-Agent' not in self.headers: if app_name: self.headers['User-Agent'] = _USER_AGENT + '/' + app_name else: self.headers['User-Agent'] = _USER_AGENT
Example #4
Source File: http.py From oss-ftp with MIT License | 6 votes |
def __init__(self, method, url, data=None, params=None, headers=None, app_name=''): self.method = method self.url = url self.data = _convert_request_body(data) self.params = params or {} if not isinstance(headers, CaseInsensitiveDict): self.headers = CaseInsensitiveDict(headers) else: self.headers = headers # tell requests not to add 'Accept-Encoding: gzip, deflate' by default if 'Accept-Encoding' not in self.headers: self.headers['Accept-Encoding'] = None if 'User-Agent' not in self.headers: if app_name: self.headers['User-Agent'] = _USER_AGENT + '/' + app_name else: self.headers['User-Agent'] = _USER_AGENT
Example #5
Source File: browser.py From brozzler with Apache License 2.0 | 6 votes |
def _network_response_received(self, message): status = message['params']['response'].get('status') if (status == 420 and 'Warcprox-Meta' in CaseInsensitiveDict( message['params']['response']['headers'])): if not self.reached_limit: warcprox_meta = json.loads(CaseInsensitiveDict( message['params']['response']['headers'])['Warcprox-Meta']) self.reached_limit = brozzler.ReachedLimit( warcprox_meta=warcprox_meta) self.logger.info('reached limit %s', self.reached_limit) brozzler.thread_raise( self.calling_thread, brozzler.ReachedLimit) else: self.logger.info( 'reached limit but self.reached_limit is already set, ' 'assuming the calling thread is already handling this') if self.on_response: self.on_response(message) if status and self.page_status is None: self.page_status = status
Example #6
Source File: client.py From cs with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _prepare_request(self, command, json=True, opcode_name='command', fetch_list=False, **kwargs): params = CaseInsensitiveDict(**kwargs) params.update({ 'apiKey': self.key, opcode_name: command, }) if json: params['response'] = 'json' if 'page' in kwargs or fetch_list: params.setdefault('pagesize', PAGE_SIZE) if 'expires' not in params and self.expiration.total_seconds() >= 0: params['signatureVersion'] = '3' tz = pytz.utc expires = tz.localize(datetime.utcnow() + self.expiration) params['expires'] = expires.astimezone(tz).strftime(EXPIRES_FORMAT) kind = 'params' if self.method == 'get' else 'data' return kind, dict(params.items())
Example #7
Source File: engine.py From fossor with BSD 2-Clause "Simplified" License | 6 votes |
def __init__(self): self.log = logging.getLogger(__name__) self.plugin_parent_classes = [fossor.plugin.Plugin, fossor.variables.variable.Variable, fossor.checks.check.Check, fossor.reports.report.Report] # Variables self.variables = CaseInsensitiveDict() self.add_variable('timeout', 600) # Default timeout # Imported Plugins self.variable_plugins = set() self.check_plugins = set() self.report_plugins = set() self.add_plugins() # Adds all plugins located within the fossor module recursively
Example #8
Source File: request.py From jumpserver-python-sdk with GNU General Public License v2.0 | 6 votes |
def __init__(self, url, method='get', data=None, params=None, headers=None, content_type='application/json', **kwargs): self.url = url self.method = method self.params = params or {} self.kwargs = kwargs if not isinstance(headers, dict): headers = {} self.headers = CaseInsensitiveDict(headers) if content_type: self.headers['Content-Type'] = content_type if data: self.data = json.dumps(data) else: self.data = {}
Example #9
Source File: models.py From aliyun-oss-python-sdk with MIT License | 6 votes |
def to_object_meta(self, headers=None, multipart_upload_context=None): if not isinstance(headers, CaseInsensitiveDict): headers = CaseInsensitiveDict(headers) if 'content-md5' in headers: headers[OSS_CLIENT_SIDE_ENCRYPTION_UNENCRYPTED_CONTENT_MD5] = headers['content-md5'] del headers['content-md5'] if 'content-length' in headers: headers[OSS_CLIENT_SIDE_ENCRYPTION_UNENCRYPTED_CONTENT_LENGTH] = headers['content-length'] del headers['content-length'] headers[OSS_CLIENT_SIDE_ENCRYPTION_KEY] = b64encode_as_string(self.encrypted_key) headers[OSS_CLIENT_SIDE_ENCRYPTION_START] = b64encode_as_string(self.encrypted_iv) headers[OSS_CLIENT_SIDE_ENCRYPTION_CEK_ALG] = self.cek_alg headers[OSS_CLIENT_SIDE_ENCRYPTION_WRAP_ALG] = self.wrap_alg if multipart_upload_context and multipart_upload_context.data_size and multipart_upload_context.part_size: headers[OSS_CLIENT_SIDE_ENCRYPTION_DATA_SIZE] = str(multipart_upload_context.data_size) headers[OSS_CLIENT_SIDE_ENCRYPTION_PART_SIZE] = str(multipart_upload_context.part_size) if self.mat_desc: headers[OSS_CLIENT_SIDE_ENCRYTPION_MATDESC] = json.dumps(self.mat_desc) return headers
Example #10
Source File: test_models.py From eventbrite-sdk-python with Apache License 2.0 | 6 votes |
def test_create_from_payload(self): evbobject = self.evbobject self.assertEqual( sorted([u'id', u'first_name', u'last_name', u'emails', u'name']), sorted(evbobject.keys()) ) self.assertTrue(evbobject.ok) self.assertEqual( self.url, evbobject.resource_uri ) self.assertTrue(isinstance(evbobject.elapsed, timedelta)) self.assertTrue(isinstance(evbobject.headers, CaseInsensitiveDict))
Example #11
Source File: test_adapter.py From pypowervm with Apache License 2.0 | 6 votes |
def _mk_response(self, status, content=None): reasons = {200: 'OK', 204: 'No Content', 401: 'Unauthorized'} # Create a Response object, that will serve as a mock return value my_response = req_mod.Response() my_response.status_code = status my_response.reason = reasons[status] clen = '0' if status == 200 and content: clen = str(len(content)) dict_headers = { 'content-length': clen, 'x-powered-by': 'Servlet/3.0', 'set-cookie': ('JSESSIONID=0000a41BnJsGTNQvBGERA3wR1nj:759878cb-4f' '9a-4b05-a09a-3357abfea3b4; Path=/; Secure; HttpOnl' 'y, CCFWSESSION=E4C0FFBE9130431DBF1864171ECC6A6E; P' 'ath=/; Secure; HttpOnly'), 'expires': 'Thu, 01 Dec 1994 16:00:00 GMT', 'x-transaction-id': 'XT10000073', 'cache-control': 'no-cache="set-cookie, set-cookie2"', 'date': 'Wed, 23 Jul 2014 21:51:10 GMT', 'content-type': 'application/vnd.ibm.powervm'} my_response.headers = req_struct.CaseInsensitiveDict(dict_headers) my_response._content = content return my_response
Example #12
Source File: serializer.py From ws-backend-community with GNU General Public License v3.0 | 6 votes |
def __deserialize_requests_response(self, to_deserialize): """ Create and return a Response based on the contents of the given dictionary. :param to_deserialize: The dictionary to create the Response from. :return: A Response created by the contents of to_deserialize. """ to_return = Response() to_return.status_code = to_deserialize["status_code"] to_return.headers = CaseInsensitiveDict(to_deserialize["headers"]) to_return.encoding = to_deserialize["encoding"] to_return._content = to_deserialize["content"] to_return._content_consumed = True to_return.reason = to_deserialize["reason"] to_return.url = to_deserialize["url"] to_return.request = self.decode(to_deserialize["request"]) return to_return
Example #13
Source File: test_requests.py From prettyprinter with MIT License | 6 votes |
def test_prepared_request(): request = Request( 'POST', 'https://example.com/pages/1', json={'a': ['b', 'c', 'd']}, headers={ 'Content-Type': 'application/json', } ) prepped = request.prepare() assert pformat(prepped, width=999) == """\ requests.PreparedRequest( method='POST', url='https://example.com/pages/1', headers=requests.structures.CaseInsensitiveDict({ 'Content-Length': '22', 'Content-Type': 'application/json' }), body=b'{"a": ["b"' # ... and 12 more bytes )"""
Example #14
Source File: test_mock_submission.py From dwave-cloud-client with Apache License 2.0 | 6 votes |
def choose_reply(path, replies, statuses=None, date=None): """Choose the right response based on the path and make a mock response.""" if statuses is None: statuses = collections.defaultdict(lambda: iter([200])) if date is None: date = datetime_in_future(0) if path in replies: response = mock.Mock(['json', 'raise_for_status', 'headers']) response.status_code = next(statuses[path]) response.json.side_effect = lambda: json.loads(replies[path]) response.headers = CaseInsensitiveDict({'Date': date.isoformat()}) def raise_for_status(): if not 200 <= response.status_code < 300: raise HTTPError(response.status_code) response.raise_for_status = raise_for_status return response else: raise NotImplementedError(path)
Example #15
Source File: requests_cache.py From foxford_courses with MIT License | 6 votes |
def build_response(self, req, resp): response = CachedResponse() response.status_code = getattr(resp, "status", None) response.headers = CaseInsensitiveDict(getattr(resp, "headers", {})) response.encoding = get_encoding_from_headers(response.headers) response.raw = resp response.reason = resp.reason if isinstance(req.url, bytes): response.url = req.url.decode("utf-8") else: response.url = req.url extract_cookies_to_jar(response.cookies, req, resp) response.request = req response.connection = self return response
Example #16
Source File: test_client.py From eventbrite-sdk-python with Apache License 2.0 | 6 votes |
def test_api_get(self): eventbrite = Eventbrite(OAUTH_TOKEN) payload = eventbrite.api("get", "/users/me/", {}) self.assertEqual( sorted([u'id', u'image_id', u'first_name', u'last_name', u'emails', u'name']), sorted(payload.keys()) ) self.assertEqual( payload.resource_uri, EVENTBRITE_API_URL + 'users/me/' ) self.assertTrue(payload.ok) self.assertTrue(isinstance(payload.elapsed, timedelta)) self.assertTrue(isinstance(payload.headers, CaseInsensitiveDict)) self.assertFalse( 'content-type' in payload.request.headers )
Example #17
Source File: gssapi_.py From requests-gssapi with ISC License | 6 votes |
def __init__(self, response): super(SanitizedResponse, self).__init__() self.status_code = response.status_code self.encoding = response.encoding self.raw = response.raw self.reason = response.reason self.url = response.url self.request = response.request self.connection = response.connection self._content_consumed = True self._content = "" self.cookies = cookiejar_from_dict({}) self.headers = CaseInsensitiveDict() self.headers['content-length'] = '0' for header in ('date', 'server'): if header in response.headers: self.headers[header] = response.headers[header]
Example #18
Source File: custom_crypto.py From aliyun-oss-python-sdk with MIT License | 6 votes |
def build_header_for_upload_part(self, headers=None): if not isinstance(headers, CaseInsensitiveDict): headers = CaseInsensitiveDict(headers) if 'content-md5' in headers: headers[OSS_CLIENT_SIDE_ENCRYPTION_UNENCRYPTED_CONTENT_MD5] = headers['content-md5'] del headers['content-md5'] if 'content-length' in headers: headers[OSS_CLIENT_SIDE_ENCRYPTION_UNENCRYPTED_CONTENT_LENGTH] = headers['content-length'] del headers['content-length'] self.plain_key = None self.plain_iv = None return headers
Example #19
Source File: httmock.py From django-wham with MIT License | 5 votes |
def response(status_code=200, content='', headers=None, reason=None, elapsed=0, request=None): res = requests.Response() res.status_code = status_code if isinstance(content, dict): if sys.version_info[0] == 3: content = bytes(json.dumps(content), 'utf-8') else: content = json.dumps(content) res._content = content res._content_consumed = content res.headers = structures.CaseInsensitiveDict(headers or {}) res.reason = reason res.elapsed = datetime.timedelta(elapsed) res.request = request if hasattr(request, 'url'): res.url = request.url if isinstance(request.url, bytes): res.url = request.url.decode('utf-8') if 'set-cookie' in res.headers: res.cookies.extract_cookies(cookies.MockResponse(Headers(res)), cookies.MockRequest(request)) # normally this closes the underlying connection, # but we have nothing to free. res.close = lambda *args, **kwargs: None return res
Example #20
Source File: response.py From pyspider with Apache License 2.0 | 5 votes |
def rebuild_response(r): response = Response( status_code=r.get('status_code', 599), url=r.get('url', ''), headers=CaseInsensitiveDict(r.get('headers', {})), content=r.get('content', ''), cookies=r.get('cookies', {}), error=r.get('error'), traceback=r.get('traceback'), time=r.get('time', 0), orig_url=r.get('orig_url', r.get('url', '')), js_script_result=r.get('js_script_result'), save=r.get('save'), ) return response
Example #21
Source File: schemas.py From schemathesis with MIT License | 5 votes |
def __getitem__(self, item: str) -> CaseInsensitiveDict: return self.endpoints[item]
Example #22
Source File: utils.py From http-observatory with Mozilla Public License 2.0 | 5 votes |
def parse_http_equiv_headers(html: str) -> CaseInsensitiveDict: http_equiv_headers = CaseInsensitiveDict() # Try to parse the HTML try: soup = bs(html, 'html.parser') except: return http_equiv_headers # Find all the meta tags metas = soup.find_all('meta') for meta in metas: if meta.has_attr('http-equiv') and meta.has_attr('content'): # Add support for multiple CSP policies specified via http-equiv # See issue: https://github.com/mozilla/http-observatory/issues/266 # Note that this is so far only done for CSP and not for other types # of http-equiv if (meta.get('http-equiv', '').lower().strip() == 'content-security-policy' and 'Content-Security-Policy' in http_equiv_headers): http_equiv_headers['Content-Security-Policy'] += '; ' + meta.get('content') else: http_equiv_headers[meta.get('http-equiv')] = meta.get('content') # Technically not HTTP Equiv, but I'm treating it that way elif meta.get('name', '').lower().strip() == 'referrer' and meta.has_attr('content'): http_equiv_headers['Referrer-Policy'] = meta.get('content') return http_equiv_headers
Example #23
Source File: test_SwiftAccountUsage.py From prometheus-openstack-exporter with GNU General Public License v3.0 | 5 votes |
def test__get_account_usage(self, _config, _requests_head, _SwiftAccountUsage__get_account_ring): s = poe.SwiftAccountUsage() s.account_ring.get_nodes.return_value = (26701, [ {'device': 'sdb', 'id': 0, 'ip': '10.24.0.18', 'meta': u'', 'port': 6002, 'region': 1, 'replication_ip': '10.24.0.18', 'replication_port': 6002, 'weight': 100.0, 'zone': 1}, {'device': 'sdd', 'id': 50, 'ip': '10.24.0.71', 'meta': u'', 'port': 6002, 'region': 1, 'replication_ip': '10.24.0.71', 'replication_port': 6002, 'weight': 180.0, 'zone': 3}, {'device': 'sdi', 'id': 59, 'ip': '10.24.0.72', 'meta': u'', 'port': 6002, 'region': 1, 'replication_ip': '10.24.0.72', 'replication_port': 6002, 'weight': 360.0, 'zone': 2} ]) response_mock = Mock() response_mock.configure_mock( status_code=204, headers=CaseInsensitiveDict({'x-account-bytes-used': '368259416'}), ) _requests_head.return_value = response_mock # Assert that _get_account_ring does what we expect. self.assertEqual(s._get_account_usage('AUTH_12bb569bf909441b90791482ae6f9ca9'), 368259416) # Assert that _get_account_ring did it in the manner we expected. s.account_ring.get_nodes.assert_called_once_with(account='AUTH_12bb569bf909441b90791482ae6f9ca9') poe.requests.head.assert_called_once() self.assertTrue(poe.requests.head.call_args in [ call('http://10.24.0.18:6002/sdb/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'), call('http://10.24.0.71:6002/sdd/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'), call('http://10.24.0.72:6002/sdi/26701/AUTH_12bb569bf909441b90791482ae6f9ca9'), ])
Example #24
Source File: serializer.py From ws-backend-community with GNU General Public License v3.0 | 5 votes |
def __serialize_requests_response(self, to_serialize): """ Serialize the contents of the given requests library response object to a JSON dictionary. A response is populated by the requests adapter build_response method as follows: response = Response() response.status_code = getattr(resp, 'status', None) response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {})) response.encoding = get_encoding_from_headers(response.headers) response.raw = resp response.reason = response.raw.reason if isinstance(req.url, bytes): response.url = req.url.decode('utf-8') else: response.url = req.url extract_cookies_to_jar(response.cookies, req, resp) response.request = req response.connection = self return response :param to_serialize: The response object to serialize. :return: A JSON object representing the contents of the given response. """ return { "status_code": to_serialize.status_code, "headers": dict(to_serialize.headers), "encoding": to_serialize.encoding, "content": to_serialize.content, "reason": to_serialize.reason, "url": to_serialize.url, "request": self.encode(to_serialize.request), "__class_type": get_import_path_for_type(to_serialize), } # Properties
Example #25
Source File: response.py From pyspider with Apache License 2.0 | 5 votes |
def __init__(self, status_code=None, url=None, orig_url=None, headers=CaseInsensitiveDict(), content='', cookies=None, error=None, traceback=None, save=None, js_script_result=None, time=0): if cookies is None: cookies = {} self.status_code = status_code self.url = url self.orig_url = orig_url self.headers = headers self.content = content self.cookies = cookies self.error = error self.traceback = traceback self.save = save self.js_script_result = js_script_result self.time = time
Example #26
Source File: tests.py From rapidpro-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, status_code, content=None, headers=None): self.status_code = status_code self.content = content or "" self.headers = CaseInsensitiveDict() if headers: self.headers.update(headers)
Example #27
Source File: network.py From federation with BSD 3-Clause "New" or "Revised" License | 5 votes |
def send_document(url, data, timeout=10, *args, **kwargs): """Helper method to send a document via POST. Additional ``*args`` and ``**kwargs`` will be passed on to ``requests.post``. :arg url: Full url to send to, including protocol :arg data: Dictionary (will be form-encoded), bytes, or file-like object to send in the body :arg timeout: Seconds to wait for response (defaults to 10) :returns: Tuple of status code (int or None) and error (exception class instance or None) """ logger.debug("send_document: url=%s, data=%s, timeout=%s", url, data, timeout) headers = CaseInsensitiveDict({ 'User-Agent': USER_AGENT, }) if "headers" in kwargs: # Update from kwargs headers.update(kwargs.get("headers")) kwargs.update({ "data": data, "timeout": timeout, "headers": headers }) try: response = requests.post(url, *args, **kwargs) logger.debug("send_document: response status code %s", response.status_code) return response.status_code, None except RequestException as ex: logger.debug("send_document: exception %s", ex) return None, ex
Example #28
Source File: core.py From requests-racer with GNU General Public License v3.0 | 5 votes |
def build_response_into(self, response, req, urllib3_resp): """Same as requests.adapters.HTTPAdapter.build_response, but writes into a provided requests.Response object instead of creating a new one. """ # Fallback to None if there's no status_code, for whatever reason. response.status_code = getattr(urllib3_resp, 'status', None) # Make headers case-insensitive. response.headers = CaseInsensitiveDict( getattr(urllib3_resp, 'headers', {}) ) # Set encoding. response.encoding = get_encoding_from_headers(response.headers) response.raw = urllib3_resp response.reason = response.raw.reason if isinstance(req.url, bytes): response.url = req.url.decode('utf-8') else: response.url = req.url # Add new cookies from the server. extract_cookies_to_jar(response.cookies, req, urllib3_resp) # Give the Response some context. response.request = req response.connection = self
Example #29
Source File: http.py From memorious with MIT License | 5 votes |
def headers(self): if self._headers is None and self.response: self._headers = self.response.headers return self._headers or CaseInsensitiveDict()
Example #30
Source File: vxapi.py From VxAPI with GNU General Public License v3.0 | 5 votes |
def get_current_key_data(self): current_time = int(time.time()) if self.cache_disabled is False and os.path.exists(self.current_key_sess_cache_file_path): file_handler = open(self.current_key_sess_cache_file_path, 'r') cache_content = json.load(file_handler) if cache_content['timestamp'] > current_time - 86400: return cache_content['response'], CaseInsensitiveDict(cache_content['headers']) current_key_cli_object = self.check_current_key() self.write_current_key_data_to_cache(current_key_cli_object) return current_key_cli_object.api_object.get_response_json(), current_key_cli_object.api_object.get_headers()