import json import sys import os import urllib.parse from haralyzer import HarParser def main(args): if len(args) == 0: print("Usage: python main.py ") return 1 # Get the file path from the command line argument file_path = args[0] # Check if the file path is absolute or relative if not file_path.startswith('/'): # If the path is relative, make it absolute file_path = os.path.abspath(file_path) # Check if the file exists if not os.path.isfile(file_path): print(f"The specified file '{file_path}' does not exist.") return 1 # Create a HarParser instance with open(file_path, 'r') as f: har_parser = HarParser(json.loads(f.read())) # Iterate over the entries and save each request/response pair as a separate JSON file for page in har_parser.pages: for index, entry in enumerate(page.entries): request = entry['request'] response = entry['response'] request_url = request['url'] # Remove the host part from the URL parsed_url = urllib.parse.urlparse(request_url) hostname = parsed_url.netloc path = parsed_url.path query = parsed_url.query request_url = path if not query else f"{path}?{query}" if hostname == 'dev-proxy.apollo.stream' and (path.startswith('/apollo-api') or path.startswith('/dsp-api') or path.startswith('/gate-api') or path.startswith('/user-data-api')) and request['method'] == 'GET': wiremock_response = { "request": { "method": request['method'], "url": request_url }, "response": { "status": response['status'], "body": response['content'].get('text'), "headers": {} } } for header in response['headers']: name = header['name'] value = header['value'] wiremock_response['response']['headers'][name] = value with open(f'mappings/response_{index}.json', 'w') as response_file: json.dump(wiremock_response, response_file, indent=4) print("Conversion completed successfully.") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))