All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.openunirest.request.BaseRequest Maven / Gradle / Ivy

/*
The MIT License

Copyright for portions of OpenUnirest/uniresr-java are held by Mashape (c) 2013 as part of Kong/unirest-java.
All other copyright for OpenUnirest/unirest-java are held by OpenUnirest (c) 2018.

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 io.github.openunirest.request;

import io.github.openunirest.http.HttpResponse;
import io.github.openunirest.http.JsonNode;
import io.github.openunirest.http.async.Callback;
import io.github.openunirest.http.exceptions.UnirestException;

import java.io.InputStream;
import java.util.concurrent.CompletableFuture;

public abstract class BaseRequest {

	private final ResponseBuilder builder = new ResponseBuilder();

	protected HttpRequest httpRequest;

	protected BaseRequest(HttpRequest httpRequest) {
		this.httpRequest = httpRequest;
	}

	public HttpRequest getHttpRequest() {
		return this.httpRequest;
	}

	protected BaseRequest() {
		super();
	}

	public HttpResponse asString() throws UnirestException {
		return HttpClientHelper.request(httpRequest, builder::asString);
	}

	public CompletableFuture> asStringAsync() {
		return HttpClientHelper.requestAsync(httpRequest, builder::asString);
	}

	public CompletableFuture> asStringAsync(Callback callback) {
		return HttpClientHelper.requestAsync(httpRequest, builder::asString, callback);
	}

	public HttpResponse asJson() throws UnirestException {
		return HttpClientHelper.request(httpRequest, builder::asJson);
	}

	public CompletableFuture> asJsonAsync() {
		return HttpClientHelper.requestAsync(httpRequest, builder::asJson);
	}

	public CompletableFuture> asJsonAsync(Callback callback) {
		return HttpClientHelper.requestAsync(httpRequest, builder::asJson, callback);
	}

	public  HttpResponse asObject(Class responseClass) throws UnirestException {
		return HttpClientHelper.request(httpRequest, r -> builder.asObject(r, responseClass));
	}

	public  HttpResponse asObject(GenericType genericType) throws UnirestException {
		return HttpClientHelper.request(httpRequest, r -> builder.asObject(r, genericType));
	}

	public  CompletableFuture> asObjectAsync(Class responseClass) {
		return HttpClientHelper.requestAsync(httpRequest, r -> builder.asObject(r, responseClass));
	}

	public  CompletableFuture> asObjectAsync(Class responseClass, Callback callback) {
		return HttpClientHelper.requestAsync(httpRequest, r -> builder.asObject(r, responseClass), callback);
	}

	public  CompletableFuture> asObjectAsync(GenericType genericType) {
		return HttpClientHelper.requestAsync(httpRequest, r -> builder.asObject(r, genericType));
	}

	public  CompletableFuture> asObjectAsync(GenericType genericType,  Callback callback) {
		return HttpClientHelper.requestAsync(httpRequest, r -> builder.asObject(r, genericType), callback);
	}

	public HttpResponse asBinary() throws UnirestException {
		return HttpClientHelper.request(httpRequest, builder::asBinary);
	}

	public CompletableFuture> asBinaryAsync() {
		return HttpClientHelper.requestAsync(httpRequest, builder::asBinary);
	}

	public CompletableFuture> asBinaryAsync(Callback callback) {
		return HttpClientHelper.requestAsync(httpRequest, builder::asBinary, callback);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy