Open obriensystems opened 8 months ago
public class RestClient {
// from
static Logger logger = Logger.getLogger(RestClient.class.getName());
private static final String URL_CREATE_RECORD =
"http://biometric.elasticbeanstalk.com/FrontController?action=activeid";
public static void restCall() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL_CREATE_RECORD))
.GET()
.build();
//HttpClient client = HttpClient.newBuilder().build();
HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
//.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
//.authenticator(Authenticator.getDefault())
.build();
try {
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();
logger.info("Response: " + body);
System.out.println(response.statusCode());
System.out.println(response.body());
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public static void main( String[] args )
{
RestClient app = new RestClient();
app.restCall();
}
}
{key: 1, value: second}
https://github.com/ObrienlabsDev/rest-client-java/tree/main
package gcfv2;
import java.io.BufferedWriter;
import java.io.IOException;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
public class HelloHttpFunction implements HttpFunction {
public String random(BufferedWriter writer, String input) throws java.lang.Exception {
List<String> strings = Collections.list(new StringTokenizer(input, ",")).stream()
.map(token -> (String) token)
.collect(Collectors.toList());
int index = (int)(Math.random() * strings.size());
writer.write("{key: " + index + ", ");
return strings.get(index);
}
public void service(final HttpRequest request, final HttpResponse response) throws java.lang.Exception, IOException {
final BufferedWriter writer = response.getWriter();
Optional<String> aCSVString = aCSVString = request.getFirstQueryParameter("list");
if(aCSVString.isPresent()) {
writer.write("value: " + random(writer, aCSVString.get()) + "}");
} else {
writer.write("append ?list=first,second,third....to get a random indexed string back in json");
}
}
}
https proxy settings must be at the war/jar classloader level to allow for AD authentication via System.setProperty()
Working with client calling into GCP https://github.com/GoogleCloudPlatform/pubsec-declarative-toolkit/issues/562 Authentication in a mixed VPN and Proxy environment requires a client beyond curl https://github.com/ObrienlabsDev/rest-client-java/tree/main
Continue work on PSC API access in https://github.com/GoogleCloudPlatform/pubsec-declarative-toolkit/issues/494 Key off existing example in spring boot off https://github.com/obrienlabs/magellan/blob/master/magellan-nbi/src/main/java/global/packet/magellan/integration/RestClient.java#L39
Move from HttpUrlConnection pre-11 to HttpClient see https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html#:~:text=An%20HttpClient%20can%20be%20used,proxy%2C%20an%20authenticator%2C%20etc.