Skip to content

Commit

Permalink
#28 Add whoami method
Browse files Browse the repository at this point in the history
  • Loading branch information
lastorel committed Aug 7, 2022
1 parent 7133f27 commit a675f44
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [#27](https://github.com/lastorel/pytion/issues/27): add support of `relation` type `Property`
- [#27](https://github.com/lastorel/pytion/issues/27): updates for `relation` type `PropertyValue`
- [#16](https://github.com/lastorel/pytion/issues/17): tests of Property model
- [#28](https://github.com/lastorel/pytion/issues/28): Add whoami method

### Breaking changes

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ There is a list of available methods for communicate with **api.notion.com**. Th

`.block_append(id_, block, blocks)` - Append block or blocks children.

`.get_myself()` - Retrieve my bot User.

`.from_linkto(linkto)` - Creates new Element object based on LinkTo information.

`.from_object(model)` - Creates new Element object from Page, Block or Database object. Usable while Element object contains an Array.
Expand Down Expand Up @@ -209,6 +211,7 @@ There are classes **based on API** structures:
- `User` based on [User object](https://developers.notion.com/reference/user)
- You can create object `User.create(...)` and use it in some properties like `people` type property
- You can retrieve more data about a User by his ID using `.get()`
- use `.get_myself()` to retrieve the current bot User
- `Property` based on [Property object](https://developers.notion.com/reference/property-object)
- You can create object `Property.create(...)` while creating or editing database: `.db_create()` or `.db_update()`
- `formula`, `rollup` type properties configuration is not supported
Expand Down
12 changes: 12 additions & 0 deletions pytion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@ def block_append(
)
return Element(api=self.api, name="blocks", obj=BlockArray(new_blocks["results"]))

def get_myself(self) -> Element:
"""
Retrieves the bot User associated with the API token provided in the authorization header.
:return: Element with User obj
`me = no.users.get_myself()`
"""
new_object = Element(self.api, name="users")
new_object.get("me")
return new_object

def from_linkto(self, linkto: LinkTo, limit: int = 0) -> Optional[Element]:
if not linkto:
logger.error("LinkTo must be provided!")
Expand Down
12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,18 @@ def test_block_append__many(self, no):
removed_block = no.blocks.block_update(block.id, archived=True)
assert removed_block.obj.archived is True

def test_get_myself(self, no):
bot = no.users.get_myself()
assert isinstance(bot.obj, User)
assert bot.obj.type == "bot"
assert bot.obj.name == "Pytion tests"

def test_get_myself__from_obj(self, root_page):
bot = root_page.get_myself()
assert isinstance(bot.obj, User)
assert bot.obj.type == "bot"
assert bot.obj.name == "Pytion tests"

def test_from_linkto__base(self, no):
link = LinkTo.create(page_id="878d628488d94894ab14f9b872cd6870")
page = no.pages.from_linkto(link)
Expand Down

0 comments on commit a675f44

Please sign in to comment.