Book a Demo

Author Topic: Automating Python Connection to Cloud_Repo.eap  (Read 5640 times)

Michal_

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Automating Python Connection to Cloud_Repo.eap
« on: February 22, 2024, 07:40:02 pm »
Hi,

I need to connect to the Cloud_Repo.eap through the server via Python
I have:
protocol, server, port, model_name, username, password.

I have been using local_path to connect to the Cloud_Repo.eap like this:
Code: [Select]
def openEARepo(EAPath):
    print("File path: {}".format(EAPath))
    try:
       EARepo = win32com.client.Dispatch('EA.Repository')
    except:
       print ("failure dispatch")
       sys.exit(2)
    print("Script starts")
    try:
       print("... opening EA Repo")
       EARepo.OpenFile(EAPath)
       print("EA Repo opened")
    except:
       print ("failure opening file")
       sys.exit(2)
    return EARepo
Example of local path:
Code: [Select]
ea_path = r'C:\Users\user\Sparx\Cloud_Repo.eap'
Now, I need to fully automate this process and run my Python code in the Azure Functions, to do this, I need to connect to the server, not the local path.
How can I connect to the server?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Automating Python Connection to Cloud_Repo.eap
« Reply #1 on: February 22, 2024, 11:37:32 pm »
If you connect to the server manually, and then save the connection as a shortcut, you get a textfile with the connection string.

You can either use that file directly, or use the connection string in the code.

Geert

Michal_

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Automating Python Connection to Cloud_Repo.eap
« Reply #2 on: February 23, 2024, 12:25:18 am »
I understand that I have no problem connecting to EA manually by passing the file directly.
I need to fully automate the process so the Python code will run through Azure Functions.
That means I can't pass the file directory.
I need to connect to the server but I do not how.
I know the protocol, server, port, model_name, username, and password.
How can I create this string connection?

I couldn't find any useful information online, so I asked ChatGPT...
Code: [Select]
def connect_to_sparx_cloud(username, password, model_name, server_url):
    # Endpoint to authenticate and obtain an access token
    auth_endpoint = f"{server_url}/authenticate"

    # Construct the URL to access your model
    model_url = f"{server_url}/models/{model_name}"

    # Make a request to authenticate and obtain an access token
    response = requests.post(auth_endpoint, json={"username": username, "password": password})

    # Check if authentication was successful
    if response.status_code == 200:
        access_token = response.json()["access_token"]
        print("Authentication successful. Access token:", access_token)

        # Use the access token to access your model
        headers = {"Authorization": f"Bearer {access_token}"}
        model_response = requests.get(model_url, headers=headers)

        # Check if model access was successful
        if model_response.status_code == 200:
            model_data = model_response.json()
            print("Model details:", model_data)
            return model_data
        else:
            print("Failed to access model. Status code:", model_response.status_code)
            return None
    else:
        print("Authentication failed. Status code:", response.status_code)
        return None
or
Code: [Select]
def connect_to_sparx_cloud(username, password, model_name, server, port, protocol):

    # Construct the base URL for the Sparx Cloud Web API
    base_url = f"{protocol}{server}:{port}/services/api"

    # Endpoint to authenticate and obtain an access token
    auth_endpoint = f"{base_url}/authenticate"

    # Construct the URL to access your model
    model_url = f"{base_url}/models/{model_name}"

    # Make a request to authenticate and obtain an access token
    response = requests.post(auth_endpoint, json={"username": username, "password": password})

    # Check if authentication was successful
    if response.status_code == 200:
        access_token = response.json()["access_token"]
        print("Authentication successful. Access token:", access_token)

        # Use the access token to access your model
        headers = {"Authorization": f"Bearer {access_token}"}
        model_response = requests.get(model_url, headers=headers)

        # Check if model access was successful
        if model_response.status_code == 200:
            model_data = model_response.json()
            print("Model details:", model_data)
            return model_data
        else:
            print("Failed to access model. Status code:", model_response.status_code)
            return None
    else:
        print("Authentication failed. Status code:", response.status_code)
        return None

However, it doesn't work. I don't know what a "token" is either.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Automating Python Connection to Cloud_Repo.eap
« Reply #3 on: February 23, 2024, 12:32:47 am »
You did not read my response completely.
If you create the shortcut file, you can open that file with a text editor, and you have the connection string you can use in OpenFile.

BTW, you can't simply run that code in an Azure Function. You are using the API of EA, which needs a full install of EA.
Compare it to automating Excel using the Excel API. That won't work either if you don't have office installed.

Geert

PS. Don't bother with ChatGPT, that's not even close to what you should be doing.

Michal_

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Automating Python Connection to Cloud_Repo.eap
« Reply #4 on: February 23, 2024, 06:21:44 pm »
Okay, now I understand. Thanks for your help Geert.