Skip to content

Commit

Permalink
Update nylas/models/credentials.py
Browse files Browse the repository at this point in the history
Co-authored-by: Mostafa Rashed <[email protected]>

Update nylas/models/credentials.py

Changing typedDic to Protocol for AdminConsentSettings

Co-authored-by: Mostafa Rashed <[email protected]>

Reformating The credential resource

Co-authored-by: Mostafa Rashed <[email protected]>

Moving credentials to auth resource

Removed credential Request

Revert "Removed credential Request"

This reverts commit 519250ccde80632f3d6e69a9280517726e185c1b.

Removed base request

Resolved comments
  • Loading branch information
kraju3 authored and Kiran Raju committed Oct 17, 2023
1 parent 78898fc commit bf54c06
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
11 changes: 0 additions & 11 deletions nylas/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from nylas.resources.applications import Applications
from nylas.resources.auth import Auth
from nylas.resources.calendars import Calendars
from nylas.resources.credentials import Credentials
from nylas.resources.events import Events
from nylas.resources.webhooks import Webhooks

Expand Down Expand Up @@ -53,16 +52,6 @@ def applications(self) -> Applications:
"""
return Applications(self.http_client)

@property
def credentials(self) -> Credentials:
"""
Access the Credentials API.
Returns:
The Credentials API.
"""
return Credentials(self.http_client)

@property
def calendars(self) -> Calendars:
"""
Expand Down
35 changes: 14 additions & 21 deletions nylas/models/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from type import List, Any, Dict, Optional, Literal, Union, NotRequired

from dataclasses_json import dataclass_json
from typing_extensions import TypedDict

from typing_extensions import TypedDict, Protocol

CredentialType = Literal["adminconsent", "serviceaccount", "connector"]


@dataclass_json
@dataclass
class Credential:
Expand All @@ -28,36 +28,32 @@ class Credential:
updated_at: Optional[int] = None


class MicrosoftAdminConsentSettings(TypedDict):
class MicrosoftAdminConsentSettings(Protocol):
client_id: str
client_secret: str


class CreateCredentialBaseRequest(TypedDict):
class GoogleServiceAccountCredential(Protocol):
private_key_id: str
private_key: str
client_email: str


CredentialData = Union[MicrosoftAdminConsentSettings, GoogleServiceAccountCredential, Dict[str, any]]


class CredentialRequest(TypedDict):
"""
Interface representing a request to create a credential.
Attributes:
provider: OAuth provider.
settings: Settings required to create a credential
credential_type: Type of credential you want to create.
credential_data: The data required to successfully create the credential object
"""
provider: str
name: Optional[str]
credential_type: CredentialType


class GoogleServiceAccountCredential(CreateCredentialBaseRequest):
credential_data: Dict[str, any]


class AdminConsentCredential(CreateCredentialBaseRequest):
credential_data: MicrosoftAdminConsentSettings


class ConnectorOverrideCredential(CreateCredentialBaseRequest):
credential_data: Dict[str, any]
credential_data: CredentialData


class ListCredentialQueryParams(TypedDict):
Expand All @@ -77,6 +73,3 @@ class ListCredentialQueryParams(TypedDict):
offset: NotRequired[int]
order_by: NotRequired[str]
sort_by: NotRequired[str]


CredentialRequest = Union[AdminConsentCredential, GoogleServiceAccountCredential, ConnectorOverrideCredential]
12 changes: 12 additions & 0 deletions nylas/resources/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
URLForAdminConsentConfig,
)
from nylas.models.response import Response
from nylas.resources.credentials import Credentials
from nylas.resources.grants import Grants
from nylas.resources.resource import Resource

Expand Down Expand Up @@ -57,6 +58,17 @@ def _build_query_with_admin_consent(config: dict) -> dict:


class Auth(Resource):

@property
def credentials(self) -> Credentials:
"""
Access the Credentials API.
Returns:
The Credentials API.
"""
return Credentials(self.http_client)

@property
def grants(self) -> Grants:
"""
Expand Down
10 changes: 6 additions & 4 deletions nylas/resources/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,28 @@ def find(self, provider: Provider, credential_id: str) -> Response[Credential]:
path=f"/v3/connectors/{provider}/creds/{credential_id}", response_type=Credential
)

def create(self, request_body: CredentialRequest) -> Response[Credential]:
def create(self, provider: Provider, request_body: CredentialRequest) -> Response[Credential]:
"""
Create a credential for a particular provider.
Args:
provider: The provider.
request_body: The values to create the Credential with.
Returns:
The created Credential.
"""

return super(Credentials, self).create(
path=f"/v3/connectors/{request_body.get('provider')}/creds", response_type=Credential, request_body=request_body
path=f"/v3/connectors/{provider}/creds", response_type=Credential, request_body=request_body
)

def update(self, credential_id: str, request_body: CredentialRequest) -> Response[Credential]:
def update(self, provider: Provider, credential_id: str, request_body: CredentialRequest) -> Response[Credential]:
"""
Update a credential.
Args:
provider: The provider.
credential_id: The ID of the credential to update.
request_body: The values to update the credential with.
Expand All @@ -79,7 +81,7 @@ def update(self, credential_id: str, request_body: CredentialRequest) -> Respons
"""

return super(Credentials, self).update(
path=f"/v3/connectors/{request_body.get('provider')}/creds/{credential_id}", response_type=Credential,
path=f"/v3/connectors/{provider}/creds/{credential_id}", response_type=Credential,
request_body=request_body
)

Expand Down

0 comments on commit bf54c06

Please sign in to comment.