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-genesys: new connector #2136

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ on:
- "source-brevo/**"
- "source-impact-native/**"
- "source-front/**"
- "source-genesys/**"

pull_request:
branches: [main]
Expand Down Expand Up @@ -60,6 +61,7 @@ on:
- "source-brevo/**"
- "source-impact-native/**"
- "source-front/**"
- "source-genesys/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -171,6 +173,10 @@ jobs:
type: capture
version: v2
usage_rate: "1.0"
- name: source-genesys
type: capture
version: v1
usage_rate: "1.0"

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions estuary-cdk/estuary_cdk/capture/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
AccessToken,
BaseOAuth2Credentials,
CaptureBinding,
ClientCredentialsOAuth2Credentials,
ClientCredentialsOAuth2Spec,
OAuth2Spec,
ValidationError,
BasicAuth,
Expand Down
20 changes: 20 additions & 0 deletions estuary-cdk/estuary_cdk/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class Checkpoint(BaseModel):
ackIntents: dict[str, str]


class ClientCredentialsOAuth2Spec(BaseModel):
accessTokenResponseMap: dict[str, str]
accessTokenUrlTemplate: str


class OAuth2Spec(BaseModel):
provider: str
accessTokenBody: str
Expand Down Expand Up @@ -122,6 +127,21 @@ class ValidationError(Exception):
errors: list[str]


class ClientCredentialsOAuth2Credentials(abc.ABC, BaseModel):
credentials_title: Literal["OAuth Credentials"] = Field(
default="OAuth Credentials",
json_schema_extra={"type": "string"}
)
client_id: str = Field(
title="Client Id",
json_schema_extra={"secret": True},
)
client_secret: str = Field(
title="Client Secret",
json_schema_extra={"secret": True},
)


class BaseOAuth2Credentials(abc.ABC, BaseModel):
credentials_title: Literal["OAuth Credentials"] = Field(
default="OAuth Credentials",
Expand Down
48 changes: 37 additions & 11 deletions estuary-cdk/estuary_cdk/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import time

from . import Mixin
from .flow import BaseOAuth2Credentials, AccessToken, OAuth2Spec, BasicAuth
from .flow import (
AccessToken,
BasicAuth,
BaseOAuth2Credentials,
ClientCredentialsOAuth2Credentials,
ClientCredentialsOAuth2Spec,
OAuth2Spec,
)

DEFAULT_AUTHORIZATION_HEADER = "Authorization"

Expand Down Expand Up @@ -120,8 +127,8 @@ class AccessTokenResponse(BaseModel):
refresh_token: str = ""
scope: str = ""

oauth_spec: OAuth2Spec | None
credentials: BaseOAuth2Credentials | AccessToken | BasicAuth
oauth_spec: OAuth2Spec | ClientCredentialsOAuth2Spec | None
credentials: BaseOAuth2Credentials | ClientCredentialsOAuth2Credentials | AccessToken | BasicAuth
authorization_header: str = DEFAULT_AUTHORIZATION_HEADER
_access_token: AccessTokenResponse | None = None
_fetched_at: int = 0
Expand All @@ -137,7 +144,7 @@ async def fetch_token(self, log: Logger, session: HTTPSession) -> tuple[str, str
).decode(),
)

assert isinstance(self.credentials, BaseOAuth2Credentials)
assert isinstance(self.credentials, BaseOAuth2Credentials) or isinstance(self.credentials, ClientCredentialsOAuth2Credentials)
current_time = time.time()

if self._access_token is not None:
Expand All @@ -158,20 +165,39 @@ async def fetch_token(self, log: Logger, session: HTTPSession) -> tuple[str, str
return ("Bearer", self._access_token.access_token)

async def _fetch_oauth2_token(
self, log: Logger, session: HTTPSession, credentials: BaseOAuth2Credentials
self, log: Logger, session: HTTPSession, credentials: BaseOAuth2Credentials | ClientCredentialsOAuth2Credentials
) -> AccessTokenResponse:
assert self.oauth_spec

headers = {}
form = {}

match credentials:
case BaseOAuth2Credentials():
form = {
"grant_type": "refresh_token",
"client_id": credentials.client_id,
"client_secret": credentials.client_secret,
"refresh_token": credentials.refresh_token,
}
case ClientCredentialsOAuth2Credentials():
form = {
"grant_type": "client_credentials",
}
headers = {
"Authorization": "Basic " + base64.b64encode(
f"{credentials.client_id}:{credentials.client_secret}".encode()
).decode()
}
case _:
raise TypeError(f"Unsupported credentials type: {type(credentials)}.")

response = await session.request(
log,
self.oauth_spec.accessTokenUrlTemplate,
method="POST",
form={
"grant_type": "refresh_token",
"client_id": credentials.client_id,
"client_secret": credentials.client_secret,
"refresh_token": credentials.refresh_token,
},
headers=headers,
form=form,
_with_token=False,
)
return self.AccessTokenResponse.model_validate_json(response)
Expand Down
1 change: 1 addition & 0 deletions source-genesys/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1
Loading
Loading