Skip to content

Commit d18bc9a

Browse files
authored
added get_avatar method (#149)
After this we can write docs and guides 🥳 Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 0d5b0bc commit d18bc9a

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [0.4.0 - 2023-10-2x]
5+
## [0.4.0 - 2023-10-15]
66

77
As the project moves closer to `beta`, final unification changes are being made.
88
This release contains some breaking changes in `users`, `notifications` API.
99

1010
### Added
1111

12+
- Support for users avatars(`get_avatar`). #149
1213
- `__repr__` method added for most objects(previously it was only present for `FsNode`). #147
1314

1415
### Changed
@@ -21,7 +22,7 @@ This release contains some breaking changes in `users`, `notifications` API.
2122
### Fixed
2223

2324
- `users.get_details` with empty parameter in some cases was raised exception.
24-
- ClientMode: in case when LDAP was used as user backend, user login differs from user_id and most API failed with 404. #148
25+
- ClientMode: in case when LDAP was used as user backend, user login differs from `user id`, and most API failed with 404. #148
2526

2627
## [0.3.1 - 2023-10-07]
2728

nc_py_api/users.py

+18
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,21 @@ def demote_from_subadmin(self, user_id: str, group_id: str) -> None:
287287
:param group_id: group where user should be removed from administrators.
288288
"""
289289
self._session.ocs(method="DELETE", path=f"{self._ep_base}/{user_id}/subadmins", params={"groupid": group_id})
290+
291+
def get_avatar(
292+
self, user_id: str = "", size: typing.Literal[64, 512] = 512, dark: bool = False, guest: bool = False
293+
) -> bytes:
294+
"""Returns user avatar binary data.
295+
296+
:param user_id: The ID of the user whose avatar should be returned.
297+
.. note:: To return the current user's avatar, leave the field blank.
298+
:param size: Size of the avatar. Currently supported values: ``64`` and ``512``.
299+
:param dark: Flag indicating whether a dark theme avatar should be returned or not.
300+
:param guest: Flag indicating whether user ID is a guest name or not.
301+
"""
302+
if not user_id and not guest:
303+
user_id = self._session.user
304+
url_path = f"/index.php/avatar/{user_id}/{size}" if not guest else f"/index.php/avatar/guest/{user_id}/{size}"
305+
if dark:
306+
url_path += "/dark"
307+
return self._session.request(method="GET", path=url_path).content

tests/actual_tests/users_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import contextlib
22
import datetime
3+
from io import BytesIO
34
from os import environ
45

56
import pytest
7+
from PIL import Image
68

79
from nc_py_api import (
810
NextcloudApp,
@@ -141,3 +143,15 @@ def test_edit_user(nc_client):
141143

142144
def test_resend_user_email(nc_client):
143145
nc_client.users.resend_welcome_email(nc_client.user)
146+
147+
148+
def test_avatars(nc):
149+
im = nc.users.get_avatar()
150+
im_64 = nc.users.get_avatar(size=64)
151+
im_black = nc.users.get_avatar(dark=True)
152+
im_64_black = nc.users.get_avatar(size=64, dark=True)
153+
assert len(im_64) < len(im)
154+
assert len(im_64_black) < len(im_black)
155+
for i in (im, im_64, im_black, im_64_black):
156+
img = Image.open(BytesIO(i))
157+
img.load()

0 commit comments

Comments
 (0)