Reading Apache Airflow active connections programatically

Viewed 895

I have set up the below in Apache Airflow Admin --> Connections.

enter image description here

How do I read these values programmatically inside my DAG?

def check_email_requests():
    conn = Connection(conn_id="artnpics_api_calls")
    
    print(conn)
    
    hostname = conn.host
    login_name = conn.login
    login_password = conn.password
    port_number = conn.port
    
    print("hostname = " + hostname + "; Login name: " + login_name + "; password = " + login_password + " ; port number = " + port_number)
    
    request_api = hostname + ":" + port_number
    print("request api " + request_api)
    
    result = requests.get(request_api, auth=(login_name, login_password)).json()
    print(result)
    print("done with check_email_requests")
    
    return False

The above obviously did not work, and I couldn't find any information on how to read from the connections (there is numerous article on how to create one programmatically). My objective is to read API connection and authentication information programmatically and invoke the call, rather than hard coding them.

  • Rhonald
1 Answers

You can do:

from airflow.hooks.base import BaseHook
conn = BaseHook.get_connection("artnpics_api_calls")
hostname = conn.host
login_name = conn.login
login_password = conn.password
port_number = conn.port
Related