UPP Java HTTP Client
Tsvetan Dimitrov
Agenda
-
Retrofit HTTP Client
-
Features
-
Workflow
-
-
Yet another private library! Why?
-
retrofit-wrapper features
Retrofit HTTP Client
Retrofit Features
-
Type safe API Modelling.
-
Synchronous/Asynchronous API.
-
Custom Interceptors.
-
Built on top of Square's okhttp HTTP client.
Retrofit Workflow / 1
-
Initialize HTTP Client
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
Retrofit Workflow / 2
-
API Interface Model
public interface UserService {
@GET("/users")
Call<List<User>> getUsers(
@Query("per_page") int perPage,
@Query("page") int page);
@GET("/users/{username}")
Call<User> getUser(@Path("username") String username);
}
Retrofit Workflow / 3
-
Initialize API Model Service and call it
UserService service = retrofit.create(UserService.class);
Call<User> call = service.getUser("fonsi");
Retrofit Workflow / 4.1
-
Asyncronous Call
Call<User> callAsync = service.getUser("eugenp");
callAsync.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();
}
@Override
public void onFailure(Call<User> call, Throwable throwable) {
System.out.println(throwable);
}
});
Retrofit Workflow / 4.2
-
Syncronous Call
Response<User> callSync = service.getUser("fonsi").execute();
if (response.isSuccessful()) {
// return response.body();
} else {
// handle API error
}
Yet another private library! Why? / 1
-
GitHub Repo: retrofit-wrapper
-
Simplification.
-
Control over library updates.
Retrofit Wrapper Features
-
Implementation of UPP specifics:
-
Custom Request/Response Logging
-
Transaction ID Propagation
-
API Error Handling
-
Default Request Headers
-
DEMO
Questions?
UPP Java HTTP Client
By Tsvetan Dimitrov
UPP Java HTTP Client
Introduction to Retrofit HTTP Client integration in UPP
- 110