提交 10801e7e 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-http 优化 github #26 .

上级 dbe29a1e
......@@ -30,25 +30,25 @@ import java.io.IOException;
*/
@ParametersAreNonnullByDefault
public class AsyncCallback implements Callback {
private final AsyncCall asyncCall;
private final AsyncExchange exchange;
AsyncCallback(AsyncCall asyncCall) {
this.asyncCall = asyncCall;
AsyncCallback(AsyncExchange exchange) {
this.exchange = exchange;
}
@Override
public void onFailure(Call call, IOException e) {
asyncCall.onFailure(call.request(), e);
exchange.onFailure(call.request(), e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try (HttpResponse httpResponse = new HttpResponse(response)) {
asyncCall.onResponse(httpResponse);
exchange.onResponse(httpResponse);
if (response.isSuccessful()) {
asyncCall.onSuccessful(httpResponse);
exchange.onSuccessful(httpResponse);
} else {
asyncCall.onFailure(call.request(), new IOException(httpResponse.message()));
exchange.onFailure(call.request(), new IOException(httpResponse.message()));
}
}
}
......
......@@ -28,7 +28,7 @@ import java.util.function.Consumer;
*
* @author L.cm
*/
public class AsyncCall {
public class AsyncExchange {
private final static Consumer<ResponseSpec> DEFAULT_CONSUMER = (r) -> {};
private final static BiConsumer<Request, IOException> DEFAULT_FAIL_CONSUMER = (r, e) -> {};
private final Call call;
......@@ -36,7 +36,7 @@ public class AsyncCall {
private Consumer<ResponseSpec> responseConsumer;
private BiConsumer<Request, IOException> failedBiConsumer;
AsyncCall(Call call) {
AsyncExchange(Call call) {
this.call = call;
this.successConsumer = DEFAULT_CONSUMER;
this.responseConsumer = DEFAULT_CONSUMER;
......@@ -53,7 +53,7 @@ public class AsyncCall {
this.execute();
}
public AsyncCall onFailed(BiConsumer<Request, IOException> biConsumer) {
public AsyncExchange onFailed(BiConsumer<Request, IOException> biConsumer) {
this.failedBiConsumer = biConsumer;
return this;
}
......
......@@ -22,6 +22,7 @@ import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.utils.Exceptions;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import javax.annotation.Nullable;
import java.io.File;
......@@ -89,6 +90,15 @@ public class Exchange {
return Optional.ofNullable(this.onSuccessful(func));
}
/**
* Returns ok http response.
*
* @return Response
*/
public Response response() {
return onResponse(ResponseSpec::rawResponse);
}
/**
* Returns body String.
*
......@@ -171,6 +181,7 @@ public class Exchange {
* toFile.
*
* @param file File
* @return File
*/
public File toFile(File file) {
return onResponse(responseSpec -> responseSpec.toFile(file));
......@@ -180,6 +191,7 @@ public class Exchange {
* toFile.
*
* @param path Path
* @return Path
*/
public Path toFile(Path path) {
return onResponse(responseSpec -> responseSpec.toFile(path));
......
......@@ -62,7 +62,7 @@ public class FormBuilder {
return this.build().execute();
}
public AsyncCall async() {
public AsyncExchange async() {
return this.build().async();
}
}
......@@ -288,8 +288,8 @@ public class HttpRequest {
return new Exchange(internalCall(httpClient));
}
public AsyncCall async() {
return new AsyncCall(internalCall(httpClient));
public AsyncExchange async() {
return new AsyncExchange(internalCall(httpClient));
}
public HttpRequest baseAuth(String userName, String password) {
......
......@@ -91,7 +91,7 @@ public class MultipartFormBuilder {
return this.build().execute();
}
public AsyncCall async() {
public AsyncExchange async() {
return this.build().async();
}
}
......@@ -176,6 +176,7 @@ public interface ResponseSpec {
* toFile.
*
* @param file File
* @return File
*/
File toFile(File file);
......@@ -183,6 +184,7 @@ public interface ResponseSpec {
* toFile.
*
* @param path Path
* @return Path
*/
Path toFile(Path path);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册