import glob import json import re import requests class GraphQlGateway: def __init__(self, app): self.gateway_url = "https://qa-graphql-router.theorchard.io/graphql" self.application = app f = open("qa_testing_utils/graphQlQueryGenerator/application_headers.json") self.default_headers = json.load(f)[app] f = open('user-data.json') self.user_data = json.load(f)[app] self.gql_files = glob.glob('graphQl-refresh-app/**/*.gql', recursive=True) def get_headers(self, var_info): headers = self.default_headers.copy() if "customHeaders" in var_info.keys(): headers.update(var_info["customHeaders"]) user_details = self.user_data[var_info["user"]] headers["Orchard-Identity-id"] = user_details["Orchard-Identity-Id"] if "Orchard-Profile-Id" not in headers.keys(): headers["Orchard-Profile-Id"] = user_details["Orchard-Profile-Id"] return headers # Checks if file extension is specified to handle queries from non .gql files # and returns filename @staticmethod def get_filename(name): extension_regex = re.compile(".*(\\..*)") if extension_regex.match(name): extension = extension_regex.match(name)[1] return name, extension else: return f"{name}.gql", ".gql" # Extracts query text from file @staticmethod def extract_query_text(file): query_text = "" import_text = True for line in file: if 'query' in line: import_text = False if not import_text: if '`' in line: break stripped_line = line.rstrip('\n').lstrip(' ') + ' ' query_text += stripped_line return query_text def get_query(self, name): filename, extension = self.get_filename(name) r = re.compile(f".*{filename}") if extension != '.gql': file_path = glob.glob(f'graphQl-refresh-app/**/{filename}', recursive=True)[0] else: file_path = list(filter(r.match, self.gql_files))[0] with open(file_path, 'r') as file: if extension == '.gql': return file.read() return self.extract_query_text(file) @staticmethod def format_array_query(var_set, query): body = [] for variables in var_set["variables"]: single_query = {"query": query, "variables": variables} body.append(single_query) return body @staticmethod def format_response_object(resp, query_info, var_set): formatted_response = {"page": var_set["page"], "response": resp, "operationName": query_info["operationName"], "variables": var_set["variables"]} if "ignoreVariables" in var_set: formatted_response["ignoreVariables"] = var_set["ignoreVariables"] if "customHeaders" in var_set: formatted_response["customHeaders"] = var_set["customHeaders"] if "forceProfileId" in var_set: resp["data"]["identityById"]["profiles"][0]["profileId"] = var_set["forceProfileId"] return formatted_response def get_response(self, query_info, var_set): headers = self.get_headers(var_set) query = self.get_query(query_info["fileName"]) if "arrayQuery" in var_set: body = self.format_array_query(var_set, query) else: body = {'query': query, "variables": var_set["variables"]} response = self.run_query(headers, body) if response.status_code != 200: response = self.run_query(headers, body) response.raise_for_status() data = json.loads(response.text) if "errors" in data: print(data) raise Exception("Error in graphQl query") return self.format_response_object(data, query_info, var_set) def run_query(self, headers, body): return requests.post( url=self.gateway_url, json=body, headers=headers ) def manual_query(self, var_set, file, variables): headers = self.get_headers(var_set) with open(file, 'r') as file: query = file.read() body = {'query': query, "variables": variables} response = self.run_query(headers, body) response.raise_for_status() return json.loads(response.text)['data']