use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project chatty by chatty.
the class Request method apache.
private void apache() {
if (listener == null) {
return;
}
String responseText = null;
int responseCode = -1;
int ratelimitRemaining = -1;
String responseEncoding = null;
String requestError = null;
LOGGER.info(String.format("%s*%s: %s", requestMethod, token != null ? " (auth) " : "", url));
RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(CONNECT_TIMEOUT)).setResponseTimeout(30, TimeUnit.SECONDS).build();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
ClassicHttpRequest request = new HttpUriRequestBase(requestMethod, new URI(url));
request.addHeader("Client-ID", CLIENT_ID);
if (token != null) {
request.addHeader("Authorization", "Bearer " + token);
}
if (data != null) {
StringEntity stringEntity;
if (contentType.equals("application/json")) {
stringEntity = new StringEntity(data, ContentType.APPLICATION_JSON, "UTF-8", false);
} else {
stringEntity = new StringEntity(data, CHARSET);
}
request.setEntity(stringEntity);
LOGGER.info("Sending data: " + data);
}
try (CloseableHttpResponse response = httpclient.execute(request)) {
responseCode = response.getCode();
responseEncoding = getStringHeader(response.getFirstHeader("Content-Encoding"), null);
ratelimitRemaining = getIntHeader(response.getFirstHeader("Ratelimit-Remaining"), -1);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseText = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
EntityUtils.consume(responseEntity);
}
}
} catch (IOException | URISyntaxException | ParseException ex) {
requestError = ex.toString();
}
// -----------------------
// Debug output / Output
// -----------------------
LOGGER.info(String.format("GOT (%d/%d, %d%s): %s%s", responseCode, ratelimitRemaining, responseText != null ? responseText.length() : -1, responseEncoding != null ? ", " + responseEncoding : "", url, requestError != null ? " [" + requestError + "]" : ""));
listener.requestResult(responseText, responseCode, ratelimitRemaining);
}
use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project webdrivermanager by bonigarcia.
the class WdmServer method exchange.
public String exchange(String url, String method, String json, int timeoutSec) throws IOException {
String responseContent = null;
try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build()) {
HttpUriRequestBase request = null;
switch(method) {
case GET:
request = new HttpGet(url);
break;
case DELETE:
request = new HttpDelete(url);
break;
default:
case POST:
request = new HttpPost(url);
HttpEntity body = new StringEntity(json);
request.setEntity(body);
request.setHeader("Content-Type", "application/json");
break;
}
RequestConfig requestConfig = custom().setConnectTimeout(timeoutSec, TimeUnit.SECONDS).build();
request.setConfig(requestConfig);
try (CloseableHttpResponse response = closeableHttpClient.execute(request)) {
responseContent = IOUtils.toString(response.getEntity().getContent(), UTF_8);
}
}
return responseContent;
}
use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project easeagent by megaease.
the class HttpClient5DoExecuteForwardedInterceptor method before.
@Override
public void before(MethodInfo methodInfo, Context context) {
if (methodInfo.getArgs() == null) {
return;
}
for (Object arg : methodInfo.getArgs()) {
if (arg instanceof HttpUriRequestBase) {
final HttpUriRequestBase httpRequestBase = (HttpUriRequestBase) arg;
context.injectForwardedHeaders(httpRequestBase::setHeader);
return;
}
}
}
use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project adyen-java-api-library by Adyen.
the class AdyenHttpClient method request.
@Override
public String request(String endpoint, String requestBody, Config config, boolean isApiKeyRequired, RequestOptions requestOptions, ApiConstants.HttpMethod httpMethod, Map<String, String> params) throws IOException, HTTPClientException {
try (CloseableHttpClient httpclient = createCloseableHttpClient(config)) {
HttpUriRequestBase httpRequest = createRequest(endpoint, requestBody, config, isApiKeyRequired, requestOptions, httpMethod, params);
// Execute request with a custom response handler
AdyenResponse response = httpclient.execute(httpRequest, new AdyenResponseHandler());
if (response.getStatus() < 200 || response.getStatus() >= 300) {
throw new HTTPClientException(response.getStatus(), "HTTP Exception", response.getHeaders(), response.getBody());
}
return response.getBody();
}
}
use of org.apache.hc.client5.http.classic.methods.HttpUriRequestBase in project adyen-java-api-library by Adyen.
the class AdyenHttpClient method createHttpRequestBase.
private HttpUriRequestBase createHttpRequestBase(URI endpoint, String requestBody, ApiConstants.HttpMethod httpMethod) {
StringEntity requestEntity = null;
if (requestBody != null && !requestBody.isEmpty()) {
requestEntity = new StringEntity(requestBody, Charset.forName(CHARSET));
}
switch(httpMethod) {
case GET:
return new HttpGet(endpoint);
case PATCH:
HttpPatch httpPatch = new HttpPatch(endpoint);
httpPatch.setEntity(requestEntity);
return httpPatch;
case DELETE:
return new HttpDelete(endpoint);
default:
// Default to POST if httpMethod is not provided
HttpPost httpPost = new HttpPost(endpoint);
httpPost.setEntity(requestEntity);
return httpPost;
}
}
Aggregations