Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.mastfrog.webapi;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.google.inject.name.Names;
import com.mastfrog.acteur.headers.Headers;
import com.mastfrog.acteur.util.BasicCredentials;
import com.mastfrog.giulius.Dependencies;
import com.mastfrog.giulius.scope.ReentrantScope;
import com.mastfrog.netty.http.client.HttpClient;
import com.mastfrog.netty.http.client.HttpClientBuilder;
import com.mastfrog.netty.http.client.HttpRequestBuilder;
import com.mastfrog.netty.http.client.ResponseFuture;
import com.mastfrog.netty.http.client.State;
import com.mastfrog.url.Path;
import com.mastfrog.url.URL;
import com.mastfrog.url.URLBuilder;
import com.mastfrog.util.preconditions.Exceptions;
import com.mastfrog.util.thread.Receiver;
import com.mastfrog.webapi.builtin.Parameters;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Invokes web api calls
*
* @author Tim Boudreau
*/
public class Invoker & WebCallEnum> {
private final HttpClient client;
private final Dependencies deps;
private final URL base;
private final ObjectMapper mapper;
@Inject
public Invoker(HttpClient client, Dependencies deps, URL base, ObjectMapper mapper) {
this.client = client;
this.deps = deps;
this.base = base;
this.mapper = mapper;
}
private void replace(String token, StringBuilder in, Object with) {
if (with == null) {
return;
}
token = "{{" + token + "}}";
int ix = in.indexOf(token);
if (ix >= 0) {
try {
String val = URLEncoder.encode(with.toString(), "UTF-8");
in.replace(ix, ix + token.length(), val);
} catch (UnsupportedEncodingException ex) {
Exceptions.chuck(ex);
}
}
}
/**
* Create a standalone Web API invoker for the passed base URL and Web API
* enum type. This method creates all of the Guice plumbing used to drive
* the invoker under the hood. Use this for deployment in simple apps which
* have no other need for dependency injection; otherwise, you may want to
* use
* WebApiModule and set up
* injection explicitly (in which case you need to bind
* com.mastfrog.url.URL and
* com.mastfrog.netty.HttpClient and Jackson's
* ObjectMapper.
*
* @param The type
* @param baseUrl The base URL for api calls
* @param webApi An enum whose constants implement WebCall
* @return An invoker
* @throws IOException
*/
public static & WebCallEnum> Invoker create(URL baseUrl, Class webApi) throws IOException {
Dependencies deps = Dependencies.builder().add(new StandaloneModule(baseUrl))
.add(new WebApiModule(webApi)).build();
return deps.getInstance(Invoker.class);
}
private static class StandaloneModule extends AbstractModule {
private final URL url;
private final HttpClientBuilder builder = HttpClient.builder().followRedirects();
StandaloneModule(URL url) {
this.url = url;
}
@Override
protected void configure() {
bind(URL.class).toInstance(url);
bind(HttpClient.class).toInstance(builder.build());
}
}
private boolean interpolate(StringBuilder sb, Class type, Dependencies deps, WebCall call) {
Class> pi = call.interpolator(type);
if (pi != null) {
return interpolate(sb, type, deps, call, pi);
}
return false;
}
private > boolean interpolate(StringBuilder sb, Class type, Dependencies deps, WebCall call, Class pi) {
R r = deps.getInstance(pi);
T obj = deps.getInstance(type);
r.interpolate(call, sb, obj, type);
return false;
}
private Object[] combine(Object[] a, Object... with) {
List