Retrieve User Consent Data

Retrieving User Consents

Overview

In this section, we discuss how to retrieve a list of user consents using the SDK. The getConsents method is designed for accessing user consent records, a critical aspect of managing user data.

Method Signature

The getConsents method is defined as:

suspend fun getConsents(consentRequest: ConsentRequest) : BWellResult<Consent>
  • consentRequest: An object of type ConsentRequest that includes a search term of category. See Create User Consent Data for full list of supported categories.
  • BWellResult<Consent>: The method returns an object representing the user's consent data.

Sample Code

Here's an example of how to use the getConsents method:

 val myConsentRequest: ConsentRequest = ConsentRequest.Builder()
        .category(ConsentCategoryCode.TOS)
        .build()

    fetchConsents(myConsentRequest)

suspend fun fetchUserConsents(consentsRequest: ConsentRequest): Flow<BWellResult<Consent>?> = flow {
    val consentsResult = BWellSdk.user?.getConsents(consentsRequest)
    emit(consentsResult)
}

Detailed Explanation

  1. Consent Data Retrieval:

    • BWellSdk.user?.getConsents(consentsRequest): This call attempts to retrieve the user's consents based on the provided ConsentRequest. The safe call operator (?.) ensures the method is invoked only if the user object is not null.
  2. Handling the Result:

    • The method stores the retrieved consent data in consentsResult, which is then emitted to the calling function.
    • This result can be used to verify the retrieval success and to process the consent records.
  3. Flow Usage:

    • The method fetchUserConsents returns a Flow<BWellResult<Consent>?>, enabling asynchronous data handling. This is particularly useful for applications that require real-time data processing or updates.

Best Practices

  • Customizable Requests: Utilize the flexibility of ConsentRequest to tailor the retrieval of consents according to specific needs, such as filtering by status or category.
  • Null Safety: Implement null-safe operations to prevent unexpected crashes.
  • Error Handling: Properly handle scenarios where consent retrieval fails, such as due to network issues or invalid search parameters.