pleplanning.blogg.se

Retrofit kotlin example github
Retrofit kotlin example github




Modeling Retrofit Responses With Sealed Classes/Interfaces Let’s see how to construct the wrapper class with a sealed class. For example, configuring UI elements and displaying a different placeholder/toast depending on error types. But suppose you need to handle the response and exceptions in a multi-layer architecture as in the API data flow below:īy passing a wrapper class to the call site, the presentation layer can handle results depending on the response type. This snippet is a basic example of calling the Retrofit API and handling the response.

retrofit kotlin example github

The fetchPoster function requests a list of posters to the network, and the PosterRemoteDataSource returns the result of the fetchPosters function: The author of this article gave a talk about 'Modeling Retrofit responses with sealed classes and coroutines', so if you're interested in listening to the whole presentation, check out the video below:įirst things first, let’s see an example of Retrofit API calls. In this post, you will cover how to model Retrofit responses with Coroutines and Sealed classes to reduce code complexity and make your application architecture consistent.īefore you dive in, make sure your project includes Coroutines and Retrofit dependencies. How an application handles API responses will determine its overall architectural design and code complexity. Call deleteRequest = leteBook(123) ĭeleteRequest.As the rate of data communication increases, the complexity of the application architecture also increases. It is what I use for some REST API requests that don't return an entity object, and all I care about is looking at the response code. The ResponseBody object will return the results from the call.

retrofit kotlin example github retrofit kotlin example github

Not sure if you've used RxJava + Retrofit 2 before, but it is nice. Make sure you make the call off the UI-thread via AsyncTask or some other threading mechanism. Do it this way as you noted last: Call int bookId)






Retrofit kotlin example github