question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SimpleHttpOperator takes lambda to transform response before doing XCom push

See original GitHub issue

Description

I propose that the SimpleHttpOperator also take an argument like xcom_function or xcom_transformation that describes a lambda or single-variable function that takes as input the response to the http request and uses the output of the function for the xcom push.

alternatively, the existing parameter xcom_push can be modified to take in both Booleans and Callables. if provided False or True, behavior is unchanged. If provided a single variable callable, the function is applied to the response.

Use case / motivation

Sometimes we want to XCom push a portion of the response, and then use that portion in a templated xcom pull, where we do not have access to things like json deserialization. Currently the best way to do this is to have a middleman task that takes the response.text xcom from the httpoperator, applies the transformation and then pushes the result.

Are you willing to submit a PR? Yes

Related Issues None that I could find

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
potiukcommented, Jul 26, 2021

Ok. closing this one then.

1reaction
Naraujo13commented, Jul 26, 2021

What do you think about using the response_filter parameter? https://github.com/apache/airflow/blob/8505d2f0a4524313e3eff7a4f16b9a9439c7a79f/airflow/providers/http/operators/http.py#L120-L122

It makes sense, for the HTTPOperator we could simply use the response_filter, so it wouldn’t make sense to add the filter here.

I think I mixed a different use case where one time at work we extended HTTPOperator to save the results on S3, and we wanted to save the data but only push a small part of it (list of ids, etc) to XCom to use in a next request, so we needed different filters there. But this is specific to a different use case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[GitHub] [airflow] Naraujo13 edited a comment on issue #14906 ...
[GitHub] [airflow] Naraujo13 edited a comment on issue #14906: SimpleHttpOperator takes lambda to transform response before doing XCom push.
Read more >
Airflow DAG - access context using SimpleHttpOperator to ...
When retrieving XCom in Jinja templates, you don't need the context object since the context object is passed to render the template value ......
Read more >
airflow.providers.http.operators.http
e.g response_filter=lambda response: json.loads(response.text). The callable takes the response object as the first positional argument and optionally any ...
Read more >
API Reference - Apache Airflow Documentation - Read the Docs
Operators¶. Operators allow for generation of certain types of tasks that become nodes in the DAG when instantiated. All operators derive from BaseOperator ......
Read more >
Airflow XCom pull and push under the hood: Multiple values ...
Question was: “How can I get Xcom that pushed by task of another DAG?” ... code and return to how you as developer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found