Search…
Manage Sudos

Create Sudos

To create a new Sudo:
Swift
Kotlin
TypeScript
1
do {
2
let sudo = Sudo(title: "Mr.",
3
firstName: "John",
4
lastName: "Smith",
5
label: "Shopping",
6
notes: "Used for shopping.",
7
avatar: avatarURL)
8
try client.createSudo(sudo: sudo) { (result) in
9
switch result {
10
case .success(let sudo):
11
// Load the avatar image.
12
if let avatar = sudo.avatar,
13
let data = try? Data(contentsOf: avatar) {
14
let image = UIImage(data: data)
15
}
16
case .failure(let cause):
17
// Handle error. A failure result may be returned if the backend is unable
18
// create the Sudo due to availability, security or entitlement issues.
19
}
20
}
21
} catch let error {
22
// Handle error. An error might be thrown for unrecoverable circumstances arising
23
// from programmatic error or configuration error. For example, if the keychain
24
// access entitlement is not set up correctly, the client is not signed in,
25
// or basic system resources are unavailable.
26
}
Copied!
1
val sudo = Sudo()
2
sudo.title = "Mr."
3
sudo.firstName = "John"
4
sudo.lastName = "Smith"
5
sudo.label = "Shopping"
6
sudo.notes = "Used for shopping."
7
sudo.avatar = avatarUri
8
​
9
CoroutineScope(Dispatchers.IO).launch {
10
try {
11
client.createSudo(sudo)
12
// Sudo created successfully.
13
} catch (e: SudoProfileException) {
14
// Handle error.
15
}
16
}
Copied!
1
import { Sudo } from '@sudoplatform/sudo-profiles'
2
​
3
const sudo = new Sudo()
4
sudo.title = "Mr."
5
sudo.firstName = "John"
6
sudo.lastName = "Smith"
7
sudo.label = "Shopping"
8
sudo.notes = "Used for shopping."
9
sudo.setAvatar(avatarFile)
10
​
11
try {
12
const createdSudo = await client.createSudo(sudo)
13
// Sudo created successfully.
14
} catch(err) {
15
// Handle error.
16
}
Copied!

List Sudos

To list Sudos:
Swift
Kotlin
TypeScript
1
do {
2
try client.listSudos(option: .remoteOnly) { (result) in
3
switch result {
4
case .success(let sudos):
5
for sudo in sudos {
6
// Load the avatar image.
7
if let avatar = sudo.avatar,
8
let data = try? Data(contentsOf: avatar) {
9
let image = UIImage(data: data)
10
}
11
}
12
case .failure(let cause):
13
// Handle error. A failure result may be returned if the backend is unable
14
// list Sudos due to availability or security issues.
15
}
16
}
17
} catch let error {
18
// Handle error. An error might be thrown for unrecoverable circumstances arising
19
// from programmatic error or configuration error. For example, if the keychain
20
// access entitlement is not set up correctly, the client is not signed in,
21
// or basic system resources are unavailable.
22
​
23
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
val sudos = client.listSudos(ListOption.REMOTE_ONLY)
4
for (sudo in sudos) {
5
// Load the avatar image.
6
val avatar = sudo.avatar
7
if (avatar != null) {
8
val file = File(avatar)
9
val data = file.readBytes()
10
}
11
}
12
​
13
} catch (e: SudoProfileException) {
14
// Handle error.
15
}
16
}
Copied!
1
try {
2
const sudos = await client.listSudos()
3
} catch(err) {
4
// Handle error.
5
}
Copied!
The list option specifies whether to return the cached entries or fetch the data from the backend. Initially the cache will be empty but is populated the first time the data is fetched from the backend using remoteOnly option. If the app is offline or if it requires a separate action to fetch new data from the backend then use cacheOnly option to show the user with the last known state then update the view when the app is able to fetch new data from the backend. create, update and delete APIs can also update the cache but the cache needs to be hydrated first by invoke list API with remoteOnly.

Modify Sudos

To update an existing Sudo:
Swift
Kotlin
TypeScript
1
sudo.firstName = "David"
2
do {
3
try client.updateSudo(sudo: sudo) { (result) in
4
switch result {
5
case .success(let sudo):
6
// Update successful.
7
case .failure(let cause):
8
// Handle error. A failure result may be returned if the backend is unable
9
// update Sudo due to availability or security issues.
10
}
11
}
12
} catch let error {
13
// Handle error. An error might be thrown for unrecoverable circumstances arising
14
// from programmatic error or configuration error. For example, if the keychain
15
// access entitlement is not set up correctly, the client is not signed in,
16
// or basic system resources are unavailable.
17
}
Copied!
1
sudo.firstName = "David"
2
CoroutineScope(Dispatchers.IO).launch {
3
try {
4
client.updateSudo(sudo)
5
// Sudo updated successfully.
6
} catch (e: SudoProfileException) {
7
// Handle error.
8
}
9
}
Copied!
1
sudo.firstName = "David"
2
try {
3
const updatedSudo = await client.updateSudo(sudo)
4
// Sudo updated successfully.
5
} catch(err) {
6
// Handle error.
7
}
Copied!

