def execute(self, context: 'Context') -> str: uri = f"gs://{self.bucket}/{self.object_name}" ads_hook = GoogleAdsHook( gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id, api_version=self.api_version, ) gcs_hook = GCSHook(gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain) with NamedTemporaryFile("w+") as temp_file: # Download accounts accounts = ads_hook.list_accessible_customers() writer = csv.writer(temp_file) writer.writerows(accounts) temp_file.flush() # Upload to GCS gcs_hook.upload(bucket_name=self.bucket, object_name=self.object_name, gzip=self.gzip, filename=temp_file.name) self.log.info("Uploaded %s to %s", len(accounts), uri) return uri
def execute(self, context: Dict): service = GoogleAdsHook(gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id) rows = service.search(client_ids=self.client_ids, query=self.query, page_size=self.page_size) try: getter = attrgetter(*self.attributes) converted_rows = [getter(row) for row in rows] except Exception as e: self.log.error( "An error occurred in converting the Google Ad Rows. \n Error %s", e) raise with NamedTemporaryFile("w", suffix=".csv") as csvfile: writer = csv.writer(csvfile) writer.writerows(converted_rows) csvfile.flush() hook = GCSHook(gcp_conn_id=self.gcp_conn_id) hook.upload( bucket_name=self.bucket, object_name=self.obj, filename=csvfile.name, gzip=self.gzip, ) self.log.info("%s uploaded to GCS", self.obj)
def mock_hook(): with mock.patch("airflow.hooks.base_hook.BaseHook.get_connection") as conn: hook = GoogleAdsHook(api_version=API_VERSION) conn.return_value.extra_dejson = EXTRAS yield hook