do {let sudoInput =Sudo(title:"Mr.", firstName:"John", lastName:"Smith", label:"Shopping", notes:"Used for shopping.", avatar: avatarURL)let sudo =tryawait self.sudoProfilesClient.createSudo(sudo: sudoInput)// Load the avatar image.iflet avatar = sudo.avatar,let data =try?Data(contentsOf: avatar) {let image =UIImage(data: data) }} catchleterror {// Handle error. An error may be thrown if the backend is unable// to create the Sudo due to availability, security or entitlement issues or// due to unrecoverable circumstances arising from programmatic error or // configuration error. For example, if the keychain access entitlement // is not set up correctly, the client is not signed in,// or basic system resources are unavailable. }
val sudo =Sudo()sudo.title ="Mr."sudo.firstName ="John"sudo.lastName ="Smith"sudo.label ="Shopping"sudo.notes ="Used for shopping."sudo.avatar = avatarUriCoroutineScope(Dispatchers.IO).launch {try { client.createSudo(sudo)// Sudo created successfully. } catch (e: SudoProfileException) {// Handle error. }}
do {let sudos =tryawait self.sudoProfilesClient.listSudos(option: .remoteOnly)for sudo in sudos {// Load the avatar image.iflet avatar = sudo.avatar,let data =try?Data(contentsOf: avatar) {let image =UIImage(data: data) } }} catchleterror {// Handle error. An error may be thrown if the backend is unable// to list Sudos due to availability or for unrecoverable circumstances arising// from programmatic error or configuration error. For example, if the keychain// access entitlement is not set up correctly, the client is not signed in,// or basic system resources are unavailable.}
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:
sudo.firstName ="David"do { sudo =tryawait self.sudoProfilesClient.updateSudo(sudo: sudo)// Update successful.} catchleterror {// Handle error. An error may be thrown if the backend is unable to// update Sudo due to availability or security issues or for unrecoverable // circumstances arising from programmatic error or configuration error. // For example, if the keychain access entitlement is not set up correctly, // the client is not signed in, or basic system resources are unavailable.}
To subscribe to a specific Sudo change notification:
classMySubscriber:SudoSubscriber {funcsudoChanged(changeType: SudoChangeType, sudo: Sudo) {// Process new, updated or deleted Sudo. }funcconnectionStatusChanged(state: SubscriptionConnectionState) {if state == .connected {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } elseif state == .disconnected {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } }}let subscriber =MySubscriber()do {tryawait self.client.subscribe(id:"subscriber_id", changeType: .create, subscriber: subscriber)} catchleterror {// Handle error. An error might be thrown for unrecoverable circumstances arising// from programmatic error or configuration error. For example, if the keychain// access entitlement is not set up correctly, the client is not signed in,// or basic system resources are unavailable.}
classMySubscriber: SudoSubscriber {overridefunconnectionStatusChanged(state: SudoSubscriber.ConnectionState) {if (state == SudoSubscriber.ConnectionState.CONNECTED) {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } else {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } }overridefunsudoChanged(changeType: SudoSubscriber.ChangeType, sudo: Sudo) {// Process new, updated or deleted Sudo. }}val subscriber =MySubscriber()CoroutineScope(Dispatchers.IO).launch { client.subscribeAsync("<subscriber_id>", SudoSubscriber.ChangeType.CREATE, subscriber)}
import { SudoSubscriber, ConnectionState, ChangeType, } from'@sudoplatform/sudo-profiles'classMySubscriberimplementsSudoSubscriber {publicsudoChanged(changeType, sudo) {// Process new, updated or deleted Sudo. }publicconnectionStatusChanged(state) {if (state ===ConnectionState.Connected) {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } else {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } } }try {constsubscriber=newMySubscriber()client.subscribe('subscriber_id',ChangeType.Create, subscriber)} catch(err) {// Handle error.}
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:
classMySubscriber:SudoSubscriber {funcsudoChanged(changeType: SudoChangeType, sudo: Sudo) {// Process new, updated or deleted Sudo. }funcconnectionStatusChanged(state: SubscriptionConnectionState) {if state == .connected {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } elseif state == .disconnected {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } }}let subscriber =MySubscriber()do {tryawait self.client.subscribe(id:"subscriber_id", subscriber: subscriber)} catchleterror {// Handle error. An error may be thrown for unrecoverable circumstances arising// from programmatic error or configuration error. For example, if the keychain// access entitlement is not set up correctly, the client is not signed in,// or basic system resources are unavailable.}
classMySubscriber: SudoSubscriber {overridefunconnectionStatusChanged(state: SudoSubscriber.ConnectionState) {if (state == SudoSubscriber.ConnectionState.CONNECTED) {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } else {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } }overridefunsudoChanged(changeType: SudoSubscriber.ChangeType, sudo: Sudo) {// Process new, updated or deleted Sudo. }}val subscriber =MySubscriber()CoroutineScope(Dispatchers.IO).launch { client.subscribeAsync("<subscriber_id>", subscriber)}
import { SudoSubscriber, ConnectionState, ChangeType, } from'@sudoplatform/sudo-profiles'classMySubscriberimplementsSudoSubscriber {publicsudoChanged(changeType, sudo) {// Process new, updated or deleted Sudo. }publicconnectionStatusChanged(state) {if (state ===ConnectionState.Connected) {// The subscription is now connected hence the subscribers// will start receiving Sudo changes. } else {// The subscription is disconnected and all subscribers were// automatically unsubscribed. Subscribe again to establish a // new connection or report the error. } } }try {constsubscriber=newMySubscriber()client.subscribeAll('subscriber_id', subscriber)} catch(err) {// Handle error.}
Delete Sudos
To delete an existing Sudo:
do {tryawait client.deleteSudo(sudo: sudo)// Delete successful.} catchleterror {// Handle error. An error might be thrown if the backend is unable // to delete the Sudo due to availability or security issues or for // unrecoverable circumstances arising from programmatic error or // configuration error. For example, if the keychain access entitlement is // not set up correctly, the client is not signed in, or basic system // resources are unavailable.}
Many Sudo Platform services require the user to present a proof that they own a particular Sudo so that a provisioned resource can be associated with a Sudo. Sudo Profiles SDK provides an API to generate a cryptographic proof of Sudo ownership so it can be used in provisioning APIs of other SDKs such as Email SDK.
To obtain a Sudo ownership proof for provisioning a resource tied to a Sudo.
do {let jwt =tryawait client.getOwnershipProof(sudo: sudo, audience:"<service_specific_audience>")// Ownership proof obtained successfully.} catchleterror {// Handle error. An error might be thrown if the backend is unable// to delete the Sudo due to availability or security issues or for // unrecoverable circumstances arising from programmatic error or // configuration error. For example, if the keychain access entitlement is // not set up correctly, the client is not signed in, or basic system // resources are unavailable.}
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 and encryption keys. Unless you are intending to import the encryption keys from an external source, e.g. key backup, you should call generateEncryptionKey API to create a new encryption key before using SudoProfileClient again.
do {try client.reset()} catchleterror {// Handle error. An error may be thrown for unrecoverable circumstances arising// from programmatic error or configuration error. For example, basic system resources// are not accessible.}
client.reset()
try {awaitclient.reset()} catch(err) {// Handle error. An error might be thrown for unrecoverable circumstances arising// from programmatic error or configuration error. For example, basic system resources// are not accessible.}