How to use _clear_request_headers method in autotest

Best Python code snippet using autotest_python

rest_client.py

Source: rest_client.py Github

copy

Full Screen

...11 headers = rpc_client_lib.authorization_headers(getpass.getuser(), uri)12 headers['Content-Type'] = 'application/​json'13 _request_headers[server] = headers14 return headers15def _clear_request_headers(uri):16 server = urlparse.urlparse(uri)[0:2]17 if server in _request_headers:18 del _request_headers[server]19def _site_verify_response_default(headers, response_body):20 return headers['status'] != '401'21class RestClientError(Exception):22 pass23class ClientError(Exception):24 pass25class ServerError(Exception):26 pass27class Response(object):28 def __init__(self, httplib_response, httplib_content):29 self.status = int(httplib_response['status'])30 self.headers = httplib_response31 self.entity_body = httplib_content32 def decoded_body(self):33 return simplejson.loads(self.entity_body)34 def __str__(self):35 return '\n'.join([str(self.status), self.entity_body])36class Resource(object):37 def __init__(self, representation_dict, http):38 self._http = http39 assert 'href' in representation_dict40 for key, value in representation_dict.iteritems():41 setattr(self, str(key), value)42 def __repr__(self):43 return 'Resource(%r)' % self._representation()44 def pprint(self):45 # pretty-print support for debugging/​interactive use46 pprint.pprint(self._representation())47 @classmethod48 def load(cls, uri, http=None):49 if not http:50 http = httplib2.Http()51 directory = cls({'href': uri}, http)52 return directory.get()53 def _read_representation(self, value):54 # recursively convert representation dicts to Resource objects55 if isinstance(value, list):56 return [self._read_representation(element) for element in value]57 if isinstance(value, dict):58 converted_dict = dict((key, self._read_representation(sub_value))59 for key, sub_value in value.iteritems())60 if 'href' in converted_dict:61 return type(self)(converted_dict, http=self._http)62 return converted_dict63 return value64 def _write_representation(self, value):65 # recursively convert Resource objects to representation dicts66 if isinstance(value, list):67 return [self._write_representation(element) for element in value]68 if isinstance(value, dict):69 return dict((key, self._write_representation(sub_value))70 for key, sub_value in value.iteritems())71 if isinstance(value, Resource):72 return value._representation()73 return value74 def _representation(self):75 return dict((key, self._write_representation(value))76 for key, value in self.__dict__.iteritems()77 if not key.startswith('_')78 and not callable(value))79 def _do_request(self, method, uri, query_parameters, encoded_body):80 uri_parts = [uri]81 if query_parameters:82 if '?' in uri:83 uri_parts += '&'84 else:85 uri_parts += '?'86 uri_parts += urllib.urlencode(query_parameters, doseq=True)87 full_uri = ''.join(uri_parts)88 if encoded_body:89 entity_body = simplejson.dumps(encoded_body)90 else:91 entity_body = None92 logging.debug('%s %s', method, full_uri)93 if entity_body:94 logging.debug(entity_body)95 site_verify = utils.import_site_function(96 __file__, 'autotest_lib.frontend.shared.site_rest_client',97 'site_verify_response', _site_verify_response_default)98 headers, response_body = self._http.request(99 full_uri, method, body=entity_body,100 headers=_get_request_headers(uri))101 if not site_verify(headers, response_body):102 logging.debug('Response verification failed, clearing headers and '103 'trying again:\n%s', response_body)104 _clear_request_headers(uri)105 headers, response_body = self._http.request(106 full_uri, method, body=entity_body,107 headers=_get_request_headers(uri))108 logging.debug('Response: %s', headers['status'])109 return Response(headers, response_body)110 def _request(self, method, query_parameters=None, encoded_body=None):111 if query_parameters is None:112 query_parameters = {}113 response = self._do_request(method, self.href, query_parameters,114 encoded_body)115 if 300 <= response.status < 400: # redirection116 return self._do_request(method, response.headers['location'],117 query_parameters, encoded_body)118 if 400 <= response.status < 500:...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Scala Testing: A Comprehensive Guide

Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.

What Agile Testing (Actually) Is

So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.

How To Choose The Right Mobile App Testing Tools

Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools

A Complete Guide To CSS Houdini

As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????

Appium Testing Tutorial For Mobile Applications

The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful