Saturday, 5 May 2018

Basic Retrofit notes

Some notes about the Retrofit library created by Square:
- It has three parts:
    - An interface which specified what to call and how to call
    - The retrofit instance which specifies the base url and some options
    - The service that retrofit creates based on the interface

The interface
- It starts with an interface, which the coder specifies how to interact with the backend server.
- The idea is get rip of that implementation by using Retrofit object and annotation processor.
    - @Query
    - @GET, @POST, @PUT, @DELETE
    - @PATH
    - @Headers("Someheader: SomeValue")
- Initially the request is done inside a Call
    - A wrapper for the request
        - It could be executed asynchronously or synchronously

interface ApiService{
    Call<List<User>> search {
        @Query("q") String query,
        @Query("sort") SortOrder order);

The retrofit instance
- The interface will require another class which is Retrofit
    - Retrofit retrofit = new Retrofit.Builder().baseUrl("").build();

The service
- Then it will be connected with the interface by using the method create
    - ApiService service = retrofit.create(ApiService.class)

The retrofit instance/Object in details
    - Parses the interface
    - Figure out how to make the request
    - Makes them and
    - Ask someone else to execute them

- It implicitly creates the http client --> OkHttpClient
- Different retrofit instance could use the same okHttpClient instances
- The OkHttpClient have a builder which allow you to create a instance and customize it
    - Everything that you don't touch, it will be shared

    - Get the bytes and turn them into nice java bits
    - Take the java bits and turn them into bytes

By default Retrofit only can be used to return a Call of ResponseBody (Call<ResponseBody>)
    - It is basically an object that represents the bytes in the response
    - So by adding a converter (GsonConverterFactory) it allow
         - To us a Call of User (Call<User>)
         - When the response comes in, Retrofit is going to ask this Gson converter to take the bytes, turn them into the User and return it to you
    - You can add customized Gson converter to the gson factory.
- You can have multiple converters
    - One based on Gson
    - One based on ProtoBuf

- The order of the convertors matters
    - if Gson convertor is put at the first place, it will handle all the classes

- To distinguish between XML convertor or Gson convertor, we can create our own convertor which check if the Gson convertor or XML convertor should be used

- When the response contains several metadata:
    - You can create a converter inside another converter
        - Using Type and retrofit instance given
        - To return the inner object instead of Envelope of the inner object

Call adapter
- OkHttpClient has its own Call object
    - It returns back bytes
- The retrofit returns another Call (Call<User>) which have the data converted
- Actually there is another Call of user between them in order to convert the bytes into the proper class

Call<User> <----- Call<User> <---- Call <---- Call.Factory from OkHttpClient)

- This is because the type conversion is done on the background thread. Then Retrofit needs a way to come back to the main thread. That's the mission of the Call Adapter

Call<User> <---- CallAdapter <----- Call<User> <---- Call <---- Call.Factory from OkHttpClient)

- The Call adapter is customizable
- There is a RxJava call adapter
    - With this, the interfaces will be able to return RxJava types
    - For example Observables
- Retrofit automatically adds a default call adapter at the end
    - BuildingCallFactory()

Mock mode
- Retrofit allows you to implement the interfaces and then, returns fake data and swap out the real service by the fake server.
- Use NetworkBehaviour.create() to mimic the behaviour of the internet connection
    - Delay
    - Variance percentage
    - Failure percentage

No comments:

Post a Comment