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 typeConsentRequestthat includes a search term ofcategory. 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
-
Consent Data Retrieval:
BWellSdk.user?.getConsents(consentsRequest): This call attempts to retrieve the user's consents based on the providedConsentRequest. The safe call operator (?.) ensures the method is invoked only if theuserobject is not null.
-
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.
- The method stores the retrieved consent data in
-
Flow Usage:
- The method
fetchUserConsentsreturns aFlow<BWellResult<Consent>?>, enabling asynchronous data handling. This is particularly useful for applications that require real-time data processing or updates.
- The method
Best Practices
- Customizable Requests: Utilize the flexibility of
ConsentRequestto 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.
Updated about 2 months ago
