App Context API for Python SDK
This page describes App Context (formerly Objects v2). To upgrade from Objects v1, refer to the migration guide.
App Context provides easy-to-use, serverless storage for user and channel data you need to build innovative, reliable, scalable applications. Use App Context to store metadata about your application users and channels, and their membership associations, without the need to stand up your own databases.
PubNub also triggers events when object data is changed: set, updated, or removed from the database. Making a request to set the same data that already exists doesn't trigger an event. Clients can receive these events in real time and update their front-end application accordingly.
Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
-
.sync()returns anEnvelopeobject, which has two fields:Envelope.result, whose type differs for each API, andEnvelope.statusof typePnStatus.1pubnub.publish() \
2 .channel("myChannel") \
3 .message("Hello from PubNub Python SDK") \
4 .sync() -
.pn_async(callback)returnsNoneand passes the values ofEnvelope.resultandEnvelope.statusto a callback you must define beforehand.1def my_callback_function(result, status):
2 print(f'TT: {result.timetoken}, status: {status.category.name}')
3
4pubnub.publish() \
5 .channel("myChannel") \
6 .message("Hello from PubNub Python SDK") \
7 .pn_async(my_callback_function)
User
Manage UUID metadata: list, fetch, set, and remove. Keep responses small by including only the fields you need.
Get metadata for all users
Get a paginated list of UUID metadata. Use filters and sorting to narrow results.
Required keyset configuration
Method(s)
To Get All UUID Metadata you can use the following method(s) in the Python SDK:
1pubnub.get_all_uuid_metadata() \
2 .limit(Integer) \
3 .page(PNPage Object) \
4 .filter(String) \
5 .sort(List<PNSortKey>) \
6 .include_total_count(Boolean) \
7 .include_custom(Boolean) \
8 .include_status(Boolean) \
9 .include_type(Boolean)
| Parameter | Description |
|---|---|
limitType: Integer Default: N/A | The maximum number of objects to retrieve at a time. |
pageType: PNPageDefault: N/A | The paging object used for pagination. |
filterType: String Default: N/A | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sortType: List<PNSortKey>Default: N/A | List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}. |
include_total_countType: Boolean Default: False | Whether to include the total count in the paginated response. Default is false. |
include_customType: Boolean Default: False | Whether to include the Custom object in the response. |
include_statusType: Boolean Default: True | Whether to include the status field in the fetch response. Setting this to False will prevent this value from being returned. |
include_typeType: Boolean Default: True | Whether to include the type field in the fetch response. Setting this to False will prevent this value from being returned. |
Sample code
Reference code
- Builder Pattern
- Named Arguments
Synchronous:
1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5from pubnub.exceptions import PubNubException
6
7
8def get_all_uuid_metadata(pubnub: PubNub):
9 try:
10 result = pubnub.get_all_uuid_metadata() \
11 .include_custom(True) \
12 .limit(10) \
13 .include_total_count(True) \
14 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
15 .page(None) \
show all 43 linesAsynchronous:
1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5
6
7def callback(response, status):
8 if status.is_error():
9 print(f"Error: {status.error_data}")
10 else:
11 for uuid_data in response.data:
12 print(f"UUID: {uuid_data["id"]}")
13 print(f"Name: {uuid_data["name"]}")
14 print(f"Custom: {uuid_data["custom"]}")
15
show all 43 linesSynchronous:
1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5from pubnub.exceptions import PubNubException
6
7
8def get_all_uuid_metadata(pubnub: PubNub):
9 try:
10 metadata = pubnub.get_all_uuid_metadata(
11 limit=10,
12 include_custom=True,
13 include_total_count=True,
14 sort_keys=[PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)]
15 ).sync()
show all 41 linesAsynchronous:
1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5
6
7def callback(response, status):
8 if status.is_error():
9 print(f"Error: {status.error_data}")
10 else:
11 for uuid_data in response.data:
12 print(f"UUID: {uuid_data["id"]}")
13 print(f"Name: {uuid_data["name"]}")
14 print(f"Custom: {uuid_data["custom"]}")
15
show all 41 linesReturns
The get_all_uuid_metadata() operation returns an Envelope which contains the following fields:
| Field | Type | Description |
|---|---|---|
| result | PNGetAllUUIDMetadataResult | A detailed object containing the result of the operation. |
| status | PNStatus | A status object with additional information. |
PNGetAllUUIDMetadataResult
| Property Name | Type | Description |
|---|---|---|
data | [] | List of dictionaries containing UUID metadata |
status | PNStatus | Status of the operation |
Each element in data contains a dictionary with UUID metadata.
| Key | Description |
|---|---|
id | UUID |
name | Name associated with UUID object |
externalId | External ID associated with UUID object |
profileUrl | Profile URL associated with UUID object |
email | Email address associated with UUID object |
custom | Custom object associated with UUID object in form of dictionary containing string to string pairs |
status | User status value |
type | User type value |
Get user metadata
Fetch metadata for a single UUID. Include the Custom object if you need custom fields.
Method(s)
To Get UUID Metadata you can use the following method(s) in the Python SDK:
1pubnub.get_uuid_metadata() \
2 .uuid(String) \
3 .include_custom(Boolean)
| Parameter | Description |
|---|---|
uuidType: String Default: pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
include_customType: Boolean Default: False | Whether to include the Custom object in the response. |
include_statusType: Boolean Default: True | Whether to include the status field in the fetch response, which is included by default. |
include_typeType: Boolean Default: True | Whether to include the type field in the fetch response, which is included by default. |
Sample code
- Builder Pattern
- Named Arguments
Synchronous:
1pubnub.get_uuid_metadata() \
2 .include_custom(True) \
3 .sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4pubnub.get_uuid_metadata() \
5 .include_custom(True) \
6 .pn_async(callback)
Synchronous:
1metadata = pubnub.get_uuid_metadata(include_custom=True).sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4metadata = pubnub.get_uuid_metadata(include_custom=True).pn_async(callback)
Returns
The get_uuid_metadata() operation returns an Envelope which contains the following fields:
| Field | Type | Description |
|---|---|---|
| result | PNGetUUIDMetadataResult | A detailed object containing the result of the operation. |
| status | PNStatus | A status object with additional information. |
PNGetUUIDMetadataResult
operation returns a PNGetUUIDMetadataResult which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
data | Dictionary containing UUID metadata | |
status | PNStatus | Status of the operation |
Where each element in data contains a dictionary with UUID metadata.
| Key | Description |
|---|---|
id | UUID |
name | Name associated with UUID object |
externalId | External ID associated with UUID object |
profileUrl | Profile URL associated with UUID object |
email | Email address associated with UUID object |
status | Status value associated with UUID object |
type | Type value associated with UUID object |
custom | Custom object associated with UUID object in form of dictionary containing string to string pairs |
Set user metadata
Create or update metadata for a UUID. Use the eTag to avoid overwriting concurrent updates.
Unsupported partial updates of custom metadata
The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:
- Get the existing metadata and store it locally.
- Append the new custom metadata to the existing one.
- Set the entire updated custom object.
Method(s)
To Set UUID Metadata you can use the following method(s) in the Python SDK:
1pubnub.set_uuid_metadata() \
2 .uuid(String) \
3 .set_name(String) \
4 .set_status(String) \
5 .set_type(String) \
6 .external_id(String) \
7 .profile_url(String) \
8 .email(String) \
9 .custom(Dictionary) \
10 .include_custom(Boolean) \
11 .include_status(Boolean) \
12 .include_type(Boolean) \
13 .if_matches_etag(String)
| Parameter | Description |
|---|---|
uuidType: String Default: pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
set_nameType: String Default: N/A | Display name for the user. |
set_statusType: String Default: N/A | User status. Max. 50 characters. |
set_typeType: String Default: N/A | User type. Max. 50 characters. |
external_idType: String Default: N/A | User's identifier in an external system. |
profile_urlType: String Default: N/A | The URL of the user's profile picture. |
emailType: String Default: N/A | The user's email address. |
customType: AnyDefault: N/A | Any object of key-value pairs with supported data types. App Context filtering language doesn't support filtering by custom properties. |
include_customType: Boolean Default: False | Whether to include the Custom object in the response. |
include_statusType: Boolean Default: False | Whether to include the status object in the fetch response. |
include_typeType: Boolean Default: False | Whether to include the type object in the fetch response. |
if_matches_etagType: String Default: n/a | The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown. |
API limits
To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.
Sample code
- Builder Pattern
- Named Arguments
Synchronous:
1pubnub.set_uuid_metadata() \
2 .include_custom(True) \
3 .uuid("Some UUID") \
4 .set_name("Some Name") \
5 .set_status("Active") \
6 .set_type("User") \
7 .email("test@example.com") \
8 .profile_url("http://example.com") \
9 .external_id("1234567890") \
10 .custom({"key1": "val1", "key2": "val2"}) \
11 .sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4pubnub.set_uuid_metadata() \
5 .include_custom(True) \
6 .uuid("Some UUID") \
7 .set_name("Some Name") \
8 .set_status("Active") \
9 .set_type("User") \
10 .email("test@example.com") \
11 .profile_url("http://example.com") \
12 .external_id("1234567890") \
13 .custom({"key1": "val1", "key2": "val2"}) \
14 pn_async(callback)
Synchronous:
1pubnub.set_uuid_metadata(uuid="Some UUID",
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 .sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4pubnub.set_uuid_metadata(uuid="Some UUID",
5 name="Some Name",
6 status="Active", type="User",
7 email="test@example.com",
8 profile_url="http://example.com",
9 external_id="1234567890",
10 custom={"key1": "val1", "key2": "val2"}) \
11 .pn_async(callback)
Returns
The set_uuid_metadata() returns a PNSetUUIDMetadataResult which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
data | Dictionary containing UUID metadata | |
status | PNStatus | Status of the operation |
Where each element in data contains a dictionary with UUID metadata.
| Key | Description |
|---|---|
id | UUID |
name | Name associated with UUID object |
externalId | External ID associated with UUID object |
profileUrl | Profile URL associated with UUID object |
email | Email address associated with UUID object |
status | User status |
type | User type |
custom | Custom object associated with UUID object in form of dictionary containing string to string pairs |
Remove user metadata
Delete metadata for the specified UUID.
Method(s)
To Remove UUID Metadata you can use the following method(s) in the Python SDK:
1pubnub.remove_uuid_metadata() \
2 .uuid(String)
| Parameter | Description |
|---|---|
uuidType: String Default: pubnub.configuration.uuid | Unique UUID Metadata identifier. If not supplied, then UUID from configuration will be used. |
Sample code
- Builder Pattern
- Named Arguments
Synchronous:
1pubnub.remove_uuid_metadata() \
2 .uuid("Some UUID").sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4pubnub.remove_uuid_metadata() \
5 .uuid("Some UUID").pn_async(callback)
1pubnub.remove_uuid_metadata(uuid="Some UUID").sync()
Asynchronous:
1def callback(response, status):
2 pass
3
4pubnub.remove_uuid_metadata(uuid="Some UUID").pn_async(callback)
Returns
The remove_uuid_metadata() operation returns an Envelope which contains the following fields:
| Field | Type | Description |
|---|---|---|
| result | PNRemoveUUIDMetadataResult | A detailed object containing the result of the operation. |
| status | PNStatus | A status object with additional information. |
PNRemoveUUIDMetadataResult
| Property Name | Type | Description |
|---|---|---|
status | PNStatus | Status of the operation |
Channel
Manage channel metadata: list, fetch, set, and remove.
Get metadata for all channels
Get a paginated list of channel metadata. Use filters and sorting to narrow results.
Required keyset configuration
Method(s)
To Get All Channel Metadata you can use the following method(s) in the Python SDK:
1pubnub.get_all_channel_metadata() \
2 .limit(Integer) \
3 .page(PNPage) \
4 .filter(String) \
5 .sort(PNSortKey) \
6 .include_total_count(Boolean) \
7 .include_custom(Boolean) \
8 .include_status(Boolean) \
9 .include_type(Boolean)
| Parameter | Description |
|---|---|
limitType: Integer Default: 100 | The maximum number of objects to retrieve at a time. |
pageType: PNPageDefault: N/A | The paging object used for pagination. |
filterType: String Default: N/A | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sortType: [PNSortKey]Default: N/A | List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}. |
include_total_countType: Boolean Default: False | Whether to include the total count in the paginated response. Default is false. |
include_customType: Boolean Default: False | Whether to include the Custom object in the response. |
include_statusType: Boolean Default: True | Whether to include the status field in the fetch response. Setting this to False will prevent this value from being returned. |
include_typeType: Boolean Default: True | Whether to include the type field in the fetch response. Setting this to False will prevent this value from being returned. |