Skip to content

Commit c84bf9a

Browse files
committed
[http] Add synchronous httpSync
1 parent b11761a commit c84bf9a

File tree

3 files changed

+79
-50
lines changed

3 files changed

+79
-50
lines changed

docs/src/modules/http.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ desc: "Contains network functions"
44
desc_ru: "Содержит функции для взаимодействия с сетью"
55
constants: []
66
functions:
7-
- name: "http"
8-
args: "url"
7+
- name: http
8+
args: "url, ..."
99
desc: |-
1010
performs GET-request to `url`.
1111
@@ -65,6 +65,18 @@ functions:
6565
http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
6666
println "Added: " + v
6767
})
68+
- name: httpSync
69+
args: 'url, method = "GET", requestParams = {}, options = {}'
70+
desc: Synchronous version of `http` function. See above for parameters description.
71+
desc_ru: Синхронная версия функции `http`. См. выше описание параметров.
72+
example: |-
73+
use http
74+
extract(isOk, content) = httpSync("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10})
75+
if (isOk) {
76+
println "Added: " + content
77+
} else {
78+
println "Failure"
79+
}
6880
- name: "download"
6981
args: "url"
7082
desc: "downloads content by url as bytes array"

modules/main/src/main/java/com/annimon/ownlang/modules/http/http_http.java renamed to modules/main/src/main/java/com/annimon/ownlang/modules/http/HttpFunctions.java

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.modules.http;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.lib.*;
54
import java.io.IOException;
65
import java.io.UnsupportedEncodingException;
@@ -14,7 +13,7 @@
1413
import okhttp3.Response;
1514
import okhttp3.internal.http.HttpMethod;
1615

17-
public final class http_http implements Function {
16+
public final class HttpFunctions {
1817

1918
private static final Value
2019
HEADER_KEY = new StringValue("header"),
@@ -27,78 +26,94 @@ public final class http_http implements Function {
2726

2827
private final OkHttpClient client = new OkHttpClient();
2928

30-
@Override
31-
public Value execute(Value[] args) {
32-
String url, method;
33-
Function function;
29+
public Value httpSync(Value[] args) {
30+
Arguments.checkRange(1, 4, args.length);
31+
32+
String url = args[0].asString();
33+
String method = (args.length >= 2) ? args[1].asString() : "GET";
34+
Value requestParams = (args.length >= 3) ? args[2] : MapValue.EMPTY;
35+
MapValue options = (args.length >= 4) ? ValueUtils.consumeMap(args[3], 3) : MapValue.EMPTY;
36+
37+
boolean isSuccessful;
38+
Value result = options.containsKey(EXTENDED_RESULT) ? MapValue.EMPTY : StringValue.EMPTY;
39+
try (Response response = executeRequest(url, method, requestParams, options)) {
40+
isSuccessful = response.isSuccessful();
41+
if (isSuccessful) {
42+
result = getResult(response, options);
43+
}
44+
} catch (IOException ioe) {
45+
isSuccessful = false;
46+
}
47+
return new ArrayValue(new Value[] {
48+
NumberValue.fromBoolean(isSuccessful),
49+
result
50+
});
51+
}
52+
53+
public Value http(Value[] args) {
54+
Arguments.checkRange(1, 5, args.length);
55+
56+
String url = args[0].asString();
57+
String method = "GET";
58+
Value requestParams = MapValue.EMPTY;
59+
MapValue options = MapValue.EMPTY;
60+
Function callback = FunctionValue.EMPTY.getValue();
61+
3462
switch (args.length) {
3563
case 1: // http(url)
36-
url = args[0].asString();
37-
return process(url, "GET");
64+
break;
3865

3966
case 2: // http(url, method) || http(url, callback)
40-
url = args[0].asString();
4167
if (args[1].type() == Types.FUNCTION) {
42-
return process(url, "GET", ValueUtils.consumeFunction(args[1], 1));
68+
callback = ValueUtils.consumeFunction(args[1], 1);
69+
} else {
70+
method = args[1].asString();
4371
}
44-
return process(url, args[1].asString());
72+
break;
4573

4674
case 3: // http(url, method, params) || http(url, method, callback)
47-
url = args[0].asString();
4875
method = args[1].asString();
4976
if (args[2].type() == Types.FUNCTION) {
50-
return process(url, method, ValueUtils.consumeFunction(args[2], 2));
77+
callback = ValueUtils.consumeFunction(args[2], 2);
78+
} else {
79+
requestParams = args[2];
5180
}
52-
return process(url, method, args[2], FunctionValue.EMPTY.getValue());
81+
break;
5382

5483
case 4: // http(url, method, params, callback)
55-
url = args[0].asString();
5684
method = args[1].asString();
57-
function = ValueUtils.consumeFunction(args[3], 3);
58-
return process(url, method, args[2], function);
85+
requestParams = args[2];
86+
callback = ValueUtils.consumeFunction(args[3], 3);
87+
break;
5988

6089
case 5: // http(url, method, params, headerParams, callback)
61-
url = args[0].asString();
6290
method = args[1].asString();
63-
MapValue options = ValueUtils.consumeMap(args[3], 3);
64-
function = ValueUtils.consumeFunction(args[4], 4);
65-
return process(url, method, args[2], options, function);
66-
67-
default:
68-
throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
91+
requestParams = args[2];
92+
options = ValueUtils.consumeMap(args[3], 3);
93+
callback = ValueUtils.consumeFunction(args[4], 4);
94+
break;
6995
}
70-
}
71-
72-
private Value process(String url, String method) {
73-
return process(url, method, FunctionValue.EMPTY.getValue());
74-
}
75-
76-
private Value process(String url, String method, Function callback) {
77-
return process(url, method, MapValue.EMPTY, callback);
78-
}
7996

80-
private Value process(String url, String method, Value params, Function callback) {
81-
return process(url, method, params, MapValue.EMPTY, callback);
82-
}
83-
84-
private Value process(String url, String methodStr, Value requestParams, MapValue options, Function callback) {
85-
final String method = methodStr.toUpperCase();
8697
try {
87-
final Request.Builder builder = new Request.Builder()
88-
.url(url)
89-
.method(method, getRequestBody(method, requestParams, options));
90-
if (options.containsKey(HEADER_KEY)) {
91-
applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
92-
}
93-
94-
final Response response = client.newCall(builder.build()).execute();
98+
final Response response = executeRequest(url, method, requestParams, options);
9599
callback.execute(getResult(response, options));
96100
return NumberValue.fromBoolean(response.isSuccessful());
97101
} catch (IOException ex) {
98102
return NumberValue.fromBoolean(false);
99103
}
100104
}
101105

106+
private Response executeRequest(String url, String methodStr, Value requestParams, MapValue options) throws IOException {
107+
final String method = methodStr.toUpperCase();
108+
final Request.Builder builder = new Request.Builder()
109+
.url(url)
110+
.method(method, getRequestBody(method, requestParams, options));
111+
if (options.containsKey(HEADER_KEY)) {
112+
applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
113+
}
114+
return client.newCall(builder.build()).execute();
115+
}
116+
102117
private Value getResult(Response response, MapValue options) throws IOException {
103118
if (options.containsKey(EXTENDED_RESULT)) {
104119
final MapValue map = new MapValue(10);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public Map<String, Value> constants() {
1919

2020
@Override
2121
public Map<String, Function> functions() {
22+
final var httpFunctions = new HttpFunctions();
2223
return Map.of(
2324
"urlencode", new http_urlencode(),
24-
"http", new http_http(),
25+
"http", httpFunctions::http,
26+
"httpSync", httpFunctions::httpSync,
2527
"download", new http_download()
2628
);
2729
}

0 commit comments

Comments
 (0)