I am starting with Airflow. I need to get an access token from my PostgreSQL database, then I have to query an API with the SimpleHttpOperator function using that access token.
This is my code:
from airflow.models import DAG
from airflow.providers.http.operators.http import SimpleHttpOperator
from airflow.hooks.postgres_hook import PostgresHook
from airflow.operators.python import PythonOperator
from datetime import datetime
import json
default_args = {
'start_date':datetime(2021, 1, 1)
}
def _get_access_token():
request = "SELECT access_token FROM access_token"
postgres_hook = PostgresHook(postgres_conn_id="postgres_default")
connection = postgres_hook.get_conn()
cursor = connection.cursor()
cursor.execute(request)
jobs = cursor.fetchall()
access_token = ([i[0] for i in jobs])
return access_token
with DAG('get_broadworks_subscribers', schedule_interval='@once',
default_args = default_args,
catchup=False) as dag:
# Tasks
get_access_token = PythonOperator(
task_id='get_access_token',
python_callable=_get_access_token
)
get_subscribers_list = SimpleHttpOperator(
task_id = 'get_subscribers_list',
http_conn_id = 'webex',
endpoint = 'v1/broadworks/subscribers/',
method = 'GET',
authorization = "Bearer" + " " + access_token[0],
headers = {
"Authorization": "authorization"
},
response_filter = lambda response: json.loads(response.text),
log_response = True
)
get_access_token >> get_subscribers_list
I get the following error:
authorization = "Bearer" + " " + access_token[0],
NameError: name 'access_token' is not defined
I hope you can give me a hand, thank you very much in advance.