Subscribe to Changes

To subscribe to a specific Sudo change notification:
Swift
Kotlin
TypeScript
1
class MySubscriber: SudoSubscriber {
2
​
3
func sudoChanged(changeType: SudoChangeType, sudo: Sudo) {
4
// Process new, updated or deleted Sudo.
5
}
6
​
7
func connectionStatusChanged(state: SubscriptionConnectionState) {
8
if state == .connected {
9
// The subscription is now connected hence the subscribers
10
// will start receiving Sudo changes.
11
} else if state == .disconnected {
12
// The subscription is disconnected and all subscribers were
13
// automatically unsubscribed. Subscribe again to establish a
14
// new connection or report the error.
15
}
16
}
17
​
18
}
19
​
20
let subscriber = MySubscriber()
21
do {
22
try self.client.subscribe(id: "subscriber_id", changeType: .create, subscriber: subscriber)
23
} catch let error {
24
// Handle error. An error might be thrown for unrecoverable circumstances arising
25
// from programmatic error or configuration error. For example, if the keychain
26
// access entitlement is not set up correctly, the client is not signed in,
27
// or basic system resources are unavailable.
28
}
Copied!
1
class MySubscriber: SudoSubscriber {
2
​
3
override fun connectionStatusChanged(state: SudoSubscriber.ConnectionState) {
4
if (state == SudoSubscriber.ConnectionState.CONNECTED) {
5
// The subscription is now connected hence the subscribers
6
// will start receiving Sudo changes.
7
} else {
8
// The subscription is disconnected and all subscribers were
9
// automatically unsubscribed. Subscribe again to establish a
10
// new connection or report the error.
11
}
12
}
13
​
14
override fun sudoChanged(changeType: SudoSubscriber.ChangeType, sudo: Sudo) {
15
// Process new, updated or deleted Sudo.
16
}
17
}
18
​
19
val subscriber = MySubscriber()
20
CoroutineScope(Dispatchers.IO).launch {
21
client.subscribeAsync("<subscriber_id>", SudoSubscriber.ChangeType.CREATE, subscriber)
22
}
Copied!
1
import {
2
SudoSubscriber,
3
ConnectionState,
4
ChangeType,
5
} from '@sudoplatform/sudo-profiles'
6
​
7
class MySubscriber implements SudoSubscriber {
8
public sudoChanged(changeType, sudo) {
9
// Process new, updated or deleted Sudo.
10
}
11
​
12
public connectionStatusChanged(state) {
13
if (state === ConnectionState.Connected) {
14
// The subscription is now connected hence the subscribers
15
// will start receiving Sudo changes.
16
} else {
17
// The subscription is disconnected and all subscribers were
18
// automatically unsubscribed. Subscribe again to establish a
19
// new connection or report the error.
20
}
21
}
22
}
23
​
24
try {
25
const subscriber = new MySubscriber()
26
client.subscribe('subscriber_id', ChangeType.Create, subscriber)
27
} catch(err) {
28
// Handle error.
29
}
Copied!
For creating a new Sudo, the subscribers will be notified twice: ".create" notification when the Sudo is created and ".update" notification when the Sudo is updated with claims that requires reference to Sudo ID. The request to create a Sudo is considered complete when both of notifications have been received.
To subscribe to Sudo change notification of all types:
Swift
Kotlin
TypeScript
1
class MySubscriber: SudoSubscriber {
2
​
3
func sudoChanged(changeType: SudoChangeType, sudo: Sudo) {
4
// Process new, updated or deleted Sudo.
5
}
6
​
7
func connectionStatusChanged(state: SubscriptionConnectionState) {
8
if state == .connected {
9
// The subscription is now connected hence the subscribers
10
// will start receiving Sudo changes.
11
} else if state == .disconnected {
12
// The subscription is disconnected and all subscribers were
13
// automatically unsubscribed. Subscribe again to establish a
14
// new connection or report the error.
15
}
16
}
17
​
18
}
19
​
20
let subscriber = MySubscriber()
21
do {
22
try self.client.subscribe(id: "subscriber_id", subscriber: subscriber)
23
} catch let error {
24
// Handle error. An error might be thrown for unrecoverable circumstances arising
25
// from programmatic error or configuration error. For example, if the keychain
26
// access entitlement is not set up correctly, the client is not signed in,
27
// or basic system resources are unavailable.
28
}
Copied!
1
class MySubscriber: SudoSubscriber {
2
​
3
override fun connectionStatusChanged(state: SudoSubscriber.ConnectionState) {
4
if (state == SudoSubscriber.ConnectionState.CONNECTED) {
5
// The subscription is now connected hence the subscribers
6
// will start receiving Sudo changes.
7
} else {
8
// The subscription is disconnected and all subscribers were
9
// automatically unsubscribed. Subscribe again to establish a
10
// new connection or report the error.
11
}
12
}
13
​
14
override fun sudoChanged(changeType: SudoSubscriber.ChangeType, sudo: Sudo) {
15
// Process new, updated or deleted Sudo.
16
}
17
}
18
​
19
val subscriber = MySubscriber()
20
CoroutineScope(Dispatchers.IO).launch {
21
client.subscribeAsync("<subscriber_id>", subscriber)
22
}
Copied!
1
import {
2
SudoSubscriber,
3
ConnectionState,
4
ChangeType,
5
} from '@sudoplatform/sudo-profiles'
6
​
7
class MySubscriber implements SudoSubscriber {
8
public sudoChanged(changeType, sudo) {
9
// Process new, updated or deleted Sudo.
10
}
11
​
12
public connectionStatusChanged(state) {
13
if (state === ConnectionState.Connected) {
14
// The subscription is now connected hence the subscribers
15
// will start receiving Sudo changes.
16
} else {
17
// The subscription is disconnected and all subscribers were
18
// automatically unsubscribed. Subscribe again to establish a
19
// new connection or report the error.
20
}
21
}
22
}
23
​
24
try {
25
const subscriber = new MySubscriber()
26
client.subscribeAll('subscriber_id', subscriber)
27
} catch(err) {
28
// Handle error.
29
}
Copied!

