Search code examples
androidrobospice

Aggregate multitple webservices into one response using RoboSpice


I just started replacing my own code for network requests with RoboSpice and ran into a blocker. What i need to do is to make one request that will return a json, this json then gives me X many new url:s that i need to load additional data from in order to complete the request.

I would prefer to do this so that it, from the fragments point of view, is just one spiceRequest and as i interpret it from the RoboSpice GitHub page it supports this somehow.

"supports aggregation of different web services" from the RoboSpice GitHub page

Has anyone done something similar and would like to give me some info on how to achieve this?

Alternatively: Does anyone know if i can start one spiceRequest that then starts X many new spiceRequests while being processed in the spiceService, i'm guessing that RoboSpice can't merge my data on it's own and doing the actual merge isn't the issue here. The important thing is that i can do this while still in the spiceService running in the background so that it completes even if the activity/fragment is destoryed/recreated.


Solution

  • Since no one seems to have a good way of achieving this, I'm adding my solution(not the nicest one) in case some one else has a similar need.

    Im subclassing SpringAndroidSpiceRequest and in loadDataFromNetwork() i get the RestTemplate and simply make x many requests and merge them my self and then return the combined data as the result for the spiceRequest.

    I'm gonna replace this code when i find a nicer way of doing it but for now it does the job.

    public class LargeSpiceRequest extends SpringAndroidSpiceRequest<LargeResponse>{
        private String mUrl;
    
        public LargeSpiceRequest(String url) {
            super(LargeResponse.class);
            mUrl = url;
        }
    
        @Override
        public LargeResponse loadDataFromNetwork() throws Exception {
            RestTemplate restTemplate = getRestTemplate();
    
            ResponseEntity<LargeResponse> largeResponseResponseEntity = restTemplate.exchange(URI.create(mUrl), HttpMethod.GET, null, LargeResponse.class);
            LargeResponse largeResponse = largeResponseResponseEntity.getBody();
    
            ArrayList<SubRequest> subRequests = largeResponse.getSubRequests();
    
            for(SubRequest subRequest : subRequests){
                try{
                    String url = subRequest.getUrl();
                    ResponseEntity<SubRequestResponse> subRequestResponseEntity = restTemplate.exchange(URI.create(url), HttpMethod.GET, null, SubRequestResponse.class);
                    SubRequestResponse subRequestResponse = subRequestResponseEntity.getBody();
    
                    // merge somethingResponse with largeResponse
                    largeResponse.mergeIn(subRequestResponse);
                } catch (RestClientException e) {
                    // Handle this if you want
                }
            }
    
            // the merged largeResponse will be stored in the cache
            return largeResponse;
        }
    }