Best Python code snippet using autotest_python
rest_client.py
Source:rest_client.py
...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:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!