package com.i3.net
import okhttp3.*
import okio.BufferedSink
import okio.Okio
import java.util.concurrent.TimeUnit
class Http3Service {
static final int MAX_CONCURRENT_CALLS = 200
static OkHttpClient client = createClient()
private static OkHttpClient createClient() {
new OkHttpClient.Builder()
.connectionPool(new ConnectionPool(MAX_CONCURRENT_CALLS, 1, TimeUnit.MINUTES))
.protocols([Protocol.HTTP_2, Protocol.HTTP_1_1])
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(10, TimeUnit.MINUTES)
.writeTimeout(10, TimeUnit.MINUTES)
.retryOnConnectionFailure(true)
.build()
}
static Map postForm(String url, Map<String, String> formData, Map<String, Map> fileData = [:]) {
retryHttpRequest({ ->
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
formData.each { key, value ->
multipartBuilder.addFormDataPart(key, value)
}
fileData.each { key, fileValue ->
RequestBody fileBody = new RequestBody() {
@Override
MediaType contentType() {
return MediaType.parse("application/octet-stream")
}
@Override
void writeTo(BufferedSink sink) throws IOException {
sink.writeAll(Okio.source(fileValue.inputStream))
}
}
multipartBuilder.addFormDataPart(key, fileValue.filename, fileBody)
}
Request request = new Request.Builder()
.url(url)
.post(multipartBuilder.build())
.build()
executeRequest(request)
})
}
static Map postJson(String url, String json) {
retryHttpRequest({ ->
RequestBody body = RequestBody.create(MediaType.parse("application/json"), json)
Request request = new Request.Builder()
.url(url)
.post(body)
.build()
executeRequest(request)
})
}
private static Map executeRequest(Request request) {
Response response = null
try {
response = client.newCall(request).execute()
String body = response.body()?.string()
int code = response.code()
return [response: body, code: code]
} catch (IOException e) {
println "HTTP call failed: ${e.getClass().simpleName} - ${e.getMessage()}"
throw new RuntimeException("HTTP request failed", e)
} finally {
response?.close()
}
}
// Overloaded method to simulate default parameters
private static Map retryHttpRequest(Closure<Map> requestClosure) {
retryHttpRequest(requestClosure, 3, 5000)
}
private static Map retryHttpRequest(Closure<Map> requestClosure, int maxRetries, long initialDelayMillis) {
int attempt = 0
long delay = initialDelayMillis
while (true) {
try {
return requestClosure.call()
} catch (IOException e) {
attempt++
if (attempt > maxRetries) {
println "HTTP request failed after ${attempt} attempts: ${e.class.simpleName} - ${e.message}"
throw new RuntimeException("HTTP request failed after retries", e)
}
println "HTTP request failed: ${e.class.simpleName} - ${e.message}. Retrying in ${delay}ms... (attempt ${attempt}/${maxRetries})"
try {
Thread.sleep(delay)
} catch (InterruptedException ie) {
Thread.currentThread().interrupt()
throw new RuntimeException("Retry interrupted", ie)
}
delay *= 2 // Exponential backoff
}
}
}
}
This class should now compile and run under Java 8 with Groovy 2.4+. Let me know if you'd like it converted to full Groovy idioms (e.g., using @Grab for dependencies or switching to Groovy HTTP builders).