org.zodiac.okhttp.Exchange Maven / Gradle / Ivy
The newest version!
package org.zodiac.okhttp;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.Call;
import okhttp3.Request;
import javax.annotation.Nullable;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
public class Exchange {
private BiConsumer failedBiConsumer = (r, e) -> {};
private final Call call;
public Exchange(Call call) {
super();
this.call = call;
}
public Exchange onFailed(BiConsumer failConsumer) {
this.failedBiConsumer = failConsumer;
return this;
}
public R onResponse(Function func) {
try (HttpResponse response = new HttpResponse(call.execute())) {
return func.apply(response);
} catch (IOException e) {
throw ExceptionUtil.unchecked(e);
}
}
@Nullable
public R onSuccess(Function func) {
try (HttpResponse response = new HttpResponse(call.execute())) {
return func.apply(response);
} catch (IOException e) {
failedBiConsumer.accept(call.request(), e);
return null;
}
}
@Nullable
public R onSuccessful(Function func) {
try (HttpResponse response = new HttpResponse(call.execute())) {
if (response.isOk()) {
return func.apply(response);
} else {
failedBiConsumer.accept(call.request(), new IOException(response.toString()));
}
} catch (IOException e) {
failedBiConsumer.accept(call.request(), e);
}
return null;
}
public Optional onSuccessOpt(Function func) {
return Optional.ofNullable(this.onSuccess(func));
}
public Optional onSuccessfulOpt(Function func) {
return Optional.ofNullable(this.onSuccessful(func));
}
/**
* Returns body String.
*
* @return body String
*/
public String asString() {
return onResponse(ResponseSpec::asString);
}
/**
* Returns body to byte arrays.
*
* @return byte arrays
*/
public byte[] asBytes() {
return onResponse(ResponseSpec::asBytes);
}
/**
* Returns body to JsonNode.
*
* @return JsonNode
*/
public JsonNode asJsonNode() {
return onResponse(ResponseSpec::asJsonNode);
}
/**
* Returns body to Object.
*
* @param 泛型
* @param valueType value value type
* @return Object
*/
public T asValue(Class valueType) {
return onResponse(responseSpec -> responseSpec.asValue(valueType));
}
/**
* Returns body to Object.
*
* @param 泛型
* @param typeReference value Type Reference
* @return Object
*/
public T asValue(TypeReference typeReference) {
return onResponse(responseSpec -> responseSpec.asValue(typeReference));
}
/**
* Returns body to List.
*
* @param 泛型
* @param valueType value type
* @return List
*/
public List asList(Class valueType) {
return onResponse(responseSpec -> responseSpec.asList(valueType));
}
/**
* Returns body to Map.
*
* @param key type
* @param value type
* @param keyClass key type
* @param valueType value type
* @return Map
*/
public Map asMap(Class> keyClass, Class> valueType) {
return onResponse(responseSpec -> responseSpec.asMap(keyClass, valueType));
}
/**
* Returns body to Map.
*
* @param value type
* @param valueType value 类型
* @return Map
*/
public Map asMap(Class> valueType) {
return onResponse(responseSpec -> responseSpec.asMap(valueType));
}
/**
* 将 xml、heml 转成对象。
*
* @param valueType 对象类
* @param 泛型
* @return 对象
*/
public T asDomValue(Class valueType) {
return onResponse(responseSpec -> responseSpec.asDomValue(valueType));
}
/**
* 将 xml、heml 转成对象。
*
* @param valueType 对象类
* @param 泛型
* @return 对象集合
*/
public List asDomList(Class valueType) {
return onResponse(responseSpec -> responseSpec.asDomList(valueType));
}
/**
* toFile.
*
* @param file File
* @return {@link File}
*/
public File toFile(File file) {
return onResponse(responseSpec -> responseSpec.toFile(file));
}
/**
* toFile.
*
* @param path Path
* @return {@link Path}
*/
public Path toFile(Path path) {
return onResponse(responseSpec -> responseSpec.toFile(path));
}
}