Exemplo n.º 1
0
    def streams(self, config: Mapping[str, Any]) -> List[Type[Stream]]:
        """Discovery method, returns available streams

        :param config: A Mapping of the user input configuration as defined in the connector spec.
        """
        config: ConnectorConfig = ConnectorConfig.parse_obj(config)  # FIXME: this will be not need after we fix CDK
        api = API(account_id=config.account_id, access_token=config.access_token)
        insights_args = dict(
            api=api,
            start_date=config.start_date,
            buffer_days=config.insights_lookback_window,
            days_per_job=config.insights_days_per_job,
        )

        return [
            Campaigns(api=api, start_date=config.start_date, include_deleted=config.include_deleted),
            AdSets(api=api, start_date=config.start_date, include_deleted=config.include_deleted),
            Ads(api=api, start_date=config.start_date, include_deleted=config.include_deleted),
            AdCreatives(api=api),
            AdsInsights(**insights_args),
            AdsInsightsAgeAndGender(**insights_args),
            AdsInsightsCountry(**insights_args),
            AdsInsightsRegion(**insights_args),
            AdsInsightsDma(**insights_args),
            AdsInsightsPlatformAndDevice(**insights_args),
        ]
Exemplo n.º 2
0
def api_fixture(some_config, requests_mock, fb_account_response):
    api = API(account_id=some_config["account_id"],
              access_token=some_config["access_token"])

    requests_mock.register_uri(
        "GET", FacebookSession.GRAPH + f"/{FB_API_VERSION}/me/adaccounts",
        [fb_account_response])
    return api
Exemplo n.º 3
0
    def check_connection(self, _logger: "logging.Logger",
                         config: Mapping[str, Any]) -> Tuple[bool, Any]:
        """Connection check to validate that the user-provided config can be used to connect to the underlying API

        :param config:  the user-input config object conforming to the connector's spec.json
        :param _logger:  logger object
        :return Tuple[bool, Any]: (True, None) if the input config can be used to connect to the API successfully, (False, error) otherwise.
        """
        config = ConnectorConfig.parse_obj(config)
        if pendulum.instance(config.end_date) < pendulum.instance(
                config.start_date):
            raise ValueError("end_date must be equal or after start_date.")
        try:
            api = API(account_id=config.account_id,
                      access_token=config.access_token)
            logger.info(f"Select account {api.account}")
            return True, None
        except requests.exceptions.RequestException as e:
            return False, e
Exemplo n.º 4
0
    def check_connection(self, logger, config: Mapping[str, Any]) -> Tuple[bool, Any]:
        """Connection check to validate that the user-provided config can be used to connect to the underlying API

        :param config:  the user-input config object conforming to the connector's spec.json
        :param logger:  logger object
        :return Tuple[bool, Any]: (True, None) if the input config can be used to connect to the API successfully, (False, error) otherwise.
        """
        ok = False
        error_msg = None

        try:
            config = ConnectorConfig.parse_obj(config)  # FIXME: this will be not need after we fix CDK
            api = API(account_id=config.account_id, access_token=config.access_token)
            logger.info(f"Select account {api.account}")
            ok = True
        except Exception as exc:
            error_msg = repr(exc)

        return ok, error_msg
Exemplo n.º 5
0
    def streams(self, config: Mapping[str, Any]) -> List[Type[Stream]]:
        """Discovery method, returns available streams

        :param config: A Mapping of the user input configuration as defined in the connector spec.
        :return: list of the stream instances
        """
        config: ConnectorConfig = ConnectorConfig.parse_obj(config)
        api = API(account_id=config.account_id,
                  access_token=config.access_token)

        insights_args = dict(
            api=api,
            start_date=config.start_date,
            end_date=config.end_date,
            insights_lookback_window=config.insights_lookback_window)
        streams = [
            AdAccount(api=api),
            AdSets(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            Ads(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            AdCreatives(
                api=api,
                fetch_thumbnail_images=config.fetch_thumbnail_images,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            AdsInsights(page_size=config.page_size,
                        max_batch_size=config.max_batch_size,
                        **insights_args),
            AdsInsightsAgeAndGender(page_size=config.page_size,
                                    max_batch_size=config.max_batch_size,
                                    **insights_args),
            AdsInsightsCountry(page_size=config.page_size,
                               max_batch_size=config.max_batch_size,
                               **insights_args),
            AdsInsightsRegion(page_size=config.page_size,
                              max_batch_size=config.max_batch_size,
                              **insights_args),
            AdsInsightsDma(page_size=config.page_size,
                           max_batch_size=config.max_batch_size,
                           **insights_args),
            AdsInsightsPlatformAndDevice(page_size=config.page_size,
                                         max_batch_size=config.max_batch_size,
                                         **insights_args),
            AdsInsightsActionType(page_size=config.page_size,
                                  max_batch_size=config.max_batch_size,
                                  **insights_args),
            Campaigns(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            Images(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            Videos(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
            Activities(
                api=api,
                start_date=config.start_date,
                end_date=config.end_date,
                include_deleted=config.include_deleted,
                page_size=config.page_size,
                max_batch_size=config.max_batch_size,
            ),
        ]

        return self._update_insights_streams(insights=config.custom_insights,
                                             default_args=insights_args,
                                             streams=streams)