Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Source FB Marketing: deprecate INSIGHTS_DAYS_PER_JOB from connector's specification. #8234

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ From the Airbyte repository root, run:
**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/sources/facebook-marketing)
to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `source_facebook_marketing/spec.json` file.
Note that any directory named `secrets` is gitignored across the entire Airbyte repo, so there is no danger of accidentally checking in sensitive information.
See `sample_files/sample_config.json` for a sample config file.
See `integration_tests/sample_config.json` for a sample config file.

**If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `source facebook-marketing test creds`
and place them into `secrets/config.json`.
Expand All @@ -49,7 +49,7 @@ and place them into `secrets/config.json`.
python main.py spec
python main.py check --config secrets/config.json
python main.py discover --config secrets/config.json
python main.py read --config secrets/config.json --catalog sample_files/configured_catalog.json
python main.py read --config secrets/config.json --catalog integration_tests/configured_catalog.json
```

### Locally running the connector docker image
Expand All @@ -73,7 +73,7 @@ Then run any of the connector commands as follows:
docker run --rm airbyte/source-facebook-marketing:dev spec
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-facebook-marketing:dev check --config /secrets/config.json
docker run --rm -v $(pwd)/secrets:/secrets airbyte/source-facebook-marketing:dev discover --config /secrets/config.json
docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/sample_files:/sample_files airbyte/source-facebook-marketing:dev read --config /secrets/config.json --catalog /sample_files/configured_catalog.json
docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/sample_files:/sample_files airbyte/source-facebook-marketing:dev read --config /secrets/config.json --catalog /integration_tests/configured_catalog.json
```
## Testing
Make sure to familiarize yourself with [pytest test discovery](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery) to know how your test files and methods should be named.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ tests:
basic_read:
- config_path: "secrets/config.json"
configured_catalog_path: "integration_tests/configured_catalog.json"
timeout_seconds: 600
timeout_seconds: 3600
incremental:
- config_path: "secrets/config.json"
configured_catalog_path: "integration_tests/configured_catalog_without_insights.json"
future_state_path: "integration_tests/future_state.json"
full_refresh:
- config_path: "secrets/config.json"
configured_catalog_path: "integration_tests/configured_catalog.json"
timeout_seconds: 3600
# Ad Insights API has estimated metrics in response, which is calculated based on another metrics.
# Sometimes API doesn't return estimated metrics. E.g, cost_per_estimated_ad_recallers is calculated
# as total amount spent divided by estimated ad recall lift rate. When second metric is equal to zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@
"maximum": 28,
"type": "integer"
},
"insights_days_per_job": {
"title": "Insights Days Per Job",
"description": "Number of days to sync in one job. The more data you have - the smaller you want this parameter to be.",
"default": 7,
"minimum": 1,
"maximum": 30,
"type": "integer"
},
"custom_insights": {
"title": "Custom Insights",
"description": "A list wich contains insights entries, each entry must have a name and can contains fields, breakdowns or action_breakdowns)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

logger = logging.getLogger("airbyte")

# deprecated from ConnectorSpec, using default value of 1 = 1 day.
INSIGHTS_DAYS_PER_JOB: int = 1


class InsightConfig(BaseModel):

Expand Down Expand Up @@ -85,12 +88,6 @@ class Config:
maximum=28,
)

insights_days_per_job: int = Field(
default=7,
description="Number of days to sync in one job. The more data you have - the smaller you want this parameter to be.",
minimum=1,
maximum=30,
)
custom_insights: Optional[List[InsightConfig]] = Field(
description="A list wich contains insights entries, each entry must have a name and can contains fields, breakdowns or action_breakdowns)"
)
Expand Down Expand Up @@ -130,7 +127,7 @@ def streams(self, config: Mapping[str, Any]) -> List[Type[Stream]]:
start_date=config.start_date,
end_date=config.end_date,
buffer_days=config.insights_lookback_window,
days_per_job=config.insights_days_per_job,
days_per_job=INSIGHTS_DAYS_PER_JOB,
)

streams = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import time
from abc import ABC
from datetime import datetime, timedelta
from typing import Any, Callable, Iterable, List, Mapping, MutableMapping, Optional, Tuple, Union
from typing import Any, Callable, Dict, Iterable, List, Mapping, MutableMapping, Optional, Tuple, Union

import requests
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from airbyte_cdk.sources.streams.http import HttpStream
from airbyte_cdk.sources.streams.http.auth import HttpAuthenticator, Oauth2Authenticator
from airbyte_cdk.sources.streams.http.requests_native_auth import TokenAuthenticator, Oauth2Authenticator
from dateutil.parser import isoparse


Expand Down Expand Up @@ -52,12 +52,15 @@ class PaypalTransactionStream(HttpStream, ABC):

def __init__(
self,
authenticator: HttpAuthenticator,
authenticator: Union[Oauth2Authenticator, TokenAuthenticator],
start_date: Union[datetime, str],
end_date: Union[datetime, str] = None,
is_sandbox: bool = False,
**kwargs,
):
super().__init__(authenticator=authenticator)
self._authenticator = authenticator

now = datetime.now().replace(microsecond=0).astimezone()

if end_date and isinstance(end_date, str):
Expand Down Expand Up @@ -89,7 +92,6 @@ def __init__(

self.is_sandbox = is_sandbox

super().__init__(authenticator=authenticator)

def validate_input_dates(self):
# Validate input dates
Expand Down Expand Up @@ -334,6 +336,14 @@ def refresh_access_token(self) -> Tuple[str, int]:
except Exception as e:
raise Exception(f"Error while refreshing access token: {e}") from e

class PayPalAuthenticator:

def __init__(self, config: Dict):
self.config = config

def get_auth(self):
access_token = self.config.get("access_token")
return TokenAuthenticator(token = access_token)

class SourcePaypalTransaction(AbstractSource):
def check_connection(self, logger, config) -> Tuple[bool, any]:
Expand All @@ -342,12 +352,13 @@ def check_connection(self, logger, config) -> Tuple[bool, any]:
: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.
"""
authenticator = PayPalOauth2Authenticator(config)
#authenticator = PayPalOauth2Authenticator(config)
authenticator = PayPalAuthenticator(config).get_auth()

# Try to get API TOKEN
token = authenticator.get_access_token()
if not token:
return False, "Unable to fetch Paypal API token due to incorrect client_id or secret"
#token = authenticator.get_access_token()
#if not token:
# return False, "Unable to fetch Paypal API token due to incorrect client_id or secret"

# Try to initiate a stream and validate input date params
try:
Expand All @@ -361,8 +372,9 @@ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
"""
:param config: A Mapping of the user input configuration as defined in the connector spec.
"""
authenticator = PayPalOauth2Authenticator(config)

# authenticator = PayPalOauth2Authenticator(config)
authenticator = PayPalAuthenticator(config).get_auth()

return [
Transactions(authenticator=authenticator, **config),
Balances(authenticator=authenticator, **config),
Expand Down