mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
[http] Add synchronous httpSync
This commit is contained in:
parent
b11761a99a
commit
c84bf9a4e5
@ -4,8 +4,8 @@ desc: "Contains network functions"
|
|||||||
desc_ru: "Содержит функции для взаимодействия с сетью"
|
desc_ru: "Содержит функции для взаимодействия с сетью"
|
||||||
constants: []
|
constants: []
|
||||||
functions:
|
functions:
|
||||||
- name: "http"
|
- name: http
|
||||||
args: "url"
|
args: "url, ..."
|
||||||
desc: |-
|
desc: |-
|
||||||
performs GET-request to `url`.
|
performs GET-request to `url`.
|
||||||
|
|
||||||
@ -65,6 +65,18 @@ functions:
|
|||||||
http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
|
http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
|
||||||
println "Added: " + v
|
println "Added: " + v
|
||||||
})
|
})
|
||||||
|
- name: httpSync
|
||||||
|
args: 'url, method = "GET", requestParams = {}, options = {}'
|
||||||
|
desc: Synchronous version of `http` function. See above for parameters description.
|
||||||
|
desc_ru: Синхронная версия функции `http`. См. выше описание параметров.
|
||||||
|
example: |-
|
||||||
|
use http
|
||||||
|
extract(isOk, content) = httpSync("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10})
|
||||||
|
if (isOk) {
|
||||||
|
println "Added: " + content
|
||||||
|
} else {
|
||||||
|
println "Failure"
|
||||||
|
}
|
||||||
- name: "download"
|
- name: "download"
|
||||||
args: "url"
|
args: "url"
|
||||||
desc: "downloads content by url as bytes array"
|
desc: "downloads content by url as bytes array"
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.annimon.ownlang.modules.http;
|
package com.annimon.ownlang.modules.http;
|
||||||
|
|
||||||
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
|
||||||
import com.annimon.ownlang.lib.*;
|
import com.annimon.ownlang.lib.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
@ -14,7 +13,7 @@ import okhttp3.RequestBody;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.internal.http.HttpMethod;
|
import okhttp3.internal.http.HttpMethod;
|
||||||
|
|
||||||
public final class http_http implements Function {
|
public final class HttpFunctions {
|
||||||
|
|
||||||
private static final Value
|
private static final Value
|
||||||
HEADER_KEY = new StringValue("header"),
|
HEADER_KEY = new StringValue("header"),
|
||||||
@ -27,71 +26,76 @@ public final class http_http implements Function {
|
|||||||
|
|
||||||
private final OkHttpClient client = new OkHttpClient();
|
private final OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
@Override
|
public Value httpSync(Value[] args) {
|
||||||
public Value execute(Value[] args) {
|
Arguments.checkRange(1, 4, args.length);
|
||||||
String url, method;
|
|
||||||
Function function;
|
String url = args[0].asString();
|
||||||
|
String method = (args.length >= 2) ? args[1].asString() : "GET";
|
||||||
|
Value requestParams = (args.length >= 3) ? args[2] : MapValue.EMPTY;
|
||||||
|
MapValue options = (args.length >= 4) ? ValueUtils.consumeMap(args[3], 3) : MapValue.EMPTY;
|
||||||
|
|
||||||
|
boolean isSuccessful;
|
||||||
|
Value result = options.containsKey(EXTENDED_RESULT) ? MapValue.EMPTY : StringValue.EMPTY;
|
||||||
|
try (Response response = executeRequest(url, method, requestParams, options)) {
|
||||||
|
isSuccessful = response.isSuccessful();
|
||||||
|
if (isSuccessful) {
|
||||||
|
result = getResult(response, options);
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
isSuccessful = false;
|
||||||
|
}
|
||||||
|
return new ArrayValue(new Value[] {
|
||||||
|
NumberValue.fromBoolean(isSuccessful),
|
||||||
|
result
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Value http(Value[] args) {
|
||||||
|
Arguments.checkRange(1, 5, args.length);
|
||||||
|
|
||||||
|
String url = args[0].asString();
|
||||||
|
String method = "GET";
|
||||||
|
Value requestParams = MapValue.EMPTY;
|
||||||
|
MapValue options = MapValue.EMPTY;
|
||||||
|
Function callback = FunctionValue.EMPTY.getValue();
|
||||||
|
|
||||||
switch (args.length) {
|
switch (args.length) {
|
||||||
case 1: // http(url)
|
case 1: // http(url)
|
||||||
url = args[0].asString();
|
break;
|
||||||
return process(url, "GET");
|
|
||||||
|
|
||||||
case 2: // http(url, method) || http(url, callback)
|
case 2: // http(url, method) || http(url, callback)
|
||||||
url = args[0].asString();
|
|
||||||
if (args[1].type() == Types.FUNCTION) {
|
if (args[1].type() == Types.FUNCTION) {
|
||||||
return process(url, "GET", ValueUtils.consumeFunction(args[1], 1));
|
callback = ValueUtils.consumeFunction(args[1], 1);
|
||||||
|
} else {
|
||||||
|
method = args[1].asString();
|
||||||
}
|
}
|
||||||
return process(url, args[1].asString());
|
break;
|
||||||
|
|
||||||
case 3: // http(url, method, params) || http(url, method, callback)
|
case 3: // http(url, method, params) || http(url, method, callback)
|
||||||
url = args[0].asString();
|
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
if (args[2].type() == Types.FUNCTION) {
|
if (args[2].type() == Types.FUNCTION) {
|
||||||
return process(url, method, ValueUtils.consumeFunction(args[2], 2));
|
callback = ValueUtils.consumeFunction(args[2], 2);
|
||||||
|
} else {
|
||||||
|
requestParams = args[2];
|
||||||
}
|
}
|
||||||
return process(url, method, args[2], FunctionValue.EMPTY.getValue());
|
break;
|
||||||
|
|
||||||
case 4: // http(url, method, params, callback)
|
case 4: // http(url, method, params, callback)
|
||||||
url = args[0].asString();
|
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
function = ValueUtils.consumeFunction(args[3], 3);
|
requestParams = args[2];
|
||||||
return process(url, method, args[2], function);
|
callback = ValueUtils.consumeFunction(args[3], 3);
|
||||||
|
break;
|
||||||
|
|
||||||
case 5: // http(url, method, params, headerParams, callback)
|
case 5: // http(url, method, params, headerParams, callback)
|
||||||
url = args[0].asString();
|
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
MapValue options = ValueUtils.consumeMap(args[3], 3);
|
requestParams = args[2];
|
||||||
function = ValueUtils.consumeFunction(args[4], 4);
|
options = ValueUtils.consumeMap(args[3], 3);
|
||||||
return process(url, method, args[2], options, function);
|
callback = ValueUtils.consumeFunction(args[4], 4);
|
||||||
|
break;
|
||||||
default:
|
|
||||||
throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private Value process(String url, String method) {
|
|
||||||
return process(url, method, FunctionValue.EMPTY.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
private Value process(String url, String method, Function callback) {
|
|
||||||
return process(url, method, MapValue.EMPTY, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Value process(String url, String method, Value params, Function callback) {
|
|
||||||
return process(url, method, params, MapValue.EMPTY, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Value process(String url, String methodStr, Value requestParams, MapValue options, Function callback) {
|
|
||||||
final String method = methodStr.toUpperCase();
|
|
||||||
try {
|
try {
|
||||||
final Request.Builder builder = new Request.Builder()
|
final Response response = executeRequest(url, method, requestParams, options);
|
||||||
.url(url)
|
|
||||||
.method(method, getRequestBody(method, requestParams, options));
|
|
||||||
if (options.containsKey(HEADER_KEY)) {
|
|
||||||
applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Response response = client.newCall(builder.build()).execute();
|
|
||||||
callback.execute(getResult(response, options));
|
callback.execute(getResult(response, options));
|
||||||
return NumberValue.fromBoolean(response.isSuccessful());
|
return NumberValue.fromBoolean(response.isSuccessful());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
@ -99,6 +103,17 @@ public final class http_http implements Function {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Response executeRequest(String url, String methodStr, Value requestParams, MapValue options) throws IOException {
|
||||||
|
final String method = methodStr.toUpperCase();
|
||||||
|
final Request.Builder builder = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.method(method, getRequestBody(method, requestParams, options));
|
||||||
|
if (options.containsKey(HEADER_KEY)) {
|
||||||
|
applyHeaderParams((MapValue) options.get(HEADER_KEY), builder);
|
||||||
|
}
|
||||||
|
return client.newCall(builder.build()).execute();
|
||||||
|
}
|
||||||
|
|
||||||
private Value getResult(Response response, MapValue options) throws IOException {
|
private Value getResult(Response response, MapValue options) throws IOException {
|
||||||
if (options.containsKey(EXTENDED_RESULT)) {
|
if (options.containsKey(EXTENDED_RESULT)) {
|
||||||
final MapValue map = new MapValue(10);
|
final MapValue map = new MapValue(10);
|
@ -19,9 +19,11 @@ public final class http implements Module {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Function> functions() {
|
public Map<String, Function> functions() {
|
||||||
|
final var httpFunctions = new HttpFunctions();
|
||||||
return Map.of(
|
return Map.of(
|
||||||
"urlencode", new http_urlencode(),
|
"urlencode", new http_urlencode(),
|
||||||
"http", new http_http(),
|
"http", httpFunctions::http,
|
||||||
|
"httpSync", httpFunctions::httpSync,
|
||||||
"download", new http_download()
|
"download", new http_download()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user