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 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
75 changes: 75 additions & 0 deletions nylas/models/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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, Protocol

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(Protocol):
client_id: str
client_secret: str


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:
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
"""
name: Optional[str]
credential_type: CredentialType
credential_data: CredentialData


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]
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
100 changes: 100 additions & 0 deletions nylas/resources/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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, 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/{provider}/creds", response_type=Credential, request_body=request_body
)

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.

Returns:
The updated credential.
"""

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

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