Created
December 31, 2020 14:49
-
-
Save brownhash/41f4e621e801e33a786e7c525bc3f941 to your computer and use it in GitHub Desktop.
Trigger airflow dag using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import time | |
import json | |
class AirflowTrigger(object): | |
def __init__(self, airflow_addr, dag_id, trigger_conf): | |
self.airflow_addr = airflow_addr | |
self.dag_id= dag_id | |
self.trigger_conf = trigger_conf | |
def exec(self): | |
time_stamp = time.strftime('%Y-%m-%dT%H:%M:%SZ') | |
payload = { | |
"execution_date": time_stamp, | |
"conf": self.trigger_conf | |
} | |
headers = { | |
'Cache-Control': 'no-cache', | |
'Content-Type': 'application/json' | |
} | |
url = os.path.join(self.airflow_addr, 'api/experimental/dags/{}/dag_runs'.format(self.dag_id)) | |
resp = requests.post(url, headers=headers, data=json.dumps(payload)) | |
return json.loads(resp.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment