From bc9bc8b1b841c5ab7794a848afc4483d174f2d8d Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Fri, 29 Dec 2023 10:33:10 -0500 Subject: [PATCH] Move `Grants` to `NylasClient` and custom authentication to `Auth` (#324) * Move custom auth out of grants to auth * move grants entry point out of auth to nylasclient * Update CHANGELOG.md --- CHANGELOG.md | 2 ++ nylas/client.py | 12 ++++++++++++ nylas/resources/auth.py | 32 ++++++++++++++++++++++---------- nylas/resources/grants.py | 18 ------------------ 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f5bfbe..e9f5164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ nylas-python Changelog v6.0.0b8 ---------------- +* **BREAKING CHANGES**: Moved grants API out of `Auth` to `NylasClient` +* **BREAKING CHANGES**: Moved `Grants.create()` to `Auth.customAuthentication()` * Added helper function for attaching files to messages * Fixed issues with sending messages and creating drafts diff --git a/nylas/client.py b/nylas/client.py index 19c2bf6..0707487 100644 --- a/nylas/client.py +++ b/nylas/client.py @@ -1,3 +1,5 @@ +from nylas.resources.grants import Grants + from nylas.config import DEFAULT_SERVER_URL from nylas.handler.http_client import HttpClient from nylas.resources.applications import Applications @@ -107,6 +109,16 @@ def folders(self) -> Folders: """ return Folders(self.http_client) + @property + def grants(self) -> Grants: + """ + Access the Grants API. + + Returns: + The Grants API. + """ + return Grants(self.http_client) + @property def messages(self) -> Messages: """ diff --git a/nylas/resources/auth.py b/nylas/resources/auth.py index 2c6a90a..711c027 100644 --- a/nylas/resources/auth.py +++ b/nylas/resources/auth.py @@ -3,6 +3,8 @@ import urllib.parse import uuid +from nylas.models.grant import CreateGrantRequest, Grant + from nylas.models.auth import ( CodeExchangeResponse, PkceAuthUrl, @@ -57,16 +59,6 @@ def _build_query_with_admin_consent(config: dict) -> dict: class Auth(Resource): - @property - def grants(self) -> Grants: - """ - Access the Grants API. - - Returns: - The Grants API. - """ - return Grants(self._http_client) - def url_for_oauth2(self, config: URLForAuthenticationConfig) -> str: """ Build the URL for authenticating users to your application via Hosted Authentication. @@ -99,6 +91,26 @@ def exchange_code_for_token( return self._get_token(request_body) + def custom_authentication( + self, request_body: CreateGrantRequest + ) -> Response[Grant]: + """ + Create a Grant via Custom Authentication. + + Args: + request_body: The values to create the Grant with. + + Returns: + The created Grant. + """ + + json_response = self._http_client._execute( + method="POST", + path=f"/v3/connect/custom", + request_body=request_body, + ) + return Response.from_dict(json_response, Grant) + def refresh_access_token( self, request: TokenExchangeRequest ) -> CodeExchangeResponse: diff --git a/nylas/resources/grants.py b/nylas/resources/grants.py index dfc5e4e..0271bec 100644 --- a/nylas/resources/grants.py +++ b/nylas/resources/grants.py @@ -1,14 +1,12 @@ from nylas.handler.api_resources import ( ListableApiResource, FindableApiResource, - CreatableApiResource, UpdatableApiResource, DestroyableApiResource, ) from nylas.models.grants import ( Grant, ListGrantsQueryParams, - CreateGrantRequest, UpdateGrantRequest, ) from nylas.models.response import Response, ListResponse, DeleteResponse @@ -17,7 +15,6 @@ class Grants( ListableApiResource, FindableApiResource, - CreatableApiResource, UpdatableApiResource, DestroyableApiResource, ): @@ -51,21 +48,6 @@ def find(self, grant_id: str) -> Response[Grant]: path=f"/v3/grants/{grant_id}", response_type=Grant ) - def create(self, request_body: CreateGrantRequest) -> Response[Grant]: - """ - Create a Grant via Custom Authentication. - - Args: - request_body: The values to create the Grant with. - - Returns: - The created Grant. - """ - - return super(Grants, self).create( - path=f"/v3/connect/custom", response_type=Grant, request_body=request_body - ) - def update( self, grant_id: str, request_body: UpdateGrantRequest ) -> Response[Grant]: