RestClient.GitApiInterface service = RestClient. Have you got a better / creative / elegant idea?ĭon't think you should have have searchNext() on the result but rather on the service. Furthermore the interceptor would run for any method of the REST API but I need it only for search queries. This solution should technically work but I think it's ugly. The only idea I had to address this is to use an OkHttp interceptor, place the query parameters map in a ThreadLocal, then use a custom Gson TypeAdapter to inject those parameters into the SearchResult object. Since retrofit create the service I don't know how I can intercept the method parameters passed with the call to arch(). If the user perform the search call with q=foo this information is not received in the response. This means the SearchResult object I created in the previous call should internally know which parameters to use when using the method searchNext(Map parameters, String key). Where should I call startActivity() method here is my wrong trying. The same pattern with delegating implementation of Call interface to the original Call is applied and only the enqueue method is overrode with the logic of mapping the exceptions. The searchNext() call takes no parameters. I want to start new activity after call and finish 7 difference requests in parallel using retrofit. If you used RxJava with Retrofit you would need to use one so Retrofit can recognize Rx return types like Single or Completable. I want the developer using my library to be able to get the next batch like this: // then later result = the SearchResult MyService service = MyLibrarySDK.getService(context) Note that I do not write the code of this service, as retrofit works it generate the service automatically using my interface ( MyService) annotations.įrom the library user perspective all this is hidden, he will just receive the service and use it like this: // obtain the service Then I obtain the service: MyService service = retrofit.create(MyService.class) This makes it possible to use RxJava Observables and Flowables here. It generate the service for my interface and use Gson to parse the response wrapping it with RxJava Observable. Retrofit lets us receive calls results with custom handlers instead of the normal Call object by using Retrofit Call adapters. addCallAdapterFactory(RxJavaCallAdapterFactory.create()) addConverterFactory(GsonConverterFactory.create(gson)) // custom type adapter factory client(okHttpClient) // added interceptors Inside my library I initialize retrofit like this: Retrofit retrofit = new Retrofit.Builder() I want / need to hide from the user of the library these internal mechanism. To get the next batch I would need to create another request with the same two parameters, q=foo and limit=20, adding startKey=. To obtain the next batch the service has to be called again with the same parameters and the key returned.įor example, suppose doing a search for q=foo and limit=20: the REST API will return the results matching string foo batched in segments for 20 results each. a key to be used to retrieve the next batch of the list.String Map String query, String Map params, String startKey) I'm relying on retrofit2 configured to use RxJava to wrap the responses and GSon to parse them. The library will have to hide to the user the internal complexity of the API as much as possible. This article is based on Retrofit2.I'm writing a library for a REST API. In addition, it also has powerful scalability, supports various format conversion and RXJAVA. It translates a basic Java interface into a HTTP request through a dynamic agent and sends the request through OKHTTP. Retrofit2 is simply a network request adapter.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |