Best Python code snippet using autotest_python
rest_client.py
Source:rest_client.py
...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:119 raise ClientError(str(response))120 if 500 <= response.status < 600:121 raise ServerError(str(response))122 return response123 def _stringify_query_parameter(self, value):124 if isinstance(value, (list, tuple)):125 return ','.join(self._stringify_query_parameter(item)126 for item in value)127 return str(value)128 def _iterlists(self, mapping):129 """This effectively lets us treat dicts as MultiValueDicts."""130 if hasattr(mapping, 'iterlists'): # mapping is already a MultiValueDict131 return mapping.iterlists()132 return ((key, (value,)) for key, value in mapping.iteritems())133 def get(self, query_parameters=None, **kwarg_query_parameters):134 """135 @param query_parameters: a dict or MultiValueDict136 """137 query_parameters = copy.copy(query_parameters) # avoid mutating original138 if query_parameters is None:139 query_parameters = {}140 query_parameters.update(kwarg_query_parameters)141 string_parameters = datastructures.MultiValueDict()142 for key, values in self._iterlists(query_parameters):143 string_parameters.setlist(144 key, [self._stringify_query_parameter(value)145 for value in values])146 response = self._request('GET',147 query_parameters=string_parameters.lists())148 assert response.status == 200149 return self._read_representation(response.decoded_body())150 def get_full(self, results_limit, query_parameters=None,151 **kwarg_query_parameters):152 """153 Like get() for collections, when the full collection is expected.154 @param results_limit: maxmimum number of results to allow155 @raises ClientError if there are more than results_limit results.156 """157 result = self.get(query_parameters=query_parameters,158 items_per_page=results_limit,159 **kwarg_query_parameters)160 if result.total_results > results_limit:161 raise ClientError(162 'Too many results (%s > %s) for request %s (%s %s)'163 % (result.total_results, results_limit, self.href,164 query_parameters, kwarg_query_parameters))165 return result166 def put(self):167 response = self._request('PUT', encoded_body=self._representation())168 assert response.status == 200169 return self._read_representation(response.decoded_body())170 def delete(self):171 response = self._request('DELETE')172 assert response.status == 204 # no content173 def post(self, request_dict):174 # request_dict may still have resources in it175 request_dict = self._write_representation(request_dict)176 response = self._request('POST', encoded_body=request_dict)177 assert response.status == 201 # created...
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!!