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 2019/10/07 17:52:59 UTC

[GitHub] [airflow] mik-laj commented on a change in pull request #6169: [AIRFLOW-4970] Add Google Campaign Manager integration

mik-laj commented on a change in pull request #6169: [AIRFLOW-4970] Add Google Campaign Manager integration
URL: https://github.com/apache/airflow/pull/6169#discussion_r332154256
 
 

 ##########
 File path: airflow/provider/google/marketing_platform/hooks/campaign_manager.py
 ##########
 @@ -0,0 +1,232 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+This module contains Google Campaign Manager hook.
+"""
+from typing import Any, Dict, List, Optional
+
+from googleapiclient import http
+from googleapiclient.discovery import Resource, build
+
+from airflow.gcp.hooks.base import GoogleCloudBaseHook
+
+
+class GoogleCampaignManagerHook(GoogleCloudBaseHook):
+    """
+    Hook for Google Campaign Manager.
+    """
+
+    _conn = None  # type: Optional[Resource]
+
+    def __init__(
+        self,
+        api_version: str = "v3.3",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+    ) -> None:
+        super().__init__(gcp_conn_id, delegate_to)
+        self.api_version = api_version
+
+    def get_conn(self) -> Resource:
+        """
+        Retrieves connection to Campaign Manager.
+        """
+        if not self._conn:
+            http_authorized = self._authorize()
+            self._conn = build(
+                "dfareporting",
+                self.api_version,
+                http=http_authorized,
+                cache_discovery=False,
+            )
+        return self._conn
+
+    def delete_report(self, profile_id: str, report_id: str) -> Any:
+        """
+        Deletes a report by its ID.
+
+        :param profile_id: The DFA user profile ID.
+        :type profile_id: str
+        :param report_id: The ID of the report.
+        :type report_id: str
+        """
+        response = (
+            self.get_conn()  # pylint: disable=no-member
+            .reports()
+            .delete(profileId=profile_id, reportId=report_id)
+            .execute(num_retries=self.num_retries)
+        )
+        return response
+
+    def insert_report(self, profile_id: str, report: Dict[str, Any]) -> Any:
+        """
+        Creates a report.
+
+        :param profile_id: The DFA user profile ID.
+        :type profile_id: str
+        :param report: The report resource to be inserted.
+        :type report: Dict[str, Any]
+        """
+        response = (
+            self.get_conn()  # pylint: disable=no-member
+            .reports()
+            .insert(profileId=profile_id, body=report)
+            .execute(num_retries=self.num_retries)
+        )
+        return response
+
+    def list_reports(
+        self,
+        profile_id: str,
+        max_results: Optional[int] = None,
+        scope: Optional[str] = None,
+        sort_field: Optional[str] = None,
+        sort_order: Optional[str] = None,
+    ) -> List[Dict]:
+        """
+        Retrieves list of reports.
+
+        :param profile_id: The DFA user profile ID.
+        :type profile_id: str
+        :param max_results: Maximum number of results to return.
+        :type max_results: Optional[int]
+        :param scope: The scope that defines which results are returned.
+        :type scope: Optional[str]
+        :param sort_field: The field by which to sort the list.
+        :type sort_field: Optional[str]
+        :param sort_order: Order of sorted results.
+        :type sort_order: Optional[str]
+        """
+        reports = []  # type: List[Dict]
+        conn = self.get_conn()
+        request = conn.reports().list(  # pylint: disable=no-member
+            profileId=profile_id,
+            maxResults=max_results,
+            scope=scope,
+            sortField=sort_field,
+            sortOrder=sort_order,
+        )
+        while request is not None:
+            response = request.execute(num_retries=self.num_retries)
+            reports = response.get("items", [])
 
 Review comment:
   ```suggestion
               reports.extends(response.get("items", []))
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services