App Context API for Python-Asyncio SDK
App Context lets a PubNub client for Python-Asyncio store metadata about users and channels, and the memberships that link them, without running a database. The methods live on the PubNubAsyncio client, and the User ID parameter is named uuid.
This page describes App Context (formerly Objects v2). To upgrade from Objects v1, see the migration guide.
The methods fall into four groups:
- User metadata: Get metadata for all users, Get user metadata, Set user metadata, and Remove user metadata.
- Channel metadata: Get metadata for all channels, Get channel metadata, Set channel metadata, and Remove channel metadata.
- Channel memberships: Get channel memberships, Set channel memberships, Remove channel memberships, and Manage channel memberships.
- Channel members: Get channel members, Set channel members, Remove channel members, and Manage channel members.
PubNub publishes an App Context event when a set or delete call succeeds. See App Context events.
A builder sends its request when you await its future(). A request that fails, or fails validation, does not raise an exception. future() resolves with a PubNubAsyncioException whose result is None and whose status holds the error. Call is_error() on the awaited value before you read result.
Each keyword argument is also a builder method of the same name, except where a table says otherwise.
The builder methods include_custom(), include_status(), include_type(), include_total_count(), include_channel(), and include_uuid() are deprecated since 10.1.0 and issue a DeprecationWarning, but they still work. Use the keyword arguments of the same names on the user and channel metadata methods. On the membership and member methods, use the include parameter with MembershipIncludes or MemberIncludes instead. Keyword arguments with the same names are not deprecated.
The methods take channel and user ID strings, not SDK channel entities.
Get metadata for all users
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.get_all_uuid_metadata() returns a paginated list of the user metadata objects stored in App Context, through a PubNub client for Python-Asyncio. Each object holds the profile fields of a user and, on request, its custom data.
Required keyset configuration
include_total_count#- Whether the result includes
total_count, the number of user metadata objects that match the request. Without it,total_countisNone. include_custom#- Whether each user metadata object in the response includes its
customobject. include_status#- Whether each user metadata object in the response includes its
statusfield. include_type#- Whether each user metadata object in the response includes its
typefield. filter#- The filter expression. The response includes only the user metadata objects that match it. See filtering.
sort_keys#- The sort order of the result, as a list of
PNSortKeyobjects. Build each one withPNSortKey.asc()orPNSortKey.desc()and aPNSortKeyValueof ID, NAME, or UPDATED. The builder method issort(), and it takes the keys as separate arguments. limit#- The maximum number of user metadata objects to return in one response. The maximum value is
100. page#- The page to return, as a
NextorPreviousobject frompubnub.models.consumer.objects_v2.page. Pass thenextorprevproperty of an earlier result, which already has that type. The keyword arguments ofget_all_uuid_metadata()do not includepage, so set it with the builder methodpage().
Sample code
Returns
get_all_uuid_metadata() resolves with an AsyncioEnvelope whose result is a PNGetAllUUIDMetadataResult and whose status is a PNStatus.
PNGetAllUUIDMetadataResult
status#- The HTTP status code of the response.
data#- The user metadata objects on this page.
- property of data:
id# - The ID of the user.
- property of data:
name# - The display name of the user, or
Nonewhen unset. - property of data:
externalId# - The ID in an external system of the user, or
Nonewhen unset. - property of data:
profileUrl# - The URL of the profile picture of the user, or
Nonewhen unset. - property of data:
email# - The email address of the user, or
Nonewhen unset. - property of data:
type# - The type of the user, or
Nonewhen unset. The result includes it unlessinclude_typeisFalse. - property of data:
status# - The status of the user, or
Nonewhen unset. The result includes it unlessinclude_statusisFalse. - property of data:
custom# - The custom data of the user. The key is present only when
include_customisTrue. The key holdsNonewhen the user has none. - property of data:
updated# - The date and time of the last update, in ISO 8601 format.
- property of data:
eTag# - The version hash of the user metadata.
total_count#- The number of user metadata objects that match the request, or
Nonewheninclude_total_countis notTrue. next#- A
Nextobject for the next page, orNonewhen there is no next page. prev#- A
Previousobject for the previous page, orNonewhen there is no previous page.
Examples for Get metadata for all users
Syntax
1await pubnub.get_all_uuid_metadata(
2 include_total_count: bool,
3 include_custom: bool,
4 include_status: bool,
5 include_type: bool,
6 filter: str,
7 sort_keys: List[PNSortKey],
8 limit: int
9).future() -> AsyncioEnvelope
Basic usage (Builder Pattern)
1from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
2result = await pubnub.get_all_uuid_metadata(include_custom=True, include_total_count=True) \
3 .limit(10) \
4 .filter("name == 'doc_type'") \
5 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
6 .page(None) \
7 .future()
8
9for uuid_data in result.result.data:
10 print(f"UUID: {uuid_data['id']}")
11 print(f"Name: {uuid_data['name']}")
12 print(f"Custom: {uuid_data['custom']}")
Basic usage (Named Arguments)
1from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
2
3metadata = await pubnub.get_all_uuid_metadata(
4 limit=10,
5 include_custom=True,
6 filter="name == 'doc_type'",
7 include_total_count=True,
8 sort_keys=[PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)]
9).future()
10
11for uuid_data in metadata.result.data:
12 print(f"UUID: {uuid_data['id']}")
13 print(f"Name: {uuid_data['name']}")
14 print(f"Custom: {uuid_data['custom']}")
Get user metadata
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.get_uuid_metadata() returns the metadata of one user from App Context through a PubNub client for Python-Asyncio. The metadata holds the profile fields of the user and, on request, its custom data.
uuid#- The ID of the user to get metadata for. When omitted, the SDK uses the
user_idof the PubNub client. An empty string fails validation with the messageuuid missing or not a string. include_custom#- Whether the response includes the
customobject of the user. include_status#- Whether the result includes the
statusfield of the user. include_type#- Whether the result includes the
typefield of the user.
Sample code
Returns
get_uuid_metadata() resolves with an AsyncioEnvelope whose result is a PNGetUUIDMetadataResult and whose status is a PNStatus.
PNGetUUIDMetadataResult
status#- The HTTP status code of the response.
data#- The user metadata object.
- property of data:
id# - The ID of the user.
- property of data:
name# - The display name of the user, or
Nonewhen unset. - property of data:
externalId# - The ID in an external system of the user, or
Nonewhen unset. - property of data:
profileUrl# - The URL of the profile picture of the user, or
Nonewhen unset. - property of data:
email# - The email address of the user, or
Nonewhen unset. - property of data:
type# - The type of the user, or
Nonewhen unset. The result includes it unlessinclude_typeisFalse. - property of data:
status# - The status of the user, or
Nonewhen unset. The result includes it unlessinclude_statusisFalse. - property of data:
custom# - The custom data of the user. The key is present only when
include_customisTrue. - property of data:
updated# - The date and time of the last update, in ISO 8601 format.
- property of data:
eTag# - The version hash of the user metadata.
Examples for Get user metadata
Syntax
1await pubnub.get_uuid_metadata(
2 uuid: str,
3 include_custom: bool,
4 include_status: bool,
5 include_type: bool
6).future() -> AsyncioEnvelope
Basic usage (Builder Pattern)
1envelope = await pubnub.get_uuid_metadata(include_custom=True).future()
Basic usage (Named Arguments)
1metadata = await pubnub.get_uuid_metadata(include_custom=True).future()
Set user metadata
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.set_uuid_metadata() sets the metadata of one user in App Context through a PubNub client for Python-Asyncio and returns the stored object. The call creates the metadata when the user has none yet.
uuid#- The ID of the user to set metadata for. When omitted, the SDK uses the
user_idof the PubNub client. An empty string fails validation with the messageuuid missing or not a string. name#- The display name of the user. The builder method is
set_name(). external_id#- The ID of the user in an external system.
profile_url#- The URL of the profile picture of the user.
email#- The email address of the user.
type#- The type of the user. The builder method is
set_type(). status#- The status of the user. The builder method is
set_status(). custom#- The custom data of the user. A set call replaces the whole stored
customobject. To add a key, get the metadata first, add the key, and set the complete object. Values can be strings, numbers, or booleans. The builder methodcustom()also setsinclude_customtoTrue, but the keyword argumentcustomdoes not. include_custom#- Whether the response includes the
customobject of the user. include_status#- Whether the result includes the
statusfield of the user. include_type#- Whether the result includes the
typefield of the user. if_matches_etag#- The
eTagof the user metadata that you last read. The update applies only if the stored metadata still has thiseTag. Otherwise PubNub rejects the update with HTTP status 412. Get theeTagfrom the response ofget_uuid_metadata(). The keyword arguments do not include it, so set it with the builder methodif_matches_etag(), available since 10.2.0.
The SDK sends name, external_id, email, and profile_url as null when you leave them out. Pass every field that you want to keep, because a set call without name clears the stored value.
API limits
To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.
Sample code
Returns
set_uuid_metadata() resolves with an AsyncioEnvelope whose result is a PNSetUUIDMetadataResult and whose status is a PNStatus.Its data property holds the stored user metadata and has the same properties as the data property of the Get user metadata response.PNSetUUIDMetadataResult
Examples for Set user metadata
Syntax
1await pubnub.set_uuid_metadata(
2 uuid: str,
3 name: str,
4 external_id: str,
5 profile_url: str,
6 email: str,
7 type: str,
8 status: str,
9 custom: dict,
10 include_custom: bool,
11 include_status: bool,
12 include_type: bool
13).future() -> AsyncioEnvelope
Basic usage (Builder Pattern)
1envelope = await pubnub.set_uuid_metadata(include_custom=True) \
2 .uuid("some_uuid") \
3 .set_name("Some Name") \
4 .set_status("Active") \
5 .set_type("User") \
6 .email("test@example.com") \
7 .profile_url("http://example.com") \
8 .external_id("1234567890") \
9 .custom({"key1": "val1", "key2": "val2"}) \
10 .future()
Basic usage (Named Arguments)
1envelope = await pubnub.set_uuid_metadata(uuid="some_uuid_named",
2 name="Some Name",
3 status="Active", type="User",
4 email="test@example.com",
5 profile_url="http://example.com",
6 external_id="1234567890",
7 custom={"key1": "val1", "key2": "val2"},
8 include_custom=True) \
9 .future()
Remove user metadata
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.remove_uuid_metadata() deletes the metadata of one user from App Context through a PubNub client for Python-Asyncio. Deleting the metadata deletes the memberships of the user only when the keyset enforces referential integrity.
uuid#- The ID of the user to delete metadata for. When omitted, the SDK uses the
user_idof the PubNub client. An empty string fails validation with the messageuuid missing or not a string.
Sample code
Returns
remove_uuid_metadata() resolves with an AsyncioEnvelope whose result is a PNRemoveUUIDMetadataResult and whose status is a PNStatus.
PNRemoveUUIDMetadataResult
Examples for Remove user metadata
Syntax
1await pubnub.remove_uuid_metadata(
2 uuid: str
3).future() -> AsyncioEnvelope
Basic usage (Builder Pattern)
1envelope = await pubnub.remove_uuid_metadata() \
2 .uuid("some_uuid").future()
Basic usage (Named Arguments)
1envelope = await pubnub.remove_uuid_metadata(uuid="some_uuid_named").future()
Get metadata for all channels
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.get_all_channel_metadata() returns a paginated list of the channel metadata objects stored in App Context, through a PubNub client for Python-Asyncio. Each object holds the name and description of a channel and, on request, its custom data.
Required keyset configuration
include_total_count#- Whether the result includes
total_count, the number of channel metadata objects that match the request. Without it,total_countisNone. include_custom#- Whether each channel metadata object in the response includes its
customobject. include_status#- Whether each channel metadata object in the response includes its
statusfield. include_type#- Whether each channel metadata object in the response includes its
typefield. filter#- The filter expression. The response includes only the channel metadata objects that match it. See filtering.
sort_keys#- The sort order of the result, as a list of
PNSortKeyobjects. Build each one withPNSortKey.asc()orPNSortKey.desc()and aPNSortKeyValueof ID, NAME, or UPDATED. The builder method issort(), and it takes the keys as separate arguments. limit#- The maximum number of channel metadata objects to return in one response. The maximum value is
100. page#- The page to return, as a
NextorPreviousobject frompubnub.models.consumer.objects_v2.page. Pass thenextorprevproperty of an earlier result, which already has that type.
Sample code
Returns
get_all_channel_metadata() resolves with an AsyncioEnvelope whose result is a PNGetAllChannelMetadataResult and whose status is a PNStatus.
PNGetAllChannelMetadataResult
status#- The HTTP status code of the response.
data#- The channel metadata objects on this page.
- property of data:
id# - The ID of the channel.
- property of data:
name# - The name of the channel, or
Nonewhen unset. - property of data:
description# - The description of the channel, or
Nonewhen unset. - property of data:
type# - The type of the channel, or
Nonewhen unset. The result includes it unlessinclude_typeisFalse. - property of data:
status# - The status of the channel, or
Nonewhen unset. The result includes it unlessinclude_statusisFalse. - property of data:
custom# - The custom data of the channel. The key is present only when
include_customisTrue. The key holdsNonewhen the channel has none. - property of data:
updated# - The date and time of the last update, in ISO 8601 format.
- property of data:
eTag# - The version hash of the channel metadata.
total_count#- The number of channel metadata objects that match the request, or
Nonewheninclude_total_countis notTrue. next#- A
Nextobject for the next page, orNonewhen there is no next page. prev#- A
Previousobject for the previous page, orNonewhen there is no previous page.
Examples for Get metadata for all channels
Syntax
1await pubnub.get_all_channel_metadata(
2 include_total_count: bool,
3 include_custom: bool,
4 include_status: bool,
5 include_type: bool,
6 filter: str,
7 sort_keys: List[PNSortKey],
8 limit: int,
9 page: PNPage
10).future() -> AsyncioEnvelope
Basic usage (Builder Pattern)
1from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
2envelope = await pubnub.get_all_channel_metadata(include_custom=True, include_total_count=True) \
3 .limit(10) \
4 .filter("name == 'doc_type'") \
5 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
6 .page(None) \
7 .future()
Basic usage (Named Arguments)
1from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
2
3metadata = await pubnub.get_all_channel_metadata(limit=10,
4 include_custom=True,
5 filter="name == 'doc_type'",
6 include_total_count=True,
7 sort_keys=[PNSortKey.asc(PNSortKeyValue.ID),
8 PNSortKey.desc(PNSortKeyValue.UPDATED)]) \
9 .future()
Get channel metadata
App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal.
Requires App Context add-on
This method requires that the App Context add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
pubnub.get_channel_metadata() returns the metadata of one channel from App Context through a PubNub client for Python-Asyncio. The metadata holds the name and description of the channel and, on request, its custom data.
channelrequired#- The ID of the channel to get metadata for. A request without a channel fails validation with the message
Channel missing. include_custom#- Whether the response includes the
customobject of the channel. include_status#- Whether the result includes the
statusfield of the channel. include_type#- Whether the result includes the
typefield of the channel.
Sample code
Returns
get_channel_metadata() resolves with an AsyncioEnvelope whose result is a PNGetChannelMetadataResult and whose status is a PNStatus.