示例#1
0
 /**
  * Calls the API with the given parameters and returns a response.
  *
  * @param url API url to call
  * @param queryParams A String array with the query parameters
  * @param headers A String Array with the headers
  * @return The response of the service call
  */
 private WS.Response callApi(
     final String url, final String[] queryParams, final String[] headers) {
   WSRequestHolder result = WS.url(SERVER_URL + url);
   if (queryParams != null)
     for (int i = 0; i + 1 < queryParams.length; i += 2) {
       result.setQueryParameter(queryParams[i], queryParams[i + 1]);
     }
   if (headers != null)
     for (int i = 0; i + 1 < headers.length; i += 2) {
       result.setHeader(headers[i], headers[i + 1]);
     }
   return result.get().get(10000);
 }
示例#2
0
 public static Promise<Response> createCacheableUrlFetchPromise(
     String getUrl, Map<String, String> getParams) {
   final String key =
       "http-cache-" + getUrl + ":" + new TreeMap<String, String>(getParams).toString();
   final Response cached = (Response) Cache.get(key);
   if (cached == null) {
     WSRequestHolder holder = WS.url(getUrl);
     for (String k : getParams.keySet()) holder.setQueryParameter(k, getParams.get(k));
     Promise<Response> promise = holder.get();
     return promise.map(
         new Function<Response, Response>() {
           @Override
           public Response apply(Response res) throws Throwable {
             Cache.set(key, res);
             synchronized (cachedSet) {
               cachedSet.insert(key);
             }
             return res;
           }
         });
   } else {
     return Promise.pure(cached);
   }
 }