generated from interTwin-eu/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dags_airflow_ale_httpsensor.py
98 lines (61 loc) · 1.75 KB
/
dags_airflow_ale_httpsensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Example code for HttpSensor
"""
import airflow.utils.dates
from airflow.utils.log.logging_mixin import LoggingMixin
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from airflow.providers.http.sensors.http import HttpSensor
dag = DAG(
dag_id="httpsens_exmp",
start_date=airflow.utils.dates.days_ago(3),
schedule_interval=None,
description="A demonstration DAG using HttpSensor.",
)
def check_response_itm(response):
LoggingMixin().log.info("Callable function start")
#the object type is List
js = response.json()
flag=None
if(js):
LoggingMixin().log.info("Read json object")
n_itm=len(js)
LoggingMixin().log.info("Json has %s items",n_itm)
if(n_itm)>=10:
flag=True
else:
flag=False
else:
flag=False
return flag
def check_response_usr(response):
js = response.json()
output=js[0]
flag=None
if(js):
LoggingMixin().log.info("Read json object")
if((output['name']=="Leanne Graham")and(output['username']=="Bret")):
flag=True
else:
flag=False
else:
flag=False
return flag
return flag
checkNItems = HttpSensor(task_id="check",
http_conn_id="fakeAPIPlaceh",
endpoint="users",
response_check=lambda response: check_response_itm(response),
poke_interval=10,
timeout=100,
)
wait = DummyOperator(task_id="wait", dag=dag)
checkUser = HttpSensor(task_id="checkUser",
http_conn_id="fakeAPIPlaceh",
endpoint="users",
response_check=lambda response: check_response_usr(response),
poke_interval=10,
timeout=100,
)
create_metrics = DummyOperator(task_id="create_metrics", dag=dag)
checkNItems>>wait>>checkUser>>create_metrics