Skip to content

Commit f1a3511

Browse files
committed
[okhttp] Add okhttp.newClient() and HttpClientBuilderValue
1 parent 3eddbbc commit f1a3511

File tree

6 files changed

+115
-15
lines changed

6 files changed

+115
-15
lines changed

docs/docs/en/changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- Fix passing command-line arguments to scripts
77
- Fix `this` in non-static class methods
88

9+
### Modules
10+
- [okhttp] Added `okhttp.newClient()` and `HttpClientBuilderValue`
11+
912

1013
## 2.0.0
1114

docs/docs/ru/changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- Исправлена передача аргументов командной строки скриптам
77
- Исправлен `this` в нестатических методах классов
88

9+
### Modules
10+
- [okhttp] Добавлен `okhttp.newClient()` и `HttpClientBuilderValue`
11+
912

1013
## 2.0.0
1114

docs/src/modules/okhttp.yml

+31
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ types:
170170
args: ''
171171
desc: returns RequestBuilderValue
172172
desc_ru: возвращает RequestBuilderValue
173+
- name: newClient
174+
args: ''
175+
desc: returns HttpClientBuilderValue
176+
desc_ru: возвращает HttpClientBuilderValue
173177
- name: HttpClientValue
174178
functions:
175179
- name: connectTimeoutMillis
@@ -208,3 +212,30 @@ types:
208212
args: ''
209213
desc: ''
210214
desc_ru: ''
215+
- name: HttpClientBuilderValue
216+
functions:
217+
- name: callTimeout
218+
args: 'duration, timeUnit'
219+
desc: 'Sets call timeout for `duration` and `timeUnit` one of "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
220+
desc_ru: 'Устанавливает таймаут вызова в `duration` и `timeUnit` один из "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
221+
- name: connectTimeout
222+
args: 'duration, timeUnit'
223+
desc: 'Sets connect timeout for `duration` and `timeUnit` one of "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
224+
desc_ru: 'Устанавливает таймаут соединения в `duration` и `timeUnit` один из "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
225+
- name: readTimeout
226+
args: 'duration, timeUnit'
227+
desc: 'Sets read timeout for `duration` and `timeUnit` one of "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
228+
desc_ru: 'Устанавливает таймаут чтения в `duration` и `timeUnit` один из "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
229+
- name: writeTimeout
230+
args: 'duration, timeUnit'
231+
desc: 'Sets write timeout for `duration` and `timeUnit` one of "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
232+
desc_ru: 'Устанавливает таймаут записи в `duration` и `timeUnit` один из "millis"/"milliseconds", "seconds", "minutes", "hours", "days"'
233+
- name: retryOnConnectionFailure
234+
args: 'flag'
235+
desc: 'Enables or disables retry on connection failure'
236+
desc_ru: 'Включает или выключает повтор при ошибках соединения'
237+
- name: build
238+
args: ''
239+
desc: 'Returns new HttpClientValue'
240+
desc_ru: 'Возвращает новый HttpClientValue'
241+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.annimon.ownlang.modules.okhttp;
2+
3+
import com.annimon.ownlang.exceptions.OwnLangRuntimeException;
4+
import com.annimon.ownlang.lib.*;
5+
import okhttp3.*;
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class HttpClientBuilderValue extends MapValue {
9+
10+
private final OkHttpClient.Builder builder;
11+
12+
public HttpClientBuilderValue(OkHttpClient.Builder builder) {
13+
super(6);
14+
this.builder = builder;
15+
init();
16+
}
17+
18+
private void init() {
19+
set("callTimeout", timeout(builder::callTimeout));
20+
set("connectTimeout", timeout(builder::connectTimeout));
21+
set("readTimeout", timeout(builder::readTimeout));
22+
set("writeTimeout", timeout(builder::writeTimeout));
23+
set("retryOnConnectionFailure", args -> {
24+
builder.retryOnConnectionFailure(args[0].asInt() != 0);
25+
return this;
26+
});
27+
set("build", args -> new HttpClientValue(builder.build()));
28+
}
29+
30+
private FunctionValue timeout(Timeout timeout) {
31+
return new FunctionValue(args -> {
32+
Arguments.check(2, args.length);
33+
long duration = args[0].type() == Types.NUMBER
34+
? ((NumberValue) args[0]).asLong()
35+
: args[0].asInt();
36+
TimeUnit unit = switch (args[1].asString().toLowerCase()) {
37+
case "millis", "milliseconds" -> TimeUnit.MILLISECONDS;
38+
case "seconds" -> TimeUnit.SECONDS;
39+
case "minutes" -> TimeUnit.MINUTES;
40+
case "hours" -> TimeUnit.HOURS;
41+
case "days" -> TimeUnit.DAYS;
42+
default -> throw new OwnLangRuntimeException("Unknown unit type");
43+
};
44+
timeout.timeout(duration, unit);
45+
return this;
46+
});
47+
}
48+
49+
interface Timeout {
50+
void timeout(long duration, TimeUnit unit);
51+
}
52+
}

modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/HttpClientValue.java

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import okhttp3.WebSocket;
99
import okhttp3.WebSocketListener;
1010
import okio.ByteString;
11+
import java.util.Objects;
1112

1213
public class HttpClientValue extends MapValue {
1314

@@ -117,6 +118,7 @@ public void onFailure(WebSocket webSocket, Throwable t, Response response) {
117118
}
118119
}
119120
});
121+
Objects.requireNonNull(ws);
120122
return new CallValue(client.newCall(request));
121123
}
122124
}

modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/okhttp.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ public final class okhttp implements Module {
1717

1818
@Override
1919
public Map<String, Value> constants() {
20+
MapValue okhttp = new MapValue(3);
21+
okhttp.set("client", defaultClient);
22+
okhttp.set("request", args -> new RequestBuilderValue());
23+
okhttp.set("newClient", args -> newClientBuilder());
24+
25+
return Map.of(
26+
"RequestBody", requestBody(),
27+
"MultipartBody", multipartBody(),
28+
"okhttp", okhttp
29+
);
30+
}
31+
32+
@Override
33+
public Map<String, Function> functions() {
34+
return Collections.emptyMap();
35+
}
36+
37+
private static MapValue requestBody() {
2038
MapValue requestBody = new MapValue(5);
2139
requestBody.set("bytes", args -> {
2240
Arguments.checkOrOr(2, 4, args.length);
@@ -52,30 +70,21 @@ public Map<String, Value> constants() {
5270
args[1].asString()
5371
));
5472
});
73+
return requestBody;
74+
}
5575

56-
76+
private static MapValue multipartBody() {
5777
MapValue multipartBody = new MapValue(6);
5878
multipartBody.set("ALTERNATIVE", new StringValue(MultipartBody.ALTERNATIVE.toString()));
5979
multipartBody.set("DIGEST", new StringValue(MultipartBody.DIGEST.toString()));
6080
multipartBody.set("FORM", new StringValue(MultipartBody.FORM.toString()));
6181
multipartBody.set("MIXED", new StringValue(MultipartBody.MIXED.toString()));
6282
multipartBody.set("PARALLEL", new StringValue(MultipartBody.PARALLEL.toString()));
6383
multipartBody.set("builder", args -> new MultipartBodyBuilderValue());
64-
65-
66-
MapValue okhttp = new MapValue(3);
67-
okhttp.set("client", defaultClient);
68-
okhttp.set("request", args -> new RequestBuilderValue());
69-
70-
return Map.of(
71-
"RequestBody", requestBody,
72-
"MultipartBody", multipartBody,
73-
"okhttp", okhttp
74-
);
84+
return multipartBody;
7585
}
7686

77-
@Override
78-
public Map<String, Function> functions() {
79-
return Collections.emptyMap();
87+
private static HttpClientBuilderValue newClientBuilder() {
88+
return new HttpClientBuilderValue(new OkHttpClient.Builder());
8089
}
8190
}

0 commit comments

Comments
 (0)