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

Nylas credentials API #290

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions nylas/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 @@ -52,6 +53,16 @@ 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)

kraju3 marked this conversation as resolved.
Show resolved Hide resolved
@property
def calendars(self) -> Calendars:
"""
Expand Down
82 changes: 82 additions & 0 deletions nylas/models/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from dataclasses import dataclass
from type import List, Any, Dict, Optional, Literal, Union, NotRequired

from dataclasses_json import dataclass_json
from typing_extensions import TypedDict


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

@dataclass_json
@dataclass
class Credential:
"""
Interface representing a Nylas Credential object.
Attributes
id: Globally unique object identifier;
name: Name of the credential
credential_type: The type of credential
hashed_data: Hashed value of the credential that you created
created_at: Timestamp of when the credential was created
updated_at: Timestamp of when the credential was updated;
"""
id: str
name: str
credential_type: Optional[CredentialType]
hashed_data: Optional[str] = None
created_at: Optional[int] = None
updated_at: Optional[int] = None


class MicrosoftAdminConsentSettings(TypedDict):
client_id: str
client_secret: str
kraju3 marked this conversation as resolved.
Show resolved Hide resolved


class CreateCredentialBaseRequest(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]
kraju3 marked this conversation as resolved.
Show resolved Hide resolved


class ListCredentialQueryParams(TypedDict):
"""
Interface representing the query parameters for credentials .

Attributes:
offset: Offset results
sort_by: Sort entries by field name
order_by: Order results by the specified field.
Currently only start is supported.
limit: The maximum number of objects to return.
This field defaults to 50. The maximum allowed value is 200.
"""

limit: NotRequired[int]
offset: NotRequired[int]
order_by: NotRequired[str]
sort_by: NotRequired[str]


CredentialRequest = Union[AdminConsentCredential, GoogleServiceAccountCredential, ConnectorOverrideCredential]
98 changes: 98 additions & 0 deletions nylas/resources/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from nylas.handler.api_resources import (
ListableApiResource,
FindableApiResource,
CreatableApiResource,
UpdatableApiResource,
DestroyableApiResource,
)
from nylas.models.auth import Provider
from nylas.models.credentials import (
Credential,
CredentialRequest, ListCredentialQueryParams
)
from nylas.models.response import Response, ListResponse, DeleteResponse


class Credentials(
ListableApiResource,
FindableApiResource,
CreatableApiResource,
UpdatableApiResource,
DestroyableApiResource,
):
def list(self, provider: Provider, query_params: ListCredentialQueryParams) -> ListResponse[Credential]:
"""
Return all credentials for a particular provider.

Args:
provider: The provider.
query_params: The query parameters to include in the request.

Returns:
The list of credentials.
"""

return super(Credentials, self).list(path=f"/v3/connectors/{provider}/creds", response_type=Credential,
query_params=query_params)

def find(self, provider: Provider, credential_id: str) -> Response[Credential]:
"""
Return a credential.

Args:
provider: The provider of the credential.
credential_id: The ID of the credential to retrieve.

Returns:
The Credential.
"""

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

def create(self, request_body: CredentialRequest) -> Response[Credential]:
"""
Create a credential for a particular provider.

Args:
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
)

def update(self, credential_id: str, request_body: CredentialRequest) -> Response[Credential]:
"""
Update a credential.

Args:
credential_id: The ID of the credential to update.
request_body: The values to update the credential with.

Returns:
The updated credential.
"""

return super(Credentials, self).update(
path=f"/v3/connectors/{request_body.get('provider')}/creds/{credential_id}", response_type=Credential,
request_body=request_body
)
kraju3 marked this conversation as resolved.
Show resolved Hide resolved

def destroy(self, provider: Provider, credential_id: str) -> DeleteResponse[Credential]:
"""
Delete a credential.

Args:
provider: the provider for the grant
credential_id: The ID of the credential to delete.

Returns:
The deletion response.
"""

return super(Credentials, self).destroy(path=f"/v3/connectors/{provider}/creds/{credential_id}")
Loading