com.landawn.abacus.http.AbstractHttpRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* Copyright (C) 2019 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.http;
import java.util.Map;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.util.ContinuableFuture;
/**
*
* @since 1.3
*
* @author Haiyang Li
*/
abstract class AbstractHttpRequest> {
final AbstractHttpClient httpClient;
HttpMethod httpMethod;
HttpSettings settings;
Object request;
AbstractHttpRequest(AbstractHttpClient httpClient) {
this.httpClient = httpClient;
}
public S header(String name, Object value) {
checkSettings();
settings.header(name, value);
return (S) this;
}
public S headers(String name1, Object value1, String name2, Object value2) {
checkSettings();
settings.headers(name1, value1, name2, value2);
return (S) this;
}
public S headers(String name1, Object value1, String name2, Object value2, String name3, Object value3) {
checkSettings();
settings.headers(name1, value1, name2, value2, name3, value3);
return (S) this;
}
public S headers(Map headers) {
checkSettings();
settings.headers(headers);
return (S) this;
}
public S headers(HttpHeaders headers) {
checkSettings();
settings.headers(headers);
return (S) this;
}
public String get() throws UncheckedIOException {
return get(String.class);
}
public T get(Class resultClass) throws UncheckedIOException {
return get(resultClass, null);
}
public String get(Object query) {
return get(String.class, query);
}
public T get(Class resultClass, Object query) throws UncheckedIOException {
this.httpMethod = HttpMethod.GET;
this.request = query;
return execute(resultClass);
}
public String post(Object body) {
return post(String.class, body);
}
public T post(Class resultClass, Object body) throws UncheckedIOException {
this.httpMethod = HttpMethod.POST;
this.request = body;
return execute(resultClass);
}
public String put(Object body) throws UncheckedIOException {
return put(String.class, body);
}
public T put(Class resultClass, Object body) throws UncheckedIOException {
this.httpMethod = HttpMethod.PUT;
this.request = body;
return execute(resultClass);
}
public String delete() throws UncheckedIOException {
return delete(String.class);
}
public T delete(Class resultClass) throws UncheckedIOException {
return delete(resultClass, null);
}
public String delete(Object query) throws UncheckedIOException {
return delete(String.class, query);
}
public T delete(Class resultClass, Object query) throws UncheckedIOException {
this.httpMethod = HttpMethod.DELETE;
this.request = query;
return execute(resultClass);
}
public ContinuableFuture asyncGet() {
return asyncGet(String.class);
}
public ContinuableFuture asyncGet(Class resultClass) {
return asyncGet(resultClass, null);
}
public ContinuableFuture asyncGet(Object query) {
return asyncGet(String.class, query);
}
public ContinuableFuture asyncGet(Class resultClass, Object query) {
this.httpMethod = HttpMethod.GET;
this.request = query;
return asyncExecute(resultClass);
}
public ContinuableFuture asyncPost(Object body) {
return asyncPost(String.class, body);
}
public ContinuableFuture asyncPost(Class resultClass, Object body) {
this.httpMethod = HttpMethod.POST;
this.request = body;
return asyncExecute(resultClass);
}
public ContinuableFuture asyncPut(Object body) {
return asyncPut(String.class, body);
}
public ContinuableFuture asyncPut(Class resultClass, Object body) {
this.httpMethod = HttpMethod.PUT;
this.request = body;
return asyncExecute(resultClass);
}
public ContinuableFuture asyncDelete() {
return asyncDelete(String.class);
}
public ContinuableFuture asyncDelete(Class resultClass) {
return asyncDelete(resultClass, null);
}
public ContinuableFuture asyncDelete(Object query) {
return asyncDelete(String.class, query);
}
public ContinuableFuture asyncDelete(Class resultClass, Object query) {
this.httpMethod = HttpMethod.DELETE;
this.request = query;
return asyncExecute(resultClass);
}
protected T execute(final Class resultClass) {
if (httpMethod == null) {
throw new RuntimeException("HTTP method is not set");
}
switch (httpMethod) {
case GET:
return httpClient.get(resultClass, request, settings);
case POST:
return httpClient.post(resultClass, request, settings);
case PUT:
return httpClient.put(resultClass, request, settings);
case DELETE:
return httpClient.delete(resultClass, request, settings);
default:
throw new RuntimeException("Unsupported HTTP method: " + httpMethod);
}
}
protected ContinuableFuture asyncExecute(final Class resultClass) {
if (httpMethod == null) {
throw new RuntimeException("HTTP method is not set");
}
switch (httpMethod) {
case GET:
return httpClient.asyncGet(resultClass, request, settings);
case POST:
return httpClient.asyncPost(resultClass, request, settings);
case PUT:
return httpClient.asyncPut(resultClass, request, settings);
case DELETE:
return httpClient.asyncDelete(resultClass, request, settings);
default:
throw new RuntimeException("Unsupported HTTP method: " + httpMethod);
}
}
protected void checkSettings() {
if (settings == null) {
settings = new HttpSettings();
}
}
}