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 PostHog: Use account information for checking the connection #4692

Merged
merged 15 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -82,7 +82,6 @@
"supported_sync_modes": ["full_refresh"],
"source_defined_cursor": null,
"default_cursor_field": null,
"source_defined_primary_key": [["id"]],
"namespace": null
},
"sync_mode": "full_refresh",
Expand All @@ -97,7 +96,6 @@
"supported_sync_modes": ["full_refresh"],
"source_defined_cursor": null,
"default_cursor_field": null,
"source_defined_primary_key": [["id"]],
"namespace": null
},
"sync_mode": "full_refresh",
Expand Down Expand Up @@ -127,7 +125,6 @@
"supported_sync_modes": ["full_refresh"],
"source_defined_cursor": null,
"default_cursor_field": null,
"source_defined_primary_key": [["id"]],
"namespace": null
},
"sync_mode": "full_refresh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"type": "integer"
},
"content": {
"type": "string"
"type": ["string", "null"]
},
"date_marker": {
"type": "string",
"type": ["string", "null"],
"format": "date-time"
},
"creation_type": {
"type": "string"
"type": ["string", "null"]
},
"dashboard_item": {
"type": "string"
"type": ["string", "null"]
},
"created_by": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
"type": "string"
},
"groups": {
"type": "array",
"type": ["array", "object"],
"items": {
"type": "object",
"properties": {
"days": {
"type": "string"
"type": ["string", "null"]
},
"action_id": {
"type": "string"
"type": ["string", "null"]
},
"properties": {
"type": "array",
"type": ["array"],
"items": {
"type": "object"
}
Expand All @@ -46,19 +46,19 @@
"type": "string"
},
"first_name": {
"type": "string"
"type": ["string", "null"]
},
"email": {
"type": "string"
"type": ["string", "null"]
}
}
},
"created_at": {
"type": "string",
"type": ["string", "null"],
"format": "date-time"
},
"last_calculation": {
"type": "string"
"type": ["string", "null"]
},
"errors_calculating": {
"type": "integer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "string"
},
"event": {
"type": "string"
"type": ["string", "object"]
},
"timestamp": {
"type": "string",
Expand All @@ -34,7 +34,7 @@
"elements": {
"type": "array",
"items": {
"type": "string"
"type": ["string", "object"]
}
},
"elements_chain": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "string"
},
"rollout_percentage": {
"type": "integer"
"type": ["integer", "null"]
},
"filters": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,118 @@
"type": "integer"
},
"name": {
"type": "string"
"type": ["string", "null"]
},
"filters": {
"type": "object",
"type": ["object", "null"],
"properties": {
"events": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
"type": ["string", "null"]
},
"math": {
"type": "string"
"type": ["string", "null"]
},
"name": {
"type": "string"
"type": ["string", "null"]
},
"type": {
"type": "string"
"type": ["string", "null"]
},
"order": {
"type": "integer"
"type": ["integer", "null"]
},
"properties": {
"type": "array",
"items": {
"type": "string"
"type": ["string", "null", "object"]
}
},
"math_property": {
"type": "string"
"type": ["string", "null"]
}
}
}
},
"display": {
"type": "string"
"type": ["string", "null"]
},
"filters": {
"type": "array",
"items": {
"type": "string"
"type": ["string", "null"]
}
},
"insight": {
"type": "string"
"type": ["string", "null"]
},
"session": {
"type": "string"
"type": ["string", "null"]
},
"interval": {
"type": "string"
"type": ["string", "integer"]
},
"pagination": {
"type": "object"
"type": ["object", "null"]
}
}
},
"filters_hash": {
"type": "string"
"type": ["string", "null"]
},
"order": {
"type": "string"
"type": ["string", "null", "object"]
},
"deleted": {
"type": "boolean"
},
"dashboard": {
"type": "string"
"type": ["string", "integer", "null"]
},
"layouts": {
"type": "object"
"type": ["object", "null"]
},
"color": {
"type": "string"
"type": ["string", "null"]
},
"last_refresh": {
"type": "string",
"type": ["string", "null"],
"format": "date-time"
},
"refreshing": {
"type": "boolean"
},
"result": {
"type": "string"
"type": ["string", "null", "object"]
},
"created_at": {
"type": "string",
"type": ["string", "null"],
"format": "date-time"
},
"saved": {
"type": "boolean"
},
"created_by": {
"type": "object",
"type": ["object", "null"],
"properties": {
"id": {
"type": "integer"
},
"uuid": {
"type": "string"
"type": ["string", "null"]
},
"distinct_id": {
"type": "string"
"type": ["string", "null"]
},
"first_name": {
"type": "string"
"type": ["string", "null"]
},
"email": {
"type": "string"
"type": ["string", "null"]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
# SOFTWARE.
#


from typing import Any, List, Mapping, Tuple

import pendulum
import requests
from airbyte_cdk.logger import AirbyteLogger
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources import AbstractSource
Expand All @@ -41,6 +41,7 @@
InsightsPath,
InsightsSessions,
Persons,
PingMe,
Trends,
)

Expand All @@ -50,11 +51,13 @@ def check_connection(self, logger: AirbyteLogger, config: Mapping[str, Any]) ->
try:
_ = pendulum.parse(config["start_date"], strict=True)
authenticator = TokenAuthenticator(token=config["api_key"])
stream = Cohorts(authenticator=authenticator)
stream = PingMe(authenticator=authenticator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the error message the user sees if the API token is incorrect? it should help them understand what the issue is

Copy link
Contributor Author

@vovavovavovavova vovavovavovavova Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status wll be failed and log will get the following output in this case:
HTTPError('401 Client Error: Unauthorized for url: https://app.posthog.com/api/users/@me')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets catch HTTPError separately and check

if exc.status_code == requests.codes.unauthorized:
   return False, f"Please check you api_key. Error: {repr(exc)}"

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied but modified working version

records = stream.read_records(sync_mode=SyncMode.full_refresh)
_ = next(records)
return True, None
except Exception as e:
if isinstance(e, requests.exceptions.HTTPError) and e.response.status_code == 401:
return False, f"Please check you api_key. Error: {repr(e)}"
return False, repr(e)

def streams(self, config: Mapping[str, Any]) -> List[Stream]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,16 @@ class Trends(PosthogStream):

def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return "insight/trend"


class PingMe(PosthogStream):
"""
Docs: https://posthog.com/docs/api/user
"""

def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return "users/@me"

def parse_response(self, response: requests.Response, stream_state: Mapping[str, Any], **kwargs) -> Iterable[Mapping]:
response_json = response.json()
yield response_json