import requests
# API credentials
API_PUBLIC_KEY = "your_public_key"
API_PRIVATE_KEY = "your_private_key"
BASE_URL = "https://api.pingproxies.com/1.0/public"
# Headers for authentication
headers = {
    "X-API-Public-Key": API_PUBLIC_KEY,
    "X-API-Private-Key": API_PRIVATE_KEY,
    "Content-Type": "application/json"
}
def search_acls(proxy_user_id):
    """
    Search for all ACL entries for a specific proxy user.
    Args:
        proxy_user_id: The proxy user ID to search ACLs for
    Returns:
        List of ACL entries or None if failed
    """
    params = {"proxy_user_id": proxy_user_id}
    response = requests.get(
        f"{BASE_URL}/user/proxy_user_acl/search",
        params=params,
        headers=headers
    )
    if response.status_code != 200:
        print(f"Error searching ACLs: {response.status_code}")
        print(response.text)
        return None
    data = response.json()
    acls = data.get("data", [])
    print(f"\nFound {len(acls)} ACL entries for {proxy_user_id}:")
    for acl in acls:
        acl_id = acl.get("proxy_user_acl_id")
        service_id = acl.get("service_id")
        proxy_id = acl.get("proxy_id")
        if service_id:
            print(f"  - ACL {acl_id}: Service access to {service_id}")
        elif proxy_id:
            print(f"  - ACL {acl_id}: Proxy access to {proxy_id}")
    return acls
def add_service_access(proxy_user_id, service_id):
    """
    Add service access for a proxy user.
    Args:
        proxy_user_id: The proxy user ID
        service_id: The service ID to grant access to
    Returns:
        The created ACL ID or None if failed
    """
    payload = {
        "proxy_user_id": proxy_user_id,
        "service_id": service_id
    }
    response = requests.post(
        f"{BASE_URL}/user/proxy_user_acl/create",
        json=payload,
        headers=headers
    )
    if response.status_code != 201:
        print(f"Error adding service access: {response.status_code}")
        print(response.text)
        return None
    data = response.json()
    acl_id = data["created"][0]
    print(f"Successfully granted access to service {service_id}")
    return acl_id
def add_proxy_access(proxy_user_id, proxy_id):
    """
    Add proxy access for a proxy user.
    Args:
        proxy_user_id: The proxy user ID
        proxy_id: The proxy ID to grant access to
    Returns:
        The created ACL ID or None if failed
    """
    payload = {
        "proxy_user_id": proxy_user_id,
        "proxy_id": proxy_id
    }
    response = requests.post(
        f"{BASE_URL}/user/proxy_user_acl/create",
        json=payload,
        headers=headers
    )
    if response.status_code != 201:
        print(f"Error adding proxy access: {response.status_code}")
        print(response.text)
        return None
    data = response.json()
    acl_id = data["created"][0]
    print(f"Successfully granted access to proxy {proxy_id}")
    return acl_id
def remove_acl(acl_id):
    """
    Remove an ACL entry to revoke access.
    Args:
        acl_id: The ACL ID to delete
    Returns:
        True if successful, False otherwise
    """
    response = requests.delete(
        f"{BASE_URL}/user/proxy_user_acl/delete/{acl_id}",
        headers=headers
    )
    if response.status_code != 200:
        print(f"Error removing ACL: {response.status_code}")
        print(response.text)
        return False
    print(f"Successfully removed ACL {acl_id}")
    return True
def change_access_type_to_all(proxy_user_id):
    """
    Change a proxy user's access type to 'all' (unrestricted).
    This will also clear all existing ACL entries.
    Args:
        proxy_user_id: The proxy user ID
    Returns:
        True if successful, False otherwise
    """
    payload = {
        "proxy_user_access_type": "all",
        "clear_proxy_user_acl": True
    }
    response = requests.patch(
        f"{BASE_URL}/user/proxy_user/edit/{proxy_user_id}",
        json=payload,
        headers=headers
    )
    if response.status_code != 200:
        print(f"Error changing access type: {response.status_code}")
        print(response.text)
        return False
    print(f"Successfully changed {proxy_user_id} to unrestricted access")
    return True
# Example usage
if __name__ == "__main__":
    PROXY_USER_ID = "seo_team"
    print("=== Example 1: View Current ACLs ===")
    current_acls = search_acls(PROXY_USER_ID)
    print("\n=== Example 2: Add Service Access ===")
    add_service_access(PROXY_USER_ID, "API-NEW-SERVICE-001")
    print("\n=== Example 3: Add Multiple Services ===")
    for service_id in ["API-SERVICE-002", "API-SERVICE-003"]:
        add_service_access(PROXY_USER_ID, service_id)
    print("\n=== Example 4: View Updated ACLs ===")
    updated_acls = search_acls(PROXY_USER_ID)
    print("\n=== Example 5: Remove Specific ACL ===")
    if updated_acls and len(updated_acls) > 0:
        first_acl_id = updated_acls[0].get("proxy_user_acl_id")
        remove_acl(first_acl_id)
    print("\n=== Example 6: Change to Unrestricted Access ===")
    # Uncomment to change to unrestricted access (removes all ACLs)
    # change_access_type_to_all(PROXY_USER_ID)