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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to