Delete Sudos

To delete an existing Sudo:
Swift
Kotlin
TypeScript
1
do {
2
try client.deleteSudo(sudo: sudo) { (result) in
3
switch result {
4
case .success:
5
// Delete successful.
6
case .failure(let cause):
7
// Handle error. A failure result may be returned if the backend is unable
8
// delete the Sudo due to availability or security issues.
9
}
10
}
11
} catch let error {
12
// Handle error. An error might be thrown for unrecoverable circumstances arising
13
// from programmatic error or configuration error. For example, if the keychain
14
// access entitlement is not set up correctly, the client is not signed in,
15
// or basic system resources are unavailable.
16
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
client.deleteSudo(sudo)
4
// Sudo deleted successfully.
5
} catch (e: SudoProfileException) {
6
// Handle error.
7
}
8
}
Copied!
1
try {
2
await client.deleteSudo(sudo)
3
// Sudo deleted successfully.
4
} catch(err) {
5
// Handle error.
6
}
Copied!

Entitlements

Entitlements determine whether or not the user is able to perform certain operations in a platform service. For Sudo Service, there's an entitlement that governs how many Sudos each user is allowed to create.
For testing purposes it maybe useful to manipulate the user's entitlement. Redeem API allows you to redeem a special token to change entitlements for the currently signed in user. Currently, the following entitlements can be changed via the redeem API:
    sudoplatform.sudo.max
Redeeming a token to change the user's entitlement is permitted only for users who have registered using a test registration key.
To redeem a token to change an entitlement:
Swift
Kotlin
TypeScript
1
do {
2
// Change the maximum number of Sudos that the user can create to 3.
3
try client.redeem(token: "sudoplatform.sudo.max=3", type: "entitlements") { (result) in
4
switch result {
5
case let .success(entitlements):
6
// You can check now existing entitlements changed as a result.
7
case let .failure(cause):
8
// Handle error returned by the backend.
9
}
10
}
11
} catch let error {
12
// Handle error. An error might be thrown for unrecoverable circumstances arising
13
// from programmatic error or configuration error. For example, if the keychain
14
// access entitlement is not set up correctly, the client is not signed in,
15
// or basic system resources are unavailable.
16
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
val entitlements = client.redeem("sudoplatform.sudo.max=1", "entitlements")
4
// You can check now existing entitlements changed as a result.
5
val entitlement = entitlements.firstOrNull()
6
} catch (e: SudoProfileException) {
7
// Handle error.
8
}
9
}
Copied!
1
try {
2
const entitlements = await client.redeem('sudoplatform.sudo.max=1', 'entitlements')
3
// You can check now existing entitlements changed as a result.
4
const entitlement = entitlements[0]
5
} catch(err) {
6
// Handle error.
7
}
Copied!

Resetting Client State

You can reset the internal state information maintained by SudoProfilesClient by calling reset API.
Resetting the client state will cause all persistent data to be lost including any cached Sudos.
Swift
Kotlin
TypeScript
1
do {
2
try client.reset()
3
} catch let error {
4
// Handle error. An error might be thrown for unrecoverable circumstances arising
5
// from programmatic error or configuration error. For example, basic system resources
6
// are not accessible.
7
}
Copied!
1
client.reset()
Copied!
1
try {
2
await client.reset()
3
} catch(err) {
4
// Handle error. An error might be thrown for unrecoverable circumstances arising
5
// from programmatic error or configuration error. For example, basic system resources
6
// are not accessible.
7
}
Copied!
Last modified 1mo ago