import requests
import warnings

warnings.filterwarnings("ignore", message="Unverified HTTPS request")

# Define initial authentication URL
auth_url = 'https://3.143.25.10/nokia-altiplano-ac/rest/auth/login'

# Define username and password
username = "adminuser"
password = "SBAc0nnect!"

# Define the payload for the POST request
payload = {
    "ibn:intent": {
        "intent-specific-data": {
            "ont:ont": {
                "ont-type": "G040PQ_v2",
                "auto": [None],
                "from-device-mapping": [None],
                "onu-service-profile": "default",
                "pon-type": "gpon",
                "expected-serial-number": "ALCLB25FCDF5",
                "uni-service-configuration": [
                    {
                        "service-profile": "default",
                        "uni-id": "LAN1"
                    }
                ],
                "fiber-name": "Concordia_CAGE1"
            }
        },
        "intent-type": "ont",
        "target": "Little-Rhody-ONT",
        "required-network-state": "active",
        "intent-type-version": 6
    }
}

# Create a session object
session = requests.Session()

try:
    # Perform initial authentication request
    response = session.post(auth_url, auth=(username, password), verify=False)
    response.raise_for_status()  # Raise an exception for 4xx or 5xx errors

    if response.status_code == 200:
        data = response.json()
        access_token = data.get('accessToken')
        print("Access token:", access_token)

        # Define the URLs for subsequent requests
        base_url = 'https://3.143.25.10/nokia-altiplano-ac/rest/restconf/data/ibn:ibn?'

        # Add the access token and content type to the headers for authorization
        headers = {
            'Authorization': f'Bearer {access_token}',
            'Content-Type': 'application/yang-data+json'
        }

        # Make requests with the session object
        response_trigger = session.post(base_url, headers=headers, json=payload, verify=False)

        # Print response status code and text
        print("Response status code:", response_trigger.status_code)
        print("Response text:", response_trigger.text)

    else:
        print("Unexpected response status code:", response.status_code)

except requests.exceptions.HTTPError as e:
    if e.response.status_code == 401:
        print("Error: Unauthorized - Please check your credentials.")
    else:
        print("HTTP Error:", e)
except requests.exceptions.RequestException as e:
    print("Error:", e)

