randr97 commented on a change in pull request #8008: Custom Facebook Ads 
Operator
URL: https://github.com/apache/airflow/pull/8008#discussion_r401837518
 
 

 ##########
 File path: airflow/providers/facebook/ads/operators/ads.py
 ##########
 @@ -0,0 +1,111 @@
+#
+# 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 Facebook Ad Reporting to GCS operators.
+"""
+import json
+import tempfile
+from typing import Any, Dict, List, Optional
+
+from airflow.models import BaseOperator
+from airflow.providers.facebook.ads.hooks.ads import FacebookAdsReportingHook
+from airflow.providers.google.cloud.hooks.gcs import GCSHook
+from airflow.utils.decorators import apply_defaults
+
+
+class FacebookAdsReportToGcsOperator(BaseOperator):
+    """
+    Fetches the results from the Facebook Ads API as desired in the params
+    Converts and saves the data as a temporary JSON file
+    Uploads the JSON to Google Cloud Storage
+
+    .. seealso::
+        For more information on the Facebook Ads API, take a look at the API 
docs:
+        https://developers.facebook.com/docs/marketing-apis/
+
+    .. seealso::
+        For more information on the Facebook Ads Python SDK, take a look at 
the docs:
+        https://github.com/facebook/facebook-python-business-sdk
+
+    :param bucket: The GCS bucket to upload to
+    :type bucket: str
+    :param obj: GCS path to save the object. Must be the full file path (ex. 
`path/to/file.txt`)
+    :type obj: str
+    :param gcp_conn_id: Airflow Google Cloud Platform connection ID
+    :type gcp_conn_id: str
+    :param facebook_ads_conn_id: Airflow Facebook Ads connection ID
+    :type facebook_ads_conn_id: str
+    :param api_version: The version of Facebook API. Default to v6.0
+    :type api_version: str
+    :param fields: List of fields that is obtained from Facebook. Found in 
AdsInsights.Field class.
+        
https://developers.facebook.com/docs/marketing-api/insights/parameters/v6.0
+    :type fields: List[str]
+    :param params: Parameters that determine the query for Facebook
+        
https://developers.facebook.com/docs/marketing-api/insights/parameters/v6.0
+    :type params: Dict[str, Any]
+    :param gzip: Option to compress local file or file data for upload
+    :type gzip: bool
+    """
+
+    template_fields = ("facebook_ads_conn_id", "bucket", "obj")
+
+    @apply_defaults
+    def __init__(
+        self,
+        bucket: str,
+        obj: str,
+        gcp_conn_id: str = "google_cloud_default",
+        facebook_ads_conn_id: str = "facebook_ads_default",
+        api_version: str = "v6.0",
+        fields: Optional[List[str]] = None,
+        params: Optional[Dict[str, Any]] = None,
+        gzip: bool = False,
+        *args,
+        **kwargs,
+    ) -> None:
+        super().__init__(*args, **kwargs)
+        self.bucket = bucket
+        self.obj = obj
+        self.gcp_conn_id = gcp_conn_id
+        self.facebook_ads_conn_id = facebook_ads_conn_id
+        self.api_version = api_version
+        self.fields = fields
+        self.params = params  # type:ignore
+        self.gzip = gzip
+
+    def execute(self, context: Dict):
+        service = 
FacebookAdsReportingHook(facebook_ads_conn_id=self.facebook_ads_conn_id,
+                                           api_version=self.api_version)
+        rows = service.bulk_facebook_report(params=self.params, 
fields=self.fields)
+        try:
+            converted_rows = [dict(row) for row in rows]
+        except Exception as e:
+            self.log.error("An error occurred in converting the Facebook Ad 
Rows. \n Error %s", e)
+            raise
 
 Review comment:
   @turbaszek looks like I handling errors in the hook, thanks for pointing it 
out. I don't think the exception is required. Before I would iterate over the 
cursor in the operator, but now its just type casting.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to