import json
import random
import string
import requests
session = requests.Session()
session.trust_env = False

def load_swagger_json(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        swagger_data = json.load(file)
    return swagger_data

def extract_endpoints(swagger_data):
    paths = swagger_data.get('paths', {})
    endpoints = []
    for path, methods in paths.items():
        for method, details in methods.items():
            endpoint = {
                'path': path,
                'method': method.upper(),
                'operationId': details.get('operationId', ''),
                'parameters': details.get('parameters', [])
    return endpoints

def replace_path_parameters(path, parameters):
    for param in parameters:
        if param['in'] == 'path':
            path = path.replace(f"{{{param['name']}}}", str(param.get('default', '')))
    return path

def generate_random_string(length):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for _ in range(length))

def generate_random_data(schema):
    if 'type' in schema:
        param_type = schema['type']
        if param_type == 'string':
            if 'format' in schema and schema['format'] == 'uuid':
                return str(uuid.uuid4())
                return generate_random_string(10)
        elif param_type == 'integer':
            return random.randint(1, 100)
        # Add more cases for other data types as needed
    return None

def execute_requests(base_url, endpoint, proxy=None):
    path = replace_path_parameters(endpoint['path'], endpoint['parameters'])
    url = f"{base_url}{path}"
    method = endpoint['method']

    # Assuming you have additional headers or parameters in your Swagger file
    # You may need to customize this part based on your Swagger specification
    headers = {'Content-Type': 'application/json',
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36'}  # Modify based on your headers
    params = {}  # Modify based on your query parameters
    data = {}  # Modify based on your request body

    for param in endpoint['parameters']:
        if param['in'] == 'header':
            headers[param['name']] = param.get('default', '')
        elif param['in'] == 'query':
            if 'schema' in param:
                params[param['name']] = generate_random_data(param['schema'])
        elif param['in'] == 'body':
            if 'schema' in param:
                # Modify based on your request body structure
                data = generate_random_data(param['schema'])

    # Make the request with proxy if provided
    proxies = {'http': proxy, 'https': proxy} if proxy else None
    response = session.request(method, url, headers=headers, params=params, json=data, proxies=proxies , verify=False)

    print(f"Endpoint: {path} - Method: {method} - Status Code: {response.status_code}")

if __name__ == "__main__":
    swagger_file_path = 'path/to/your/swagger.json'
    base_url = 'https://api.example.com'  # Replace with your API base URL
    proxy_url = 'http://your.proxy.server:port'  # Replace with your proxy URL, or None if no proxy is needed

    swagger_data = load_swagger_json(swagger_file_path)
    endpoints = extract_endpoints(swagger_data)

    for endpoint in endpoints:
        execute_requests(base_url, endpoint, proxy=proxy_url)