|
1 | 1 | import allure
|
2 | 2 | import requests
|
| 3 | +import yaml |
| 4 | +import os.path |
| 5 | + |
3 | 6 | from Library.variable import Var
|
4 | 7 | from Library.store import Store
|
5 | 8 |
|
6 | 9 |
|
7 | 10 | class Api:
|
8 |
| - URL = None |
9 |
| - endpoint = None |
10 |
| - params = None |
11 |
| - response = None |
12 |
| - |
13 |
| - def __init__(self, url): |
14 |
| - Api.URL = url |
15 |
| - |
16 |
| - @classmethod |
17 |
| - def get(cls, **kwargs): |
18 |
| - response = requests.get(Api.URL + Api.endpoint, Api.params, **kwargs) |
19 |
| - allure.step("Response for Get with " + Api.endpoint) |
20 |
| - allure.step("Get request Prams " + str(Api.params)) |
21 |
| - allure.step("Other get Args " + str(**kwargs)) |
22 |
| - allure.attach(response.status_code, name="Response Status Code") |
23 |
| - allure.attach(response.headers, name="Response Headers") |
24 |
| - allure.attach(response.text, name="Response Text") |
25 |
| - allure.attach(response.json(), name="Response JSON") |
26 |
| - Api.response = response |
27 |
| - |
28 |
| - @classmethod |
29 |
| - def set_end_point(cls, endpoint): |
30 |
| - Api.endpoint = endpoint |
31 |
| - |
32 |
| - @classmethod |
33 |
| - def set_params(cls, params): |
34 |
| - Api.params = params |
| 11 | + |
| 12 | + @staticmethod |
| 13 | + def get(endpoint, params=None, **kwargs): |
| 14 | + url = Var.current("URL") |
| 15 | + with allure.step("Get request with the url " + url + endpoint): |
| 16 | + response = requests.get(url + endpoint, params, **kwargs) |
| 17 | + with allure.step("Response for Get with " + endpoint + " " + str(params)): |
| 18 | + allure.attach(str(**kwargs), name="Other arguments passed for get request") |
| 19 | + allure.attach(str(response.status_code), name="Response Status Code") |
| 20 | + allure.attach(str(response.headers), name="Response Headers") |
| 21 | + allure.attach(str(response.text), name="Response Text") |
| 22 | + allure.attach(str(response.json()), name="Response JSON") |
| 23 | + Store.current_response = response |
| 24 | + return response |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def verify_response_code(status_code): |
| 28 | + with allure.step("Validating the status code for the request"): |
| 29 | + result = Store.current_response.status_code == status_code |
| 30 | + assert (result is True), "Response status code is not matched, \n" \ |
| 31 | + "expected: " + status_code + "\n" \ |
| 32 | + "actual: " + Store.current_response.status_code |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def create_file_if_not_present(file_path): |
| 36 | + if Var.env("snap") == "1": |
| 37 | + if not os.path.isfile(file_path): |
| 38 | + with open(file_path, 'w') as file: |
| 39 | + documents = yaml.dump({'created_from': 'snap mode in pyrest framework'}, file) |
| 40 | + print(documents) |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def dump_in_dynamic_variable_file(file_path, params): |
| 44 | + if Var.env("snap") == "1": |
| 45 | + with open(file_path, 'w') as file: |
| 46 | + documents = yaml.dump(params, file) |
| 47 | + print(documents) |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def verify_response_json(file_name, key_name): |
| 51 | + expected_json = {} |
| 52 | + file_path = Var.root_path() + "/Data/DynamicData/" + file_name |
| 53 | + Api.create_file_if_not_present(file_path) |
| 54 | + with open(file_path) as file: |
| 55 | + yaml_load = yaml.load(file, Loader=yaml.FullLoader) |
| 56 | + with allure.step("Validating the response json with stored value"): |
| 57 | + allure.attach.file(file_path, name=file_name, attachment_type=allure.attachment_type.TEXT) |
| 58 | + allure.attach(str(Store.current_response.json()), name="Response JSON") |
| 59 | + try: |
| 60 | + expected_json = yaml_load[key_name] |
| 61 | + if Var.env("snap") == "1": |
| 62 | + yaml_load[key_name] = Store.current_response.json() |
| 63 | + Api.dump_in_dynamic_variable_file(file_path, yaml_load) |
| 64 | + except Exception as e: |
| 65 | + allure.attach(str(e), name="Error while fetching the key from expected yaml file") |
| 66 | + if Var.env("snap") == "1": |
| 67 | + yaml_load[key_name] = Store.current_response.json() |
| 68 | + Api.dump_in_dynamic_variable_file(file_path, yaml_load) |
| 69 | + assert (expected_json == Store.current_response.json()), "Expected Json doesn't match with stored json" \ |
| 70 | + "file \nExpected: " + str(expected_json) + "\n" \ |
| 71 | + "Actual response: " \ |
| 72 | + "" + str(Store.current_response.json()) |
0 commit comments