Skip to content

Commit

Permalink
Add ability to edit own user banner (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
beagold authored Apr 7, 2024
1 parent af31e9b commit 876c53d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/1871.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ability to edit own user banner
4 changes: 4 additions & 0 deletions hikari/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,7 @@ async def edit_my_user(
*,
username: undefined.UndefinedOr[str] = undefined.UNDEFINED,
avatar: undefined.UndefinedNoneOr[files.Resourceish] = undefined.UNDEFINED,
banner: undefined.UndefinedNoneOr[files.Resourceish] = undefined.UNDEFINED,
) -> users.OwnUser:
"""Edit the token's associated user.
Expand All @@ -2577,6 +2578,9 @@ async def edit_my_user(
avatar : undefined.UndefinedNoneOr[hikari.files.Resourceish]
If provided, the new avatar. If [`None`][],
the avatar will be removed.
banner : undefined.UndefinedNoneOr[hikari.files.Resourceish]
If provided, the new banner. If [`None`][],
the banner will be removed.
Returns
-------
Expand Down
8 changes: 8 additions & 0 deletions hikari/impl/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@ async def edit_my_user(
*,
username: undefined.UndefinedOr[str] = undefined.UNDEFINED,
avatar: undefined.UndefinedNoneOr[files.Resourceish] = undefined.UNDEFINED,
banner: undefined.UndefinedNoneOr[files.Resourceish] = undefined.UNDEFINED,
) -> users.OwnUser:
route = routes.PATCH_MY_USER.compile()
body = data_binding.JSONObjectBuilder()
Expand All @@ -2074,6 +2075,13 @@ async def edit_my_user(
async with avatar_resource.stream(executor=self._executor) as stream:
body.put("avatar", await stream.data_uri())

if banner is None:
body.put("banner", None)
elif banner is not undefined.UNDEFINED:
banner_resource = files.ensure_resource(banner)
async with banner_resource.stream(executor=self._executor) as stream:
body.put("banner", await stream.data_uri())

response = await self._request(route, json=body)
assert isinstance(response, dict)
return self._entity_factory.deserialize_my_user(response)
Expand Down
24 changes: 24 additions & 0 deletions tests/hikari/impl/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3519,6 +3519,30 @@ async def test_edit_my_user_when_avatar_is_file(self, rest_client, file_resource
rest_client._request.assert_awaited_once_with(expected_route, json=expected_json)
rest_client._entity_factory.deserialize_my_user.assert_called_once_with({"id": "123"})

async def test_edit_my_user_when_banner_is_None(self, rest_client):
user = StubModel(123)
expected_route = routes.PATCH_MY_USER.compile()
expected_json = {"username": "new username", "banner": None}
rest_client._request = mock.AsyncMock(return_value={"id": "123"})
rest_client._entity_factory.deserialize_my_user = mock.Mock(return_value=user)

assert await rest_client.edit_my_user(username="new username", banner=None) is user

rest_client._request.assert_awaited_once_with(expected_route, json=expected_json)
rest_client._entity_factory.deserialize_my_user.assert_called_once_with({"id": "123"})

async def test_edit_my_user_when_banner_is_file(self, rest_client, file_resource_patch):
user = StubModel(123)
expected_route = routes.PATCH_MY_USER.compile()
expected_json = {"username": "new username", "banner": "some data"}
rest_client._request = mock.AsyncMock(return_value={"id": "123"})
rest_client._entity_factory.deserialize_my_user = mock.Mock(return_value=user)

assert await rest_client.edit_my_user(username="new username", banner="somebanner.png") is user

rest_client._request.assert_awaited_once_with(expected_route, json=expected_json)
rest_client._entity_factory.deserialize_my_user.assert_called_once_with({"id": "123"})

async def test_fetch_my_connections(self, rest_client):
connection1 = StubModel(123)
connection2 = StubModel(456)
Expand Down

0 comments on commit 876c53d

Please sign in to comment.