Utilities

Cache Data Storage

class pyryver.cache_storage.AbstractCacheStorage

Bases: abc.ABC

An abstract class defining the requirements for cache storages.

A cache storage is used by the Ryver class to cache chats data to improve performance.

abstractmethod load(ryver: Ryver, obj_type: str) → List[pyryver.objects.Object]

Load all saved objects of a specific type.

If no objects were saved, this method returns an empty list.

abstractmethod save(obj_type: str, data: List[pyryver.objects.Object]) → None

Save all objects of a specific type.

class pyryver.cache_storage.FileCacheStorage(root_dir: str = '.', prefix: str = '')

Bases: pyryver.cache_storage.AbstractCacheStorage

A cache storage implementation using files.

load(ryver: Ryver, obj_type: str) → List[pyryver.objects.Object]

Load all saved objects of a specific type.

If no objects were saved, this method returns an empty list.

save(obj_type: str, data: List[pyryver.objects.Object]) → None

Save all objects of a specific type.

API Helpers

async for ... in pyryver.util.get_all(session: aiohttp.client.ClientSession, url: str, top: int = -1, skip: int = 0, param_sep: str = '?') → List[dict]

Because the REST API only gives 50 results at a time, this function is used to retrieve all objects.

Intended for internal use only.

pyryver.util.get_type_from_entity(entity_type: str) → str

Gets the object type from the entity type.

Note that it doesn’t actually return a class, just the string.

Intended for internal use only.

await pyryver.util.retry_until_available(coro: Awaitable[T], *args, timeout: float = None, **kwargs) → T

Repeatedly tries to do some action (usually getting a resource) until the resource becomes available or a timeout elapses.

This function will try to run the given coroutine once every 0.5 seconds. If it results in a 404, the function tries again. Otherwise, the exception is raised.

If it times out, an asyncio.TimeoutError will be raised.

args and kwargs are passed to the coroutine.

Parameters:
  • coro – The coroutine to run
  • timeout – The timeout in seconds, or None for no timeout
pyryver.objects.get_obj_by_field(objs: List[pyryver.objects.Object], field: str, value: Any) → pyryver.objects.Object

Gets an object from a list of objects by a field.

For example, this function can find a chat with a specific nickname in a list of chats.

Parameters:
  • objs – List of objects to search in.
  • field – The field’s name (usually a constant beginning with FIELD_ in pyryver.util) within the object’s JSON data.
  • value – The value to look for.

Entity Types

pyryver.util.TYPE_USER

Corresponds to pyryver.objects.User.

pyryver.util.TYPE_FORUM

Corresponds to pyryver.objects.Forum.

pyryver.util.TYPE_TEAM

Corresponds to pyryver.objects.Team.

pyryver.util.TYPE_GROUPCHAT_MEMBER

Corresponds to pyryver.objects.GroupChatMember.

pyryver.util.TYPE_TOPIC

Corresponds to pyryver.objects.Topic.

pyryver.util.TYPE_TOPIC_REPLY

Corresponds to pyryver.objects.TopicReply.

pyryver.util.TYPE_NOTIFICATION

Corresponds to pyryver.objects.Notification.

pyryver.util.TYPE_STORAGE

Corresponds to pyryver.objects.Storage.

pyryver.util.TYPE_FILE

Corresponds to pyryver.objects.File.

Common Field Names

pyryver.util.FIELD_USERNAME
pyryver.util.FIELD_EMAIL_ADDR
pyryver.util.FIELD_DISPLAY_NAME

The object’s display name (friendly name)

pyryver.util.FIELD_NAME
pyryver.util.FIELD_NICKNAME
pyryver.util.FIELD_ID

The object’s ID, sometimes an int, sometimes a str depending on the object type.

pyryver.util.FIELD_JID

The object’s JID, or JabberID. Used in the live socket interface for referring to chats.