You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "infohash (via GitHub)" <gi...@apache.org> on 2024/02/04 18:21:47 UTC
[I] Simplify Parsing of apprise_default Connection ID [airflow]
infohash opened a new issue, #37170:
URL: https://github.com/apache/airflow/issues/37170
### Apache Airflow Provider(s)
apprise
### Versions of Apache Airflow Providers
1.2.1
### Apache Airflow version
2.8.1
### Operating System
Ubuntu 20.04.6 LTS
### Deployment
Docker-Compose
### Deployment details
Added airflow connection environment variable for Apprise.
```shell
AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": {"path": "https://hooks.slack.com/services/T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7F", "tags": "pipeline"}}}'
```
### What happened
https://github.com/apache/airflow/blob/46470aba68e5ebeee24a03dc22d012a50ee287ad/airflow/providers/apprise/hooks/apprise.py#L49-L51
The way the return statement of this method parses the connection value raises the exception if quotes are not escaped in the serialized JSON string. Here are the examples, note the variations in the JSON string:
## When quotes are not escaped
```python
$ export AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": "{"path": "https://hooks.slack.com/services/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", "tags": "alert"}"}}'
>>> from airflow.providers.apprise.hooks.apprise import AppriseHook
>>> hook = AppriseHook()
>>> conn = hook.get_connection('apprise_default')
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 25 (char 24)
```
## When the value is a valid JSON string
```python
$ export AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": {"path": "https://hooks.slack.com/services/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", "tags": "alert"}}}'
>>> import json
>>> from airflow.providers.apprise.hooks.apprise import AppriseHook
>>> hook = AppriseHook()
>>> conn = hook.get_connection('apprise_default')
>>> json.loads(conn.extra_dejson["config"])
TypeError: the JSON object must be str, bytes or bytearray, not dict
```
## Current way to do it
Escape quotes with backslash.
```python
$ export AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": "{\"path\": \"https://hooks.slack.com/services/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\", \"tags\": \"alert\"}"}}'
>>> from airflow.providers.apprise.hooks.apprise import AppriseHook
>>> hook = AppriseHook()
>>> conn = hook.get_connection('apprise_default')
>>> json.loads(conn.extra_dejson["config"])
{"path": "https://hooks.slack.com/services/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", "tags": "alert"}
```
### What you think should happen instead
To simplify setting of connection ID through an environment variable as a valid JSON string, a slight change can be made to `AppriseHook.get_config_from_conn`.
```python
def get_config_from_conn(self):
conn = self.get_connection(self.apprise_conn_id)
return conn['config']
```
```python
$ export AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": {"path": "https://hooks.slack.com/services/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", "tags": "alert"}}}'
>>> hook = AppriseHook()
>>> hook.get_config_from_conn()
{"path": "https://hooks.slack.com/services/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", "tags": "alert"}
```
### How to reproduce
```python
$ export AIRFLOW_CONN_APPRISE_DEFAULT='{"extra": {"config": {"path": "https://hooks.slack.com/services/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", "tags": "alert"}}}'
>>> hook = AppriseHook()
>>> hook.get_config_from_conn()
TypeError: the JSON object must be str, bytes or bytearray, not dict
```
### Anything else
_No response_
### Are you willing to submit PR?
- [X] Yes I am willing to submit a PR!
### Code of Conduct
- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Simplify Parsing of apprise_default Connection ID [airflow]
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala closed issue #37170: Simplify Parsing of apprise_default Connection ID
URL: https://github.com/apache/airflow/issues/37170
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Simplify Parsing of apprise_default Connection ID [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on issue #37170:
URL: https://github.com/apache/airflow/issues/37170#issuecomment-1925872214
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org