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

fix: pass auth scheme to _get_integration_for_app #882

Merged
merged 10 commits into from
Nov 22, 2024
7 changes: 7 additions & 0 deletions python/composio/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,19 @@ def initiate_connection(
:param integration: Optional existing IntegrationModel instance to be used.
:return: A ConnectionRequestModel instance representing the initiated connection.
"""
from composio.tools.toolset import AUTH_SCHEMES, AuthSchemeType
tushar-composio marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(app_name, App):
app_name = app_name.slug

app = self.client.apps.get(name=app_name)
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
if integration is None and auth_mode is not None:
if auth_mode not in AUTH_SCHEMES:
raise ComposioClientError(
f"'auth_mode' should be one of {AUTH_SCHEMES}"
)
auth_mode = t.cast(AuthSchemeType, auth_mode)
if "OAUTH" not in auth_mode:
use_composio_auth = False
integration = self.client.integrations.create(
Expand Down
5 changes: 3 additions & 2 deletions python/composio/client/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

if t.TYPE_CHECKING:
from composio.client import Composio
from composio.tools.toolset import AuthSchemeType


def to_trigger_names(
Expand Down Expand Up @@ -282,7 +283,7 @@ class AppAuthScheme(BaseModel):
"""App authenticatio scheme."""

scheme_name: str
auth_mode: str
auth_mode: "AuthSchemeType"
fields: t.List[AuthSchemeField]

proxy: t.Optional[t.Dict] = None
Expand Down Expand Up @@ -1329,7 +1330,7 @@ def create(
self,
app_id: str,
name: t.Optional[str] = None,
auth_mode: t.Optional[str] = None,
auth_mode: t.Optional["AuthSchemeType"] = None,
auth_config: t.Optional[t.Dict[str, t.Any]] = None,
use_composio_auth: bool = False,
force_new_integration: bool = False,
Expand Down
20 changes: 12 additions & 8 deletions python/composio/tools/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
MetadataType = t.Dict[_KeyType, t.Dict]
ParamType = t.TypeVar("ParamType")
ProcessorType = te.Literal["pre", "post", "schema"]

AUTH_SCHEMES = ("OAUTH2", "OAUTH1", "API_KEY", "BASIC", "BEARER_TOKEN")
AuthSchemeType = t.Literal["OAUTH2", "OAUTH1", "API_KEY", "BASIC", "BEARER_TOKEN"]


Expand Down Expand Up @@ -1144,12 +1146,7 @@ def get_auth_scheme_for_app(
if auth_scheme is not None:
return auth_schemes[auth_scheme]

for scheme in (
"OAUTH2",
"OAUTH1",
"API_KEY",
"BASIC",
):
for scheme in AUTH_SCHEMES:
if scheme in auth_schemes:
return auth_schemes[scheme]

Expand Down Expand Up @@ -1293,7 +1290,7 @@ def fetch_expected_integration_params(
def create_integration(
self,
app: AppType,
auth_mode: t.Optional[str] = None,
auth_mode: t.Optional[AuthSchemeType] = None,
auth_config: t.Optional[t.Dict[str, t.Any]] = None,
use_composio_oauth_app: bool = True,
force_new_integration: bool = False,
Expand All @@ -1317,19 +1314,26 @@ def initiate_connection(
entity_id: t.Optional[str] = None,
redirect_url: t.Optional[str] = None,
connected_account_params: t.Optional[t.Dict] = None,
*,
auth_scheme: t.Optional[AuthSchemeType] = None,
) -> ConnectionRequestModel:
if integration_id is None and app is None:
raise ComposioSDKError(
message="Both `integration_id` and `app` cannot be None"
)

if auth_scheme is not None:
if auth_scheme not in AUTH_SCHEMES:
raise ComposioSDKError(f"'auth_scheme' must be one of {AUTH_SCHEMES}")

if integration_id is None:
try:
integration_id = self._get_integration_for_app(
app=t.cast(
AppType,
app,
)
),
auth_scheme=auth_scheme,
).id
except NoItemsFound as e:
raise ComposioSDKError(
Expand Down
Loading