Skip to content

Adding OAuth revoke and introspect endpoints #261

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions notion_client/api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,29 @@ def list(self, **kwargs: Any) -> SyncAsync[Any]:
query=pick(kwargs, "block_id", "start_cursor", "page_size"),
auth=kwargs.get("auth"),
)


class OAuthEndpoint(Endpoint):
def introspect(self, **kwargs: Any) -> SyncAsync[Any]:
"""Introspect the provided token.

*[🔗 Endpoint documentation](https://developers.notion.com/reference/introspect-token)*
"""
return self.parent.request(
path="oauth/introspect",
method="POST",
body=pick(kwargs, "token"),
auth=kwargs.get("auth"),
)

def revoke(self, **kwargs: Any) -> SyncAsync[Any]:
"""Revoke the provided token.

*[🔗 Endpoint documentation](https://developers.notion.com/reference/revoke-token)*
"""
return self.parent.request(
path="oauth/revoke",
method="POST",
body=pick(kwargs, "token"),
auth=kwargs.get("auth"),
)
2 changes: 2 additions & 0 deletions notion_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PagesEndpoint,
SearchEndpoint,
UsersEndpoint,
OAuthEndpoint,
)
from notion_client.errors import (
APIResponseError,
Expand Down Expand Up @@ -77,6 +78,7 @@ def __init__(
self.pages = PagesEndpoint(self)
self.search = SearchEndpoint(self)
self.comments = CommentsEndpoint(self)
self.oauth = OAuthEndpoint(self)

@property
def client(self) -> Union[httpx.Client, httpx.AsyncClient]:
Expand Down
29 changes: 29 additions & 0 deletions tests/cassettes/test_introspect_token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interactions:
- request:
body: '{"token": "ntn_..."}'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
authorization:
- Basic '"$BASE64_ENCODED_ID_AND_SECRET"'
connection:
- keep-alive
content-length:
- '63'
content-type:
- application/json
host:
- api.notion.com
notion-version:
- '2022-06-28'
method: POST
uri: https://api.notion.com/v1/oauth/introspect
response:
content: '{"active":true,"scope":"read_content insert_content update_content read_user_with_email
read_user_without_email","iat":1742002165921,"request_id":"14314d51-f34c-47a0-886b-4d792d9dad0c"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
version: 1
28 changes: 28 additions & 0 deletions tests/cassettes/test_revoke_token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
interactions:
- request:
body: '{"token": "ntn..."}'
headers:
accept:
- '*/*'
accept-encoding:
- gzip, deflate
authorization:
- Basic '"$BASE64_ENCODED_ID_AND_SECRET"'
connection:
- keep-alive
content-length:
- '63'
content-type:
- application/json
host:
- api.notion.com
notion-version:
- '2022-06-28'
method: POST
uri: https://api.notion.com/v1/oauth/revoke
response:
content: '{"request_id":"ba61313e-c752-49bc-858d-0d961eda422e"}'
headers: {}
http_version: HTTP/1.1
status_code: 200
version: 1
12 changes: 12 additions & 0 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,15 @@ def test_pages_delete(client, page_id):
assert response

client.pages.update(page_id=page_id, archived=False)


@pytest.mark.vcr()
def test_revoke_token(client, token):
response = client.oauth.revoke(token=token)
assert response


@pytest.mark.vcr()
def test_introspect_token(client, token):
response = client.oauth.introspect(token=token)
assert response