3.2 VYou – API

API

Every public method inside the SDK return a VYouResult which basically is a wrapper of the result of an action. This is represented by a sealed class whose possible values are

  • VYouResult.Success which holds a value: T with the result of the action
  • VYouResult.Failure which holds an error: Throwable with the reason for the failure

The VYou-Core SDK is divided in three main components that allows the developers to interact with the SDK depending on their needs, those are the following

  • VYouLoginManager This component allows you to interact with the three possible login platforms that the SDK provides. The API for that is the following suspend fun signInWithAuth(): VyouResult<VyouSession> suspend fun signInWithGoogle(): VyouResult<VyouSession> suspend fun signInWithFacebook(fragment: Fragment): VyouResult<VyouSession> Those three methods will start a new Activity inside your application, gather the necessary information from the user, perform an API request to the VYou backend and return the proper result inside the VYouSession object. To be able to interact with the VYouLoginManager, it’s mandatory to pass an ActivityResultCaller as a parameter from your application. fun getLogin(actResultCaller: ActivityResultCaller) = VYouLoginManager(actResultCaller) The activityResultCaller can be an activity or a fragment, this class can call APIs of type Activity#startActivityForResult without having to manage the request codes, and converting the request/response to an Intent. It’s necessary to call this function before the onCreate of your Activity or Fragment. You can do it like this: private val vyouLogin = VYou.getLogin(this) Last but not least, if you are using Facebook to authenticate your users, you need to override override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) in the Fragment or Activity that is calling the signInWithFacebook(...) method and call VyouLoginManager.onActivityResult(requestCode, resultCode, data) inside it
  • VYouTenantManager This manager allows the developer to interact with all of the API that doesn’t require the user to be authenticated. Those methods are the one for obtaining the Tenant information and the one for registering a new user in the platform suspend fun tenant(): VYouResult<VYouTenant> suspend fun register(customer: RegisterDto): VYouResult<Unit> Any of those two methods will perform an API request and return the proper result wrapped inside a VYouResult object.
  • VYouSession The last one of the components inside the VYou-Core SDK is the session. This session is accessible statically via the val session: VYouSession? property inside the VYou object. As you might have realized, this is a nullable property which will only hold a value if the user has been previously authenticated in your application. All of this session data is stored inside Android’s EncryptedSharedPreferences, so all of this data is properly secured by the OS. The VYouSession class holds the user’s credentials (VYouCredentials) which are accessible to the developer in case it’s needed. It also holds the necessary methods to interact with a logged-in user, which are the following: suspend fun signOut(): VYouResult<Unit> suspend fun tenantProfile(): VYouResult<VYouProfile> suspend fun editProfile(editProfileDto: EditProfileDto): VYouResult<Unit>
  • HttpClient This SDK also provides you with an OkHttpClient with the proper configuration so that you can make authenticated requests with any server that needs it. This client has three interceptors which are responsible for adding the Client-Credentials header which authenticated your application against the VYou servers, adding the Authorization header, which authenticates your user if it’s logged in, and refreshing the user’s token if it’s expired or invalid. This client is accessible via a static property val httpClient: OkHttpClient inside the VYou object.
  • CredentialsStorage VYou provides a built-in storage for credentials based on EncryptedSharedPreferences provided by the OS. It might be the case that this solution does not fit the security requirements for your application. If that’s the case, we provide an API so that you can install your own CredentialsStorage system to replace the one we provide. In order to do so, we provide an example with an InMemory storage. class InMemoryCredentialsStorage: CredentialsStorage { private var credentials: VYouCredentials? = null override fun save(credentials: VYouCredentials) { this.credentials = credentials } override fun read(): VYouCredentials? = credentials override fun clear() { credentials = null } } Notice that you need to implement the CredentialsStorage interface Once this is done, you need to override the storage inside VYou. In order to do so, as we are using Koin for dependency injection, this is pretty straightforward. You must create a module that provides your CredentialsStorage implementation and overrides our own, this is declared as follows. val credentialsModule = module { single<CredentialsStorage>(override = true) { InMemoryCredentialsStorage() } } The last thing you should do is passing a list containing this module when you initialize the SDK: VYou.initialize(this, listOf(credentialsModule)) All of this code is available for you to test inside the sample app we provide.
Scroll to Top