Skip to content

Commit

Permalink
Updated to format
Browse files Browse the repository at this point in the history
  • Loading branch information
lazebnyi committed Aug 25, 2021
1 parent 3ad29c4 commit 35746d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def next_page_token(self, response: requests.Response) -> Optional[Mapping[str,
return {"pageToken": next_page}

def request_body_json(
self, stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None, **kwargs
self, stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None, **kwargs
) -> Optional[Mapping]:

metrics = [{"expression": metric} for metric in self.metrics]
Expand Down Expand Up @@ -173,9 +173,7 @@ def get_json_schema(self) -> Mapping[str, Any]:
"type": ["null", "object"],
"additionalProperties": False,
"properties": {
"view_id": {
"type": ["string"]
},
"view_id": {"type": ["string"]},
},
}

Expand Down Expand Up @@ -227,8 +225,7 @@ def stream_slices(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Ite

while start_date <= end_date:
end_date_slice = start_date.add(days=self.window_in_days)
date_slices.append(
{"startDate": self.to_datetime_str(start_date), "endDate": self.to_datetime_str(end_date_slice)})
date_slices.append({"startDate": self.to_datetime_str(start_date), "endDate": self.to_datetime_str(end_date_slice)})
# add 1 day for start next slice from next day and not duplicate data from previous slice end date.
start_date = end_date_slice.add(days=1)

Expand Down Expand Up @@ -259,7 +256,7 @@ def lookup_data_type(self, field_type, attribute):
# Custom Google Analytics Metrics {ga:goalXXStarts, ga:metricXX, ... }
# We always treat them as as strings as we can not be sure of their data type
if attribute.startswith("ga:goal") and attribute.endswith(
("Starts", "Completions", "Value", "ConversionRate", "Abandons", "AbandonRate")
("Starts", "Completions", "Value", "ConversionRate", "Abandons", "AbandonRate")
):
return "string"
elif attribute.startswith("ga:searchGoal") and attribute.endswith("ConversionRate"):
Expand Down Expand Up @@ -399,13 +396,11 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
class GoogleAnalyticsV4IncrementalObjectsBase(GoogleAnalyticsV4Stream):
cursor_field = "ga_date"

def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]) -> \
Mapping[str, Any]:
def get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]) -> Mapping[str, Any]:
"""
Update the state value, default CDK method.
"""
return {self.cursor_field: max(latest_record.get(self.cursor_field, ""),
current_stream_state.get(self.cursor_field, ""))}
return {self.cursor_field: max(latest_record.get(self.cursor_field, ""), current_stream_state.get(self.cursor_field, ""))}


class GoogleAnalyticsOauth2Authenticator(Oauth2Authenticator):
Expand Down Expand Up @@ -433,8 +428,7 @@ def refresh_access_token(self) -> Tuple[str, int]:
"""
response_json = None
try:
response = requests.request(method="POST", url=self.token_refresh_endpoint,
params=self.get_refresh_request_params())
response = requests.request(method="POST", url=self.token_refresh_endpoint, params=self.get_refresh_request_params())

response_json = response.json()
response.raise_for_status()
Expand Down Expand Up @@ -492,7 +486,7 @@ def check_connection(self, logger, config) -> Tuple[bool, any]:
return True, None
except (requests.exceptions.RequestException, ValueError) as e:
if e == ValueError:
logger.error(f"Invalid custom reports json structure.")
logger.error("Invalid custom reports json structure.")
return False, e

def streams(self, config: Mapping[str, Any]) -> List[Stream]:
Expand All @@ -501,8 +495,7 @@ def streams(self, config: Mapping[str, Any]) -> List[Stream]:
authenticator = GoogleAnalyticsOauth2Authenticator(config)

config["authenticator"] = authenticator
config["ga_streams"] = json.loads(
pkgutil.get_data("source_google_analytics_v4", "defaults/default_reports.json")) + json.loads(
config["ga_streams"] = json.loads(pkgutil.get_data("source_google_analytics_v4", "defaults/default_reports.json")) + json.loads(
config["custom_reports"]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{"items":
[
{
"items": [
{
"id": "ga:users",
"kind": "analytics#column",
Expand Down Expand Up @@ -54,5 +54,5 @@
"addedInApiVersion": "3"
}
}
]
]
}

0 comments on commit 35746d1

Please sign in to comment.