You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/02/06 16:55:39 UTC

[GitHub] [airflow] pierrejeambrun commented on a change in pull request #21349: Switch from zdesk to zenpy in ZendeskHook

pierrejeambrun commented on a change in pull request #21349:
URL: https://github.com/apache/airflow/pull/21349#discussion_r800206149



##########
File path: airflow/providers/zendesk/hooks/zendesk.py
##########
@@ -31,90 +32,64 @@ class ZendeskHook(BaseHook):
     :param zendesk_conn_id: The Airflow connection used for Zendesk credentials.
     """
 
-    def __init__(self, zendesk_conn_id: str) -> None:
+    conn_name_attr = 'zendesk_conn_id'
+    default_conn_name = 'zendesk_default'
+    conn_type = 'zendesk'
+    hook_name = 'Zendesk'
+
+    def __init__(self, zendesk_conn_id: str = default_conn_name) -> None:
         super().__init__()
-        self.__zendesk_conn_id = zendesk_conn_id
-        self.__url = None
+        self.zendesk_conn_id = zendesk_conn_id
+        self.base_api: Optional[BaseApi] = None
+        zenpy_client, url = self._init_conn()
+        self.zenpy_client = zenpy_client
+        self.__url = url
+        # Keep reference to the _get method to allow arbitrary endpoint call for
+        # backwards compatibility. (ZendeskHook.call method)
+        self._get = self.zenpy_client.users._get
+
+    def _init_conn(self) -> Tuple[Zenpy, str]:
+        """
+        Create the Zenpy Client for our Zendesk connection.
 
-    def get_conn(self) -> Zendesk:
-        conn = self.get_connection(self.__zendesk_conn_id)
-        self.__url = "https://" + conn.host
-        return Zendesk(
-            zdesk_url=self.__url, zdesk_email=conn.login, zdesk_password=conn.password, zdesk_token=True
-        )
+        :return: zenpy.Zenpy client and the url for the API.
+        """
+        conn = self.get_connection(self.zendesk_conn_id)
+        url = "https://" + conn.host
+        domain = conn.host
+        subdomain: Optional[str] = None
+        if conn.host.count(".") >= 2:
+            dot_splitted_string = conn.host.rsplit(".", 2)
+            subdomain = dot_splitted_string[0]
+            domain = ".".join(dot_splitted_string[1:])
+        return Zenpy(domain=domain, subdomain=subdomain, email=conn.login, password=conn.password), url
 
-    def __handle_rate_limit_exception(self, rate_limit_exception: ZendeskError) -> None:
+    def get_conn(self) -> Zenpy:
         """
-        Sleep for the time specified in the exception. If not specified, wait
-        for 60 seconds.
+        Get the underlying Zenpy client.
+
+        :return: zenpy.Zenpy client.
         """
-        retry_after = int(rate_limit_exception.response.headers.get('Retry-After', 60))
-        self.log.info("Hit Zendesk API rate limit. Pausing for %s seconds", retry_after)
-        time.sleep(retry_after)
+        return self.zenpy_client
 
     def call(
         self,
         path: str,
         query: Optional[dict] = None,
-        get_all_pages: bool = True,
-        side_loading: bool = False,
-    ) -> dict:
+        **kwargs: Any,
+    ) -> BaseObject:
         """
-        Call Zendesk API and return results
+        Call Zendesk API and return results.
 
-        :param path: The Zendesk API to call
-        :param query: Query parameters
-        :param get_all_pages: Accumulate results over all pages before
-               returning. Due to strict rate limiting, this can often timeout.
-               Waits for recommended period between tries after a timeout.
-        :param side_loading: Retrieve related records as part of a single
-               request. In order to enable side-loading, add an 'include'
-               query parameter containing a comma-separated list of resources
-               to load. For more information on side-loading see
-               https://developer.zendesk.com/rest_api/docs/core/side_loading
-        """
-        query_params = query or {}
-        zendesk = self.get_conn()
-        first_request_successful = False
-
-        while not first_request_successful:
-            try:
-                results = zendesk.call(path, query_params)
-                first_request_successful = True
-            except RateLimitError as rle:
-                self.__handle_rate_limit_exception(rle)
-
-        # Find the key with the results
-        keys = [path.split("/")[-1].split(".json")[0]]
-        next_page = results['next_page']
-        if side_loading:
-            keys += query_params['include'].split(',')
-        results = {key: results[key] for key in keys}
+        This endpoint is kept for backward compatibility purpose but it should be
+        removed in the future to expose specific methods for each resource.
+        TODO: When removing this method, remove the reference to _get in __init__

Review comment:
       Done




-- 
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