Implementing the user service

Retrofit client calls the Gerrit API and handles the result by printing the result of the call to the console.

Create a class where we'll build a Retrofit client, and this will call the API and handle the result. This will be responsible for controlling all the tasks using the Retrofit.builder class and configuring it with the base of the given URL.   

Here's the code of UserServiceImpl.kt:

class UserServiceImpl{
fun getGithubServiceImpl(username:String, password:String): UserService {
val retrofit = Retrofit.Builder()
.client(getOkhttpClient(username, password))
.baseUrl(YOUR_SERVER_DOMAIN)
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(UserService::class.java)
}

private fun getOkhttpClient(username:String, password:String): OkHttpClient{
return OkHttpClient.Builder()
.addInterceptor(BasicAuthInterceptor(username, password))
.build()
}
}

According to this code, we set .client() with username and password. Then we implemented the YOUR_SERVER_DOMAIN (assume the URL of the Rest API server is http://localhost:8080), baseUrl() , and we've used  OkHttpClient as the client. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.239.166