19
19
20
20
# Apache Airflow Python Client
21
21
22
- > ** _ NOTE:_ ** The Apache Airflow Client is still under active development and some methods
23
- > or APIs might be broken. Please raise an issue in github if you encounter any such issues.
24
-
25
-
26
-
27
22
## Requirements.
28
23
29
- Python >= 3.6
24
+ Python >= 3.7
30
25
31
26
## Installation & Usage
27
+
32
28
### pip install
33
29
34
30
You can install directly using pip:
@@ -41,7 +37,7 @@ pip install apache-airflow-client
41
37
42
38
Or install via [Setuptools](http://pypi.python.org/pypi/setuptools).
43
39
44
- ` ` ` sh
40
+ ` ` ` shell
45
41
git clone [email protected] :apache/airflow-client-python.git
46
42
cd airflow-client-python
47
43
python setup.py install --user
@@ -53,40 +49,116 @@ Then import the package:
53
49
import airflow_client.client
54
50
` ` `
55
51
52
+ # # Changelog
53
+
54
+ See [CHANGELOG.md](https://github.com/apache/airflow-client-python/blob/main/CHANGELOG.md) for keeping
55
+ track on what has changed in the client.
56
+
57
+
56
58
# # Getting Started
57
59
58
- Please follow the [installation procedure](# installation--usage) and then run the following:
60
+ Please follow the [installation procedure](# installation--usage) and then run the following
61
+ example python script:
59
62
60
63
` ` ` python
61
- import airflow_client.client
62
- from pprint import pprint
63
- from airflow_client.client.api import config_api
64
+ import uuid
64
65
66
+ import airflow_client.client
67
+ try:
68
+ # If you have rich installed, you will have nice colored output of the API responses
69
+ from rich import print
70
+ except ImportError:
71
+ print(" Output will not be colored. Please install rich to get colored output: ` pip install rich` " )
72
+ pass
73
+ from airflow_client.client.api import config_api, dag_api, dag_run_api
74
+ from airflow_client.client.model.dag_run import DAGRun
75
+
76
+ # The client must use the authentication and authorization parameters
77
+ # in accordance with the API server security policy.
78
+ # Examples for each auth method are provided below, use the example that
79
+ # satisfies your auth use case.
65
80
#
66
- # In case of the basic authentication below. Make sure:
67
- # - Airflow is configured with the basic_auth as backend:
68
- # auth_backend = airflow.api.auth.backend.basic_auth
69
- # - Make sure that the client has been generated with securitySchema Basic.
81
+ # In case of the basic authentication below, make sure that Airflow is
82
+ # configured also with the basic_auth as backend additionally to regular session backend needed
83
+ # by the UI. In the `[api]` section of your `airflow.cfg` set:
84
+ #
85
+ # auth_backend = airflow.api.auth.backend.session,airflow.api.auth.backend.basic_auth
86
+ #
87
+ # Make sure that your user/name are configured properly - using the user/password that has admin
88
+ # privileges in Airflow
70
89
71
90
# Configure HTTP basic authorization: Basic
72
91
configuration = airflow_client.client.Configuration(
73
- host=" http://localhost/api/v1" ,
92
+ host=" http://localhost:8080 /api/v1" ,
74
93
username=' admin' ,
75
94
password=' admin'
76
95
)
77
96
97
+ # Make sure in the [core] section, the `load_examples` config is set to True in your airflow.cfg
98
+ # or AIRFLOW__CORE__LOAD_EXAMPLES environment variable set to True
99
+ DAG_ID = " example_bash_operator"
78
100
79
101
# Enter a context with an instance of the API client
80
102
with airflow_client.client.ApiClient(configuration) as api_client:
81
- # Create an instance of the API class
82
- api_instance = config_api.ConfigApi(api_client)
83
103
104
+ errors = False
105
+
106
+ print(' [blue]Getting DAG list' )
107
+ dag_api_instance = dag_api.DAGApi(api_client)
108
+ try:
109
+ api_response = dag_api_instance.get_dags ()
110
+ print(api_response)
111
+ except airflow_client.client.OpenApiException as e:
112
+ print(" [red]Exception when calling DagAPI->get_dags: %s\n" % e)
113
+ errors = True
114
+ else:
115
+ print(' [green]Getting DAG list successful' )
116
+
117
+
118
+ print(' [blue]Getting Tasks for a DAG' )
119
+ try:
120
+ api_response = dag_api_instance.get_tasks(DAG_ID)
121
+ print(api_response)
122
+ except airflow_client.client.exceptions.OpenApiException as e:
123
+ print(" [red]Exception when calling DagAPI->get_tasks: %s\n" % e)
124
+ errors = True
125
+ else:
126
+ print(' [green]Getting Tasks successful' )
127
+
128
+
129
+ print(' [blue]Triggering a DAG run' )
130
+ dag_run_api_instance = dag_run_api.DAGRunApi(api_client)
131
+ try:
132
+ # Create a DAGRun object (no dag_id should be specified because it is read-only property of DAGRun)
133
+ # dag_run id is generated randomly to allow multiple executions of the script
134
+ dag_run = DAGRun(
135
+ dag_run_id=' some_test_run_' + uuid.uuid4 ().hex,
136
+ )
137
+ api_response = dag_run_api_instance.post_dag_run(DAG_ID, dag_run)
138
+ print(api_response)
139
+ except airflow_client.client.exceptions.OpenApiException as e:
140
+ print(" [red]Exception when calling DAGRunAPI->post_dag_run: %s\n" % e)
141
+ errors = True
142
+ else:
143
+ print(' [green]Posting DAG Run successful' )
144
+
145
+ # Get current configuration. Note, this is disabled by default with most installation.
146
+ # You need to set `expose_config = True` in Airflow configuration in order to retrieve configuration.
147
+ conf_api_instance = config_api.ConfigApi(api_client)
84
148
try:
85
- # Get current configuration
86
- api_response = api_instance.get_config ()
87
- pprint(api_response)
88
- except airflow_client.client.ApiException as e:
89
- print(" Exception when calling ConfigApi->get_config: %s\n" % e)
149
+ api_response = conf_api_instance.get_config ()
150
+ print(api_response)
151
+ except airflow_client.client.OpenApiException as e:
152
+ print(" [red]Exception when calling ConfigApi->get_config: %s\n" % e)
153
+ errors = True
154
+ else:
155
+ print(' [green]Config retrieved successfully' )
156
+
157
+ if errors:
158
+ print (' \n[red]There were errors while running the script - see above for details' )
159
+ else:
160
+ print (' \n[green]Everything went well' )
90
161
` ` `
91
162
92
- See [README](./airflow_client/README.md#documentation-for-api-endpoints) for full client API documentation.
163
+ See [README](https://github.com/apache/airflow-client-python/blob/main/README.md#documentation-for-api-endpoints)
164
+ for full client API documentation.
0 commit comments