def update_user(self, id, name, job): """ Updates the user info. using PUT call to endpoint based on input name and job """ payload = { "name": name, "job": job } logger.debug("Attempting to update user {} using payload {}".format(id,payload)) return self.api_object.put(self.end_point+'{}'.format(id),payload)
def create_user(self, name, job): """ Creates the user using POST call to endpoint based on name and job input """ payload = { "name": name, "job": job } logger.debug("Attempting to create user with payload {}".format(payload)) return self.api_object.post(self.end_point, json_obj=payload)
def delete(self, end_point, headers=None): """ Purpose: Makes DELETE REST call based on the input arguments and returns API response :request_url: Endpoint in the request :headers: Any headers to make get request :return:dictionary having response json and status code of the GET request """ request_url = self.base_uri + self.base_path + end_point resp = requests.delete(request_url, headers=headers, verify=False) result = { "status_code": resp.status_code, "headers":resp.headers } logger.debug("The response of DELETE api {}".format(result)) return result
def put(self, end_point, json_obj, headers=None): """ Purpose: Makes PUT REST call based on the input arguments and returns API response :end_point: Endpoint in the request :param : Path parameters :headers: Any headers to make get request :return:Dictionary object having response json and status code of the GET request """ request_url = self.base_uri + self.base_path + end_point resp = requests.put(request_url, data=json.dumps(json_obj), headers=headers, verify=False) result = { "response_body": resp.json(), "status_code": resp.status_code, "headers":resp.headers } logger.debug("The response of PUT api {}".format(result)) return result
def get(self, end_point, params=None,headers=None): """ Purpose: Makes GET REST call based on the input arguments and returns API response :request_url: endpoint in request :params: Path parameters :headers: headers in request :return:Dictionary object having response json and status code of the request """ request_url = self.base_uri + self.base_path + end_point resp = requests.get(request_url, params=params, headers=None, verify=False) result = { "response_body": resp.json(), "status_code": resp.status_code, "headers":resp.headers } logger.debug("The response of GET api {}".format(result)) return result
def assert_equal(actual_output, expected_output, msg): logger.debug("actual output is:") logger.debug(actual_output) logger.debug("expected output is:") logger.debug(expected_output) try: assert actual_output == expected_output, msg except AssertionError as e: logger.error(traceback.format_exc(), "error") raise e
def post(self, end_point, json_obj=None, params=None, headers=None): """ Purpose: Makes POST REST call and returns API response :end_point: Endpoint in the request :json_obj: JSON payload :params: Path parameters :headers: Any headers to make GET request :return: Dictionary object having response json and status code of the POST request """ request_url = self.base_uri + self.base_path + end_point resp = requests.post(request_url, params=params,json=json_obj,verify=False) result = { "response_body": resp.json(), "status_code": resp.status_code, "headers":resp.headers } logger.debug("The response of POST api {}".format(result)) return result
def inner(*args, **kwargs): start_time = time.perf_counter() if args and kwargs: logger.debug('INFO: Started executing {}({},{}) '.format( func.__qualname__, args, kwargs)) elif args: logger.debug('INFO: Started executing {}({})'.format( func.__qualname__, args)) elif kwargs: logger.debug('INFO: Started executing {}({})'.format( func.__qualname__, kwargs)) else: logger.debug('INFO: Started executing {}()'.format( func.__qualname__)) retval = func(*args, **kwargs) end_time = time.perf_counter() logger.debug("Info: Completed executing {} in {} seconds ".format( func.__qualname__, end_time)) return retval
def is_last_name_present(self, last_name): """ Confirms whether last name is present in application """ resp = self.api_object.get("users") logger.debug("Checking if last name {} is present?".format(last_name)) for record in resp.get("response_body").get('data'): if record.get('last_name') == last_name.strip(): logger.debug("Last name {} is present".format(last_name)) return True logger.debug("Last name {} is NOT present".format(last_name)) return False
def is_email_present(self, email): """ Confirms whether email address is present in application """ resp = self.api_object.get("users") logger.debug("Checking if email {} is present?".format(email)) for record in resp.get("response_body").get('data'): if record.get('email') == email.strip(): logger.debug("Email address {} is present".format(email)) return True logger.debug("Email address {} is NOT present".format(email)) return False
def get_user_by_id(self, id): """ Gets the user information based on id """ logger.debug("Fetching user id - {} ".format(id)) return self.api_object.get(self.end_point + "{}".format(id))
def get_all_users(self): """ Fetches all the users in the system """ logger.debug("Attempting to fetch all the users ") return self.api_object.get("users")
def delete_user(self, id): """ Deletes the user info. based on input id """ logger.debug("Attempting to delete user with id - {} ".format(id)) return self.api_object.delete(self.end_point+"{}".format(id))