Skip to content

Commit

Permalink
added get_avatar method (#149)
Browse files Browse the repository at this point in the history
After this we can write docs and guides 🥳

Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Oct 15, 2023
1 parent 0d5b0bc commit d18bc9a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

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

## [0.4.0 - 2023-10-2x]
## [0.4.0 - 2023-10-15]

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

### Added

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

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

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

## [0.3.1 - 2023-10-07]

Expand Down
18 changes: 18 additions & 0 deletions nc_py_api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,21 @@ def demote_from_subadmin(self, user_id: str, group_id: str) -> None:
:param group_id: group where user should be removed from administrators.
"""
self._session.ocs(method="DELETE", path=f"{self._ep_base}/{user_id}/subadmins", params={"groupid": group_id})

def get_avatar(
self, user_id: str = "", size: typing.Literal[64, 512] = 512, dark: bool = False, guest: bool = False
) -> bytes:
"""Returns user avatar binary data.
:param user_id: The ID of the user whose avatar should be returned.
.. note:: To return the current user's avatar, leave the field blank.
:param size: Size of the avatar. Currently supported values: ``64`` and ``512``.
:param dark: Flag indicating whether a dark theme avatar should be returned or not.
:param guest: Flag indicating whether user ID is a guest name or not.
"""
if not user_id and not guest:
user_id = self._session.user
url_path = f"/index.php/avatar/{user_id}/{size}" if not guest else f"/index.php/avatar/guest/{user_id}/{size}"
if dark:
url_path += "/dark"
return self._session.request(method="GET", path=url_path).content
14 changes: 14 additions & 0 deletions tests/actual_tests/users_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import contextlib
import datetime
from io import BytesIO
from os import environ

import pytest
from PIL import Image

from nc_py_api import (
NextcloudApp,
Expand Down Expand Up @@ -141,3 +143,15 @@ def test_edit_user(nc_client):

def test_resend_user_email(nc_client):
nc_client.users.resend_welcome_email(nc_client.user)


def test_avatars(nc):
im = nc.users.get_avatar()
im_64 = nc.users.get_avatar(size=64)
im_black = nc.users.get_avatar(dark=True)
im_64_black = nc.users.get_avatar(size=64, dark=True)
assert len(im_64) < len(im)
assert len(im_64_black) < len(im_black)
for i in (im, im_64, im_black, im_64_black):
img = Image.open(BytesIO(i))
img.load()

0 comments on commit d18bc9a

Please sign in to comment.