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

com.github.cosycode.ext.web.http.MyHttpTransaction Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.web.http;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.util.function.BiFunction;

/**
 * Description : 
 * 

* created in 2023/8/20 *

**/ @Getter @Accessors(fluent = true, chain = true) public class MyHttpTransaction { @SuppressWarnings("java:S2065") private final transient MyHttpClient client; private final MyHttpRequest request; protected MyHttpResponse response; public static MyHttpTransaction generate(MyHttpClient client, MyHttpRequest request) { return new MyHttpTransaction(client, request); } public MyHttpTransaction(MyHttpClient client, MyHttpRequest request) { this.client = client; this.request = request; } public MyHttpResponse send() throws IOException { return client.send(request); } public R send(@NonNull MyHttpTransactionFunction responseDisposer) throws IOException { return new Handler<>(this.client, this.request, responseDisposer).sendRequest(); } public R sendAndCatchException(@NonNull MyHttpTransactionFunction responseDisposer, BiFunction, Exception, R> catchDisposer) { Handler httpTransaction = new Handler<>(this.client, this.request, responseDisposer); try { return httpTransaction.sendRequest(); } catch (IOException e) { if (catchDisposer == null) { throw new RuntimeException(e); } return catchDisposer.apply(httpTransaction, e); } } public R sendWithRuntimeException(@NonNull MyHttpTransactionFunction responseDisposer) { return sendAndCatchException(responseDisposer, null); } @FunctionalInterface public interface MyHttpTransactionFunction { R apply(Handler httpTransaction, MyHttpResponse response) throws IOException; } @SuppressWarnings("java:S2065") public class Handler extends MyHttpTransaction { private final transient MyHttpTransactionFunction responseDisposer; @Setter(value = AccessLevel.NONE) private int retryTime; public Handler(MyHttpClient client, MyHttpRequest request, @NonNull MyHttpTransactionFunction responseDisposer) { super(client, request); this.responseDisposer = responseDisposer; } public MyHttpResponse retry(int cnt) throws IOException { if (cnt > retryTime) { sendRequest(); } throw new IOException(String.format("try %s times, and all fails ==> %s", retryTime, this)); } private R sendRequest() throws IOException { retryTime++; this.response = client.send(request); return responseDisposer.apply(this, this.response); } @Override public String toString() { return "MyHttpTransaction{" + "request=" + request + ", response=" + response + ", retryTime=" + retryTime + '}'; } } @Override public String toString() { return "MyHttpTransaction{" + "request=" + request + ", response=" + response + '}'; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy