|
9 | 9 | import java.net.*; |
10 | 10 | import java.time.*; |
11 | 11 | import java.util.*; |
| 12 | +import org.apache.http.HttpHost; |
12 | 13 | import org.apache.http.client.config.RequestConfig; |
13 | 14 | import org.apache.http.client.methods.HttpPatch; |
14 | 15 | import org.apache.http.entity.ByteArrayEntity; |
15 | 16 | import org.apache.http.impl.client.CloseableHttpClient; |
| 17 | +import org.apache.http.impl.client.HttpClientBuilder; |
16 | 18 | import org.apache.http.impl.client.HttpClients; |
17 | 19 | import org.apache.http.util.EntityUtils; |
18 | 20 | import org.jetbrains.annotations.*; |
@@ -83,15 +85,61 @@ public HttpResponse sendPutRequestWithBackoff( |
83 | 85 | return sendRequestWithBackoff(PUT, relativeUrl, content).toStringResponse(); |
84 | 86 | } |
85 | 87 |
|
| 88 | + public HttpResponse sendJsonRequestWithBackoff(String relativeUrl, String jsonBody) |
| 89 | + throws InterruptedException, DeepLException { |
| 90 | + HttpContent content = HttpContent.buildJsonContent(jsonBody); |
| 91 | + return sendRequestWithBackoff(POST, relativeUrl, content).toStringResponse(); |
| 92 | + } |
| 93 | + |
| 94 | + public HttpResponse sendJsonRequestWithBackoff(String method, String relativeUrl, String jsonBody) |
| 95 | + throws InterruptedException, DeepLException { |
| 96 | + HttpContent content = HttpContent.buildJsonContent(jsonBody); |
| 97 | + return sendRequestWithBackoff(method, relativeUrl, content).toStringResponse(); |
| 98 | + } |
| 99 | + |
| 100 | + public HttpResponse sendJsonPatchRequestWithBackoff(String relativeUrl, String jsonBody) |
| 101 | + throws InterruptedException, DeepLException { |
| 102 | + return sendPatchRequestWithBackoff(relativeUrl, HttpContent.buildJsonContent(jsonBody)); |
| 103 | + } |
| 104 | + |
86 | 105 | public HttpResponse sendPatchRequestWithBackoff( |
87 | 106 | String relativeUrl, @Nullable Iterable<KeyValuePair<String, String>> params) |
88 | | - throws DeepLException { |
89 | | - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
90 | | - HttpContent content = HttpContent.buildFormURLEncodedContent(params); |
| 107 | + throws InterruptedException, DeepLException { |
| 108 | + HttpContent content = HttpContent.buildFormURLEncodedContent(params); |
| 109 | + return sendPatchRequestWithBackoff(relativeUrl, content); |
| 110 | + } |
| 111 | + |
| 112 | + private HttpResponse sendPatchRequestWithBackoff(String relativeUrl, HttpContent content) |
| 113 | + throws InterruptedException, DeepLException { |
| 114 | + BackoffTimer backoffTimer = new BackoffTimer(this.minTimeout); |
| 115 | + while (true) { |
| 116 | + try { |
| 117 | + HttpResponse response = sendPatchRequest(relativeUrl, content, backoffTimer); |
| 118 | + if (backoffTimer.getNumRetries() >= this.maxRetries) { |
| 119 | + return response; |
| 120 | + } else if (response.getCode() != 429 && response.getCode() < 500) { |
| 121 | + return response; |
| 122 | + } |
| 123 | + } catch (ConnectionException exception) { |
| 124 | + if (!exception.getShouldRetry() || backoffTimer.getNumRetries() >= this.maxRetries) { |
| 125 | + throw exception; |
| 126 | + } |
| 127 | + } |
| 128 | + backoffTimer.sleepUntilRetry(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private HttpResponse sendPatchRequest( |
| 133 | + String relativeUrl, HttpContent content, BackoffTimer backoffTimer) throws DeepLException { |
| 134 | + HttpClientBuilder builder = HttpClients.custom(); |
| 135 | + if (proxy != null) { |
| 136 | + InetSocketAddress addr = (InetSocketAddress) proxy.address(); |
| 137 | + builder.setProxy(new HttpHost(addr.getHostName(), addr.getPort())); |
| 138 | + } |
| 139 | + try (CloseableHttpClient httpClient = builder.build()) { |
91 | 140 | HttpPatch request = new HttpPatch(serverUrl + relativeUrl); |
92 | 141 |
|
93 | 142 | // Set timeouts |
94 | | - BackoffTimer backoffTimer = new BackoffTimer(this.minTimeout); |
95 | 143 | RequestConfig requestConfig = |
96 | 144 | RequestConfig.custom() |
97 | 145 | .setConnectTimeout((int) backoffTimer.getTimeoutMillis()) |
|
0 commit comments