
unirest.BaseRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of open-unirest-java Show documentation
Show all versions of open-unirest-java Show documentation
Simplified, lightweight HTTP client library.
The newest version!
/**
* The MIT License
*
* Copyright for portions of OpenUnirest/uniresr-java are held by Kong Inc (c) 2013 as part of Kong/unirest-java.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package unirest;
import java.io.File;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
abstract class BaseRequest implements HttpRequest {
private Optional objectMapper = Optional.empty();
private String responseEncoding;
protected Headers headers = new Headers();
protected final Config config;
protected HttpMethod method;
protected Path url;
BaseRequest(BaseRequest httpRequest) {
this.config = httpRequest.config;
this.method = httpRequest.method;
this.url = httpRequest.url;
this.headers.putAll(httpRequest.headers);
}
BaseRequest(Config config, HttpMethod method, String url) {
this.config = config;
this.method = method;
this.url = new Path(url);
headers.putAll(config.getDefaultHeaders());
}
@Override
public R routeParam(String name, String value) {
url.param(name, value);
return (R)this;
}
@Override
public R basicAuth(String username, String password) {
header("Authorization", "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
return (R)this;
}
@Override
public R accept(String value) {
return header(HeaderNames.ACCEPT, value);
}
@Override
public R responseEncoding(String encoding) {
this.responseEncoding = encoding;
return (R)this;
}
@Override
public R header(String name, String value) {
this.headers.add(name.trim(), value);
return (R)this;
}
@Override
public R headerReplace(String name, String value) {
this.headers.replace(name, value);
return (R)this;
}
@Override
public R headers(Map headerMap) {
if (headers != null) {
for (Map.Entry entry : headerMap.entrySet()) {
header(entry.getKey(), entry.getValue());
}
}
return (R)this;
}
@Override
public R queryString(String name, Collection> value) {
url.queryString(name, value);
return (R)this;
}
@Override
public R queryString(String name, Object value) {
url.queryString(name, value);
return (R)this;
}
@Override
public R queryString(Map parameters) {
url.queryString(parameters);
return (R)this;
}
@Override
public R withObjectMapper(ObjectMapper mapper) {
Objects.requireNonNull(mapper, "ObjectMapper may not be null");
this.objectMapper = Optional.of(mapper);
return (R)this;
}
@Override
public HttpResponse asEmpty() {
return config.getClient().request(this, EmptyResponse::new);
}
@Override
public CompletableFuture> asEmptyAsync() {
return config.getAsyncClient()
.request(this, EmptyResponse::new, new CompletableFuture<>());
}
@Override
public CompletableFuture> asEmptyAsync(Callback callback) {
return config.getAsyncClient()
.request(this, EmptyResponse::new, CallbackFuture.wrap(callback));
}
@Override
public HttpResponse asString() throws UnirestException {
return config.getClient().request(this, r -> new StringResponse(r, responseEncoding));
}
@Override
public CompletableFuture> asStringAsync() {
return config.getAsyncClient()
.request(this, r -> new StringResponse(r, responseEncoding), new CompletableFuture<>());
}
@Override
public CompletableFuture> asStringAsync(Callback callback) {
return config.getAsyncClient()
.request(this, r -> new StringResponse(r, responseEncoding), CallbackFuture.wrap(callback));
}
@Override
public HttpResponse asJson() throws UnirestException {
return config.getClient().request(this, JsonResponse::new);
}
@Override
public CompletableFuture> asJsonAsync() {
return config.getAsyncClient().request(this, JsonResponse::new, new CompletableFuture<>());
}
@Override
public CompletableFuture> asJsonAsync(Callback callback) {
return config.getAsyncClient().request(this, JsonResponse::new, CallbackFuture.wrap(callback));
}
@Override
public HttpResponse asObject(Class extends T> responseClass) throws UnirestException {
return config.getClient().request(this, r -> new ObjectResponse(getObjectMapper(), r, responseClass));
}
@Override
public HttpResponse asObject(GenericType genericType) throws UnirestException {
return config.getClient().request(this, r -> new ObjectResponse(getObjectMapper(), r, genericType));
}
@Override
public HttpResponse asObject(Function function) {
return config.getClient().request(this, funcResponse(function));
}
@Override
public CompletableFuture> asObjectAsync(Function function) {
return config.getAsyncClient().request(this, funcResponse(function), new CompletableFuture<>());
}
@Override
public CompletableFuture> asObjectAsync(Class extends T> responseClass) {
return config.getAsyncClient().request(this, r -> new ObjectResponse(getObjectMapper(), r, responseClass), new CompletableFuture<>());
}
@Override
public CompletableFuture> asObjectAsync(Class extends T> responseClass, Callback callback) {
return config.getAsyncClient().request(this, r -> new ObjectResponse<>(getObjectMapper(), r, responseClass), CallbackFuture.wrap(callback));
}
@Override
public CompletableFuture> asObjectAsync(GenericType genericType) {
return config.getAsyncClient().request(this, r -> new ObjectResponse<>(getObjectMapper(), r, genericType), new CompletableFuture<>());
}
@Override
public CompletableFuture> asObjectAsync(GenericType genericType, Callback callback) {
return config.getAsyncClient().request(this, r -> new ObjectResponse<>(getObjectMapper(), r, genericType), CallbackFuture.wrap(callback));
}
private Function> funcResponse(Function function) {
return r -> new BasicResponse<>(r, function.apply(r));
}
@Override
public HttpResponse asBinary() throws UnirestException {
return config.getClient().request(this, BinaryResponse::new);
}
@Override
public CompletableFuture> asBinaryAsync() {
return config.getAsyncClient().request(this, BinaryResponse::new, new CompletableFuture<>());
}
@Override
public CompletableFuture> asBinaryAsync(Callback callback) {
return config.getAsyncClient().request(this, BinaryResponse::new, CallbackFuture.wrap(callback));
}
@Override
public void thenConsume(Consumer consumer) {
config.getClient().request(this, getConsumer(consumer));
}
@Override
public void thenConsumeAsync(Consumer consumer) {
config.getAsyncClient().request(this, getConsumer(consumer), new CompletableFuture<>());
}
@Override
public HttpResponse asFile(String path) {
return config.getClient().request(this, r -> new FileResponse(r, path));
}
@Override
public CompletableFuture> asFileAsync(String path) {
return config.getAsyncClient().request(this, r -> new FileResponse(r, path), new CompletableFuture<>());
}
@Override
public CompletableFuture> asFileAsync(String path, Callback callback) {
return config.getAsyncClient().request(this, r -> new FileResponse(r, path), CallbackFuture.wrap(callback));
}
private Function> getConsumer(Consumer consumer) {
return r -> {
consumer.accept(r);
return null;
};
}
@Override
public HttpMethod getHttpMethod() {
return method;
}
@Override
public String getUrl() {
return url.toString();
}
@Override
public Headers getHeaders() {
return headers;
}
@Override
public Body getBody() {
return null;
}
private ObjectMapper getObjectMapper() {
return objectMapper.orElseGet(config::getObjectMapper);
}
@Override
public PagedList asPaged(Function mappingFunction, Function, String> linkExtractor) {
PagedList all = new PagedList<>();
String nextLink = this.getUrl();
do {
this.url = new Path(nextLink);
HttpResponse next = mappingFunction.apply(this);
all.add(next);
nextLink = linkExtractor.apply(next);
}while (!Util.isNullOrEmpty(nextLink));
return all;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy