Move classes to separate modules

This commit is contained in:
aNNiMON 2023-08-26 17:19:03 +03:00 committed by Victor Melnik
parent 3aedda0e93
commit 219d654fe5
283 changed files with 722 additions and 714 deletions

View File

@ -6,19 +6,12 @@ import java.util.Map;
public final class functional_foreach implements Function {
private static final int UNKNOWN = -1;
@Override
public Value execute(Value... args) {
Arguments.check(2, args.length);
final Value container = args[0];
final Function consumer = ValueUtils.consumeFunction(args[1], 1);
final int argsCount;
if (consumer instanceof UserDefinedFunction) {
argsCount = ((UserDefinedFunction) consumer).getArgsCount();
} else {
argsCount = UNKNOWN;
}
final int argsCount = consumer.getArgsCount();
switch (container.type()) {
case Types.STRING:

View File

@ -1,62 +1,62 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.ValueUtils;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class CallValue extends MapValue {
private final Call call;
public CallValue(Call call) {
super(6);
this.call = call;
init();
}
private void init() {
set("cancel", Converters.voidToVoid(call::cancel));
set("enqueue", this::enqueue);
set("execute", this::execute);
set("isCanceled", Converters.voidToBoolean(call::isCanceled));
set("isExecuted", Converters.voidToBoolean(call::isExecuted));
}
private Value enqueue(Value[] args) {
Arguments.checkOrOr(1, 2, args.length);
final Function onResponse = ValueUtils.consumeFunction(args[0], 0);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
onResponse.execute(new CallValue(call), new ResponseValue(response));
}
@Override
public void onFailure(Call call, IOException e) {
if (args.length == 2) {
ValueUtils.consumeFunction(args[1], 1)
.execute(new CallValue(call), new StringValue(e.getMessage()));
}
}
});
return NumberValue.ZERO;
}
private Value execute(Value[] args) {
Arguments.check(0, args.length);
try {
return new ResponseValue(call.execute());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.ValueUtils;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class CallValue extends MapValue {
private final Call call;
public CallValue(Call call) {
super(6);
this.call = call;
init();
}
private void init() {
set("cancel", Converters.voidToVoid(call::cancel));
set("enqueue", this::enqueue);
set("execute", this::execute);
set("isCanceled", Converters.voidToBoolean(call::isCanceled));
set("isExecuted", Converters.voidToBoolean(call::isExecuted));
}
private Value enqueue(Value[] args) {
Arguments.checkOrOr(1, 2, args.length);
final Function onResponse = ValueUtils.consumeFunction(args[0], 0);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
onResponse.execute(new CallValue(call), new ResponseValue(response));
}
@Override
public void onFailure(Call call, IOException e) {
if (args.length == 2) {
ValueUtils.consumeFunction(args[1], 1)
.execute(new CallValue(call), new StringValue(e.getMessage()));
}
}
});
return NumberValue.ZERO;
}
private Value execute(Value[] args) {
Arguments.check(0, args.length);
try {
return new ResponseValue(call.execute());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,122 +1,122 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public class HttpClientValue extends MapValue {
private final OkHttpClient client;
public HttpClientValue(OkHttpClient client) {
super(10);
this.client = client;
init();
}
public OkHttpClient getClient() {
return client;
}
private void init() {
set("connectTimeoutMillis", Converters.voidToInt(client::connectTimeoutMillis));
set("followRedirects", Converters.voidToBoolean(client::followRedirects));
set("followSslRedirects", Converters.voidToBoolean(client::followSslRedirects));
set("newCall", args -> {
Arguments.check(1, args.length);
final Request request = Values.getRequest(args[0], " at first argument");
return new CallValue(client.newCall(request));
});
set("newWebSocket", this::newWebSocket);
set("pingIntervalMillis", Converters.voidToInt(client::pingIntervalMillis));
set("readTimeoutMillis", Converters.voidToInt(client::readTimeoutMillis));
set("retryOnConnectionFailure", Converters.voidToBoolean(client::retryOnConnectionFailure));
set("writeTimeoutMillis", Converters.voidToInt(client::writeTimeoutMillis));
}
private static final StringValue onOpen = new StringValue("onOpen");
private static final StringValue onTextMessage = new StringValue("onTextMessage");
private static final StringValue onBytesMessage = new StringValue("onBytesMessage");
private static final StringValue onClosing = new StringValue("onClosing");
private static final StringValue onClosed = new StringValue("onClosed");
private static final StringValue onFailure = new StringValue("onFailure");
private Value newWebSocket(Value[] args) {
Arguments.check(2, args.length);
final Request request = Values.getRequest(args[0], " at first argument");
if (args[1].type() != Types.MAP) {
throw new TypeException("Map expected at second argument");
}
final MapValue callbacks = (MapValue) args[1];
final WebSocket ws = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
final Value func = callbacks.get(onOpen);
if (func != null) {
ValueUtils.consumeFunction(func, " at onOpen").execute(
new WebSocketValue(webSocket),
new ResponseValue(response));
}
}
@Override
public void onMessage(WebSocket webSocket, String text) {
final Value func = callbacks.get(onTextMessage);
if (func != null) {
ValueUtils.consumeFunction(func, "at onTextMessage").execute(
new WebSocketValue(webSocket),
new StringValue(text));
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
final Value func = callbacks.get(onBytesMessage);
if (func != null) {
ValueUtils.consumeFunction(func, "at onBytesMessage").execute(
new WebSocketValue(webSocket),
ArrayValue.of(bytes.toByteArray()));
}
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
final Value func = callbacks.get(onClosing);
if (func != null) {
ValueUtils.consumeFunction(func, "at onClosing").execute(
new WebSocketValue(webSocket),
NumberValue.of(code),
new StringValue(reason));
}
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
final Value func = callbacks.get(onClosed);
if (func != null) {
ValueUtils.consumeFunction(func, "at onClosed").execute(
new WebSocketValue(webSocket),
NumberValue.of(code),
new StringValue(reason));
}
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
final Value func = callbacks.get(onFailure);
if (func != null) {
ValueUtils.consumeFunction(func, "at onFailure").execute(
new WebSocketValue(webSocket),
new StringValue(t.getMessage()),
new ResponseValue(response));
}
}
});
return new CallValue(client.newCall(request));
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public class HttpClientValue extends MapValue {
private final OkHttpClient client;
public HttpClientValue(OkHttpClient client) {
super(10);
this.client = client;
init();
}
public OkHttpClient getClient() {
return client;
}
private void init() {
set("connectTimeoutMillis", Converters.voidToInt(client::connectTimeoutMillis));
set("followRedirects", Converters.voidToBoolean(client::followRedirects));
set("followSslRedirects", Converters.voidToBoolean(client::followSslRedirects));
set("newCall", args -> {
Arguments.check(1, args.length);
final Request request = Values.getRequest(args[0], " at first argument");
return new CallValue(client.newCall(request));
});
set("newWebSocket", this::newWebSocket);
set("pingIntervalMillis", Converters.voidToInt(client::pingIntervalMillis));
set("readTimeoutMillis", Converters.voidToInt(client::readTimeoutMillis));
set("retryOnConnectionFailure", Converters.voidToBoolean(client::retryOnConnectionFailure));
set("writeTimeoutMillis", Converters.voidToInt(client::writeTimeoutMillis));
}
private static final StringValue onOpen = new StringValue("onOpen");
private static final StringValue onTextMessage = new StringValue("onTextMessage");
private static final StringValue onBytesMessage = new StringValue("onBytesMessage");
private static final StringValue onClosing = new StringValue("onClosing");
private static final StringValue onClosed = new StringValue("onClosed");
private static final StringValue onFailure = new StringValue("onFailure");
private Value newWebSocket(Value[] args) {
Arguments.check(2, args.length);
final Request request = Values.getRequest(args[0], " at first argument");
if (args[1].type() != Types.MAP) {
throw new TypeException("Map expected at second argument");
}
final MapValue callbacks = (MapValue) args[1];
final WebSocket ws = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
final Value func = callbacks.get(onOpen);
if (func != null) {
ValueUtils.consumeFunction(func, " at onOpen").execute(
new WebSocketValue(webSocket),
new ResponseValue(response));
}
}
@Override
public void onMessage(WebSocket webSocket, String text) {
final Value func = callbacks.get(onTextMessage);
if (func != null) {
ValueUtils.consumeFunction(func, "at onTextMessage").execute(
new WebSocketValue(webSocket),
new StringValue(text));
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
final Value func = callbacks.get(onBytesMessage);
if (func != null) {
ValueUtils.consumeFunction(func, "at onBytesMessage").execute(
new WebSocketValue(webSocket),
ArrayValue.of(bytes.toByteArray()));
}
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
final Value func = callbacks.get(onClosing);
if (func != null) {
ValueUtils.consumeFunction(func, "at onClosing").execute(
new WebSocketValue(webSocket),
NumberValue.of(code),
new StringValue(reason));
}
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
final Value func = callbacks.get(onClosed);
if (func != null) {
ValueUtils.consumeFunction(func, "at onClosed").execute(
new WebSocketValue(webSocket),
NumberValue.of(code),
new StringValue(reason));
}
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
final Value func = callbacks.get(onFailure);
if (func != null) {
ValueUtils.consumeFunction(func, "at onFailure").execute(
new WebSocketValue(webSocket),
new StringValue(t.getMessage()),
new ResponseValue(response));
}
}
});
return new CallValue(client.newCall(request));
}
}

View File

@ -1,70 +1,70 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
public class MultipartBodyBuilderValue extends MapValue {
private final MultipartBody.Builder builder;
public MultipartBodyBuilderValue() {
super(5);
this.builder = new MultipartBody.Builder();
init();
}
private void init() {
set("addFormData", this::addFormData);
set("addFormDataPart", this::addFormDataPart);
set("addPart", this::addPart);
set("build", args -> new MultipartBodyValue(builder.build()));
set("setType", args -> {
Arguments.check(1, args.length);
builder.setType(MediaType.parse(args[0].asString()));
return this;
});
}
private Value addFormDataPart(Value[] args) {
Arguments.checkOrOr(2, 3, args.length);
if (args.length == 2) {
builder.addFormDataPart(args[0].asString(), args[1].asString());
} else {
builder.addFormDataPart(
args[0].asString(),
args[1].asString(),
Values.getRequestBody(args[2], " at third argument"));
}
return this;
}
private Value addFormData(Value[] args) {
Arguments.check(1, args.length);
if (args[0].type() != Types.MAP) {
throw new TypeException("Map expected at first argument");
}
for (Map.Entry<Value, Value> entry : ((MapValue) args[0])) {
builder.addFormDataPart(entry.getKey().asString(), entry.getValue().asString());
}
return this;
}
private Value addPart(Value[] args) {
Arguments.checkOrOr(2, 3, args.length);
if (args.length == 1) {
builder.addPart(
Values.getRequestBody(args[0], " at first argument"));
} else {
builder.addPart(
Values.getHeaders(args[0], " at first argument"),
Values.getRequestBody(args[1], " at second argument"));
}
return this;
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
public class MultipartBodyBuilderValue extends MapValue {
private final MultipartBody.Builder builder;
public MultipartBodyBuilderValue() {
super(5);
this.builder = new MultipartBody.Builder();
init();
}
private void init() {
set("addFormData", this::addFormData);
set("addFormDataPart", this::addFormDataPart);
set("addPart", this::addPart);
set("build", args -> new MultipartBodyValue(builder.build()));
set("setType", args -> {
Arguments.check(1, args.length);
builder.setType(MediaType.parse(args[0].asString()));
return this;
});
}
private Value addFormDataPart(Value[] args) {
Arguments.checkOrOr(2, 3, args.length);
if (args.length == 2) {
builder.addFormDataPart(args[0].asString(), args[1].asString());
} else {
builder.addFormDataPart(
args[0].asString(),
args[1].asString(),
Values.getRequestBody(args[2], " at third argument"));
}
return this;
}
private Value addFormData(Value[] args) {
Arguments.check(1, args.length);
if (args[0].type() != Types.MAP) {
throw new TypeException("Map expected at first argument");
}
for (Map.Entry<Value, Value> entry : ((MapValue) args[0])) {
builder.addFormDataPart(entry.getKey().asString(), entry.getValue().asString());
}
return this;
}
private Value addPart(Value[] args) {
Arguments.checkOrOr(2, 3, args.length);
if (args.length == 1) {
builder.addPart(
Values.getRequestBody(args[0], " at first argument"));
} else {
builder.addPart(
Values.getHeaders(args[0], " at first argument"),
Values.getRequestBody(args[1], " at second argument"));
}
return this;
}
}

View File

@ -1,24 +1,24 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Converters;
import okhttp3.MultipartBody;
public class MultipartBodyValue extends RequestBodyValue {
private final MultipartBody multipartBody;
public MultipartBodyValue(MultipartBody multipartBody) {
super(multipartBody, 5);
this.multipartBody = multipartBody;
init();
}
public MultipartBody getMultipartBody() {
return multipartBody;
}
private void init() {
set("boundary", Converters.voidToString(multipartBody::boundary));
set("size", Converters.voidToInt(multipartBody::size));
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Converters;
import okhttp3.MultipartBody;
public class MultipartBodyValue extends RequestBodyValue {
private final MultipartBody multipartBody;
public MultipartBodyValue(MultipartBody multipartBody) {
super(multipartBody, 5);
this.multipartBody = multipartBody;
init();
}
public MultipartBody getMultipartBody() {
return multipartBody;
}
private void init() {
set("boundary", Converters.voidToString(multipartBody::boundary));
set("size", Converters.voidToInt(multipartBody::size));
}
}

View File

@ -1,55 +1,55 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.MediaType;
import okhttp3.RequestBody;
public class RequestBodyValue extends MapValue {
private final RequestBody requestBody;
private final MediaType mediaType;
public RequestBodyValue(RequestBody requestBody) {
this(requestBody, 0);
}
protected RequestBodyValue(RequestBody requestBody, int methodsCount) {
super(4 + methodsCount);
this.requestBody = requestBody;
this.mediaType = requestBody.contentType();
init();
}
public RequestBody getRequestBody() {
return requestBody;
}
public MediaType getMediaType() {
return mediaType;
}
private void init() {
set("getContentLength", Converters.voidToLong(() -> {
try {
return requestBody.contentLength();
} catch (IOException ex) {
return -1;
}
}));
set("getType", Converters.voidToString(mediaType::type));
set("getSubtype", Converters.voidToString(mediaType::subtype));
set("getCharset", args -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
return new StringValue(mediaType.charset().name());
} else {
return new StringValue(mediaType.charset(Charset.forName(args[0].asString())).name());
}
});
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.MediaType;
import okhttp3.RequestBody;
public class RequestBodyValue extends MapValue {
private final RequestBody requestBody;
private final MediaType mediaType;
public RequestBodyValue(RequestBody requestBody) {
this(requestBody, 0);
}
protected RequestBodyValue(RequestBody requestBody, int methodsCount) {
super(4 + methodsCount);
this.requestBody = requestBody;
this.mediaType = requestBody.contentType();
init();
}
public RequestBody getRequestBody() {
return requestBody;
}
public MediaType getMediaType() {
return mediaType;
}
private void init() {
set("getContentLength", Converters.voidToLong(() -> {
try {
return requestBody.contentLength();
} catch (IOException ex) {
return -1;
}
}));
set("getType", Converters.voidToString(mediaType::type));
set("getSubtype", Converters.voidToString(mediaType::subtype));
set("getCharset", args -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
return new StringValue(mediaType.charset().name());
} else {
return new StringValue(mediaType.charset(Charset.forName(args[0].asString())).name());
}
});
}
}

View File

@ -1,109 +1,109 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters.VoidToVoidFunction;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.MapValue;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class RequestBuilderValue extends MapValue {
private final Request.Builder requestBuilder;
public RequestBuilderValue() {
super(15);
requestBuilder = new Request.Builder();
init();
}
public Request getRequest() {
return requestBuilder.build();
}
private void init() {
set("addHeader", args -> {
Arguments.check(2, args.length);
requestBuilder.addHeader(args[0].asString(), args[1].asString());
return this;
});
set("cacheControl", args -> {
Arguments.check(1, args.length);
// TODO
return this;
});
set("delete", httpMethod(requestBuilder::delete, requestBuilder::delete));
set("get", args -> {
requestBuilder.get();
return this;
});
set("head", args -> {
requestBuilder.head();
return this;
});
set("header", args -> {
Arguments.check(2, args.length);
requestBuilder.header(args[0].asString(), args[1].asString());
return this;
});
set("headers", args -> {
Arguments.check(1, args.length);
requestBuilder.headers(Values.getHeaders(args[0], " at first argument"));
return this;
});
set("method", args -> {
Arguments.checkOrOr(1, 2, args.length);
final RequestBody body;
if (args.length == 1) {
body = null;
} else {
body = Values.getRequestBody(args[1], " at second argument");
}
requestBuilder.method(args[0].asString(), body);
return this;
});
set("newCall", args -> {
Arguments.check(1, args.length);
final OkHttpClient client = Values.getHttpClient(args[0], " at first argument");
return new CallValue(client.newCall(getRequest()));
});
set("patch", httpMethod(requestBuilder::patch));
set("post", httpMethod(requestBuilder::post));
set("put", httpMethod(requestBuilder::put));
set("removeHeader", args -> {
Arguments.check(1, args.length);
requestBuilder.removeHeader(args[0].asString());
return this;
});
set("url", args -> {
Arguments.check(1, args.length);
requestBuilder.url(args[0].asString());
return this;
});
}
private Function httpMethod(VoidToVoidFunction voidFunc, RequestBodyToVoidFunction bodyFunc) {
return (args) -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
voidFunc.apply();
} else {
bodyFunc.apply(Values.getRequestBody(args[0], " at first argument"));
}
return this;
};
}
private Function httpMethod(RequestBodyToVoidFunction bodyFunc) {
return (args) -> {
Arguments.check(1, args.length);
bodyFunc.apply(Values.getRequestBody(args[0], " at first argument"));
return this;
};
}
private interface RequestBodyToVoidFunction {
void apply(RequestBody value);
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Converters.VoidToVoidFunction;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.MapValue;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class RequestBuilderValue extends MapValue {
private final Request.Builder requestBuilder;
public RequestBuilderValue() {
super(15);
requestBuilder = new Request.Builder();
init();
}
public Request getRequest() {
return requestBuilder.build();
}
private void init() {
set("addHeader", args -> {
Arguments.check(2, args.length);
requestBuilder.addHeader(args[0].asString(), args[1].asString());
return this;
});
set("cacheControl", args -> {
Arguments.check(1, args.length);
// TODO
return this;
});
set("delete", httpMethod(requestBuilder::delete, requestBuilder::delete));
set("get", args -> {
requestBuilder.get();
return this;
});
set("head", args -> {
requestBuilder.head();
return this;
});
set("header", args -> {
Arguments.check(2, args.length);
requestBuilder.header(args[0].asString(), args[1].asString());
return this;
});
set("headers", args -> {
Arguments.check(1, args.length);
requestBuilder.headers(Values.getHeaders(args[0], " at first argument"));
return this;
});
set("method", args -> {
Arguments.checkOrOr(1, 2, args.length);
final RequestBody body;
if (args.length == 1) {
body = null;
} else {
body = Values.getRequestBody(args[1], " at second argument");
}
requestBuilder.method(args[0].asString(), body);
return this;
});
set("newCall", args -> {
Arguments.check(1, args.length);
final OkHttpClient client = Values.getHttpClient(args[0], " at first argument");
return new CallValue(client.newCall(getRequest()));
});
set("patch", httpMethod(requestBuilder::patch));
set("post", httpMethod(requestBuilder::post));
set("put", httpMethod(requestBuilder::put));
set("removeHeader", args -> {
Arguments.check(1, args.length);
requestBuilder.removeHeader(args[0].asString());
return this;
});
set("url", args -> {
Arguments.check(1, args.length);
requestBuilder.url(args[0].asString());
return this;
});
}
private Function httpMethod(VoidToVoidFunction voidFunc, RequestBodyToVoidFunction bodyFunc) {
return (args) -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
voidFunc.apply();
} else {
bodyFunc.apply(Values.getRequestBody(args[0], " at first argument"));
}
return this;
};
}
private Function httpMethod(RequestBodyToVoidFunction bodyFunc) {
return (args) -> {
Arguments.check(1, args.length);
bodyFunc.apply(Values.getRequestBody(args[0], " at first argument"));
return this;
};
}
private interface RequestBodyToVoidFunction {
void apply(RequestBody value);
}
}

View File

@ -1,56 +1,56 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import java.io.IOException;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
public class ResponseBodyValue extends MapValue {
private final ResponseBody responseBody;
public ResponseBodyValue(ResponseBody response) {
super(8);
this.responseBody = response;
init();
}
private void init() {
set("bytes", args -> {
try {
return ArrayValue.of(responseBody.bytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
set("close", Converters.voidToVoid(responseBody::close));
set("contentLength", Converters.voidToLong(responseBody::contentLength));
set("contentType", args -> new StringValue(responseBody.contentType().toString()));
set("string", args -> {
try {
return new StringValue(responseBody.string());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
set("file", args -> {
Arguments.check(1, args.length);
try {
BufferedSink sink = Okio.buffer(Okio.sink(Console.fileInstance(args[0].asString())));
sink.writeAll(responseBody.source());
sink.close();
return NumberValue.ONE;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import java.io.IOException;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
public class ResponseBodyValue extends MapValue {
private final ResponseBody responseBody;
public ResponseBodyValue(ResponseBody response) {
super(8);
this.responseBody = response;
init();
}
private void init() {
set("bytes", args -> {
try {
return ArrayValue.of(responseBody.bytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
set("close", Converters.voidToVoid(responseBody::close));
set("contentLength", Converters.voidToLong(responseBody::contentLength));
set("contentType", args -> new StringValue(responseBody.contentType().toString()));
set("string", args -> {
try {
return new StringValue(responseBody.string());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
set("file", args -> {
Arguments.check(1, args.length);
try {
BufferedSink sink = Okio.buffer(Okio.sink(Console.fileInstance(args[0].asString())));
sink.writeAll(responseBody.source());
sink.close();
return NumberValue.ONE;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}

View File

@ -1,52 +1,52 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import java.util.List;
import java.util.Map;
import okhttp3.Response;
public class ResponseValue extends MapValue {
private final Response response;
public ResponseValue(Response response) {
super(15);
this.response = response;
init();
}
private void init() {
set("body", args -> new ResponseBodyValue(response.body()));
set("cacheResponse", args -> new ResponseValue(response.cacheResponse()));
set("code", Converters.voidToInt(response::code));
set("close", Converters.voidToVoid(response::close));
set("header", args -> {
Arguments.checkOrOr(1, 2, args.length);
final String defaultValue = (args.length == 1) ? null : args[1].asString();
return new StringValue(response.header(args[0].asString(), defaultValue));
});
set("headers", args -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
final Map<String, List<String>> headers = response.headers().toMultimap();
final MapValue result = new MapValue(headers.size());
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
result.set(entry.getKey(), ArrayValue.of(entry.getValue().toArray(new String[0])));
}
return result;
} else {
return ArrayValue.of(response.headers(args[0].asString()).toArray(new String[0]));
}
});
set("message", Converters.voidToString(response::message));
set("networkResponse", args -> new ResponseValue(response.networkResponse()));
set("priorResponse", args -> new ResponseValue(response.priorResponse()));
set("receivedResponseAtMillis", Converters.voidToLong(response::receivedResponseAtMillis));
set("sentRequestAtMillis", Converters.voidToLong(response::sentRequestAtMillis));
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import java.util.List;
import java.util.Map;
import okhttp3.Response;
public class ResponseValue extends MapValue {
private final Response response;
public ResponseValue(Response response) {
super(15);
this.response = response;
init();
}
private void init() {
set("body", args -> new ResponseBodyValue(response.body()));
set("cacheResponse", args -> new ResponseValue(response.cacheResponse()));
set("code", Converters.voidToInt(response::code));
set("close", Converters.voidToVoid(response::close));
set("header", args -> {
Arguments.checkOrOr(1, 2, args.length);
final String defaultValue = (args.length == 1) ? null : args[1].asString();
return new StringValue(response.header(args[0].asString(), defaultValue));
});
set("headers", args -> {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
final Map<String, List<String>> headers = response.headers().toMultimap();
final MapValue result = new MapValue(headers.size());
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
result.set(entry.getKey(), ArrayValue.of(entry.getValue().toArray(new String[0])));
}
return result;
} else {
return ArrayValue.of(response.headers(args[0].asString()).toArray(new String[0]));
}
});
set("message", Converters.voidToString(response::message));
set("networkResponse", args -> new ResponseValue(response.networkResponse()));
set("priorResponse", args -> new ResponseValue(response.priorResponse()));
set("receivedResponseAtMillis", Converters.voidToLong(response::receivedResponseAtMillis));
set("sentRequestAtMillis", Converters.voidToLong(response::sentRequestAtMillis));
}
}

View File

@ -1,46 +1,46 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.ValueUtils;
import okhttp3.WebSocket;
import okio.ByteString;
public class WebSocketValue extends MapValue {
private final WebSocket ws;
protected WebSocketValue(WebSocket ws) {
super(4);
this.ws = ws;
init();
}
public WebSocket getWebSocket() {
return ws;
}
private void init() {
set("cancel", Converters.voidToVoid(ws::cancel));
set("close", args -> {
Arguments.checkOrOr(1, 2, args.length);
final String reason = (args.length == 2) ? args[1].asString() : null;
return NumberValue.fromBoolean(ws.close(args[0].asInt(), reason));
});
set("queueSize", Converters.voidToLong(ws::queueSize));
set("send", args -> {
Arguments.check(1, args.length);
final boolean result;
if (args[0].type() == Types.ARRAY) {
result = ws.send(ByteString.of( ValueUtils.toByteArray(((ArrayValue) args[0])) ));
} else {
result = ws.send(args[0].asString());
}
return NumberValue.fromBoolean(result);
});
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Converters;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.ValueUtils;
import okhttp3.WebSocket;
import okio.ByteString;
public class WebSocketValue extends MapValue {
private final WebSocket ws;
protected WebSocketValue(WebSocket ws) {
super(4);
this.ws = ws;
init();
}
public WebSocket getWebSocket() {
return ws;
}
private void init() {
set("cancel", Converters.voidToVoid(ws::cancel));
set("close", args -> {
Arguments.checkOrOr(1, 2, args.length);
final String reason = (args.length == 2) ? args[1].asString() : null;
return NumberValue.fromBoolean(ws.close(args[0].asInt(), reason));
});
set("queueSize", Converters.voidToLong(ws::queueSize));
set("send", args -> {
Arguments.check(1, args.length);
final boolean result;
if (args[0].type() == Types.ARRAY) {
result = ws.send(ByteString.of( ValueUtils.toByteArray(((ArrayValue) args[0])) ));
} else {
result = ws.send(args[0].asString());
}
return NumberValue.fromBoolean(result);
});
}
}

View File

@ -1,81 +1,81 @@
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.ValueUtils;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.modules.Module;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
public final class okhttp implements Module {
private static final HttpClientValue defaultClient = new HttpClientValue(new OkHttpClient());
public static void initConstants() {
MapValue requestBody = new MapValue(5);
requestBody.set("bytes", args -> {
Arguments.checkOrOr(2, 4, args.length);
if (args[1].type() != Types.ARRAY) {
throw new TypeException("Array of bytes expected at second argument");
}
final byte[] bytes = ValueUtils.toByteArray((ArrayValue) args[1]);
final int offset;
final int bytesCount;
if (args.length == 2) {
offset = 0;
bytesCount = bytes.length;
} else {
offset = args[2].asInt();
bytesCount = args[3].asInt();
}
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
bytes, offset, bytesCount
));
});
requestBody.set("file", args -> {
Arguments.check(2, args.length);
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
Console.fileInstance(args[1].asString())
));
});
requestBody.set("string", args -> {
Arguments.check(2, args.length);
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
args[1].asString()
));
});
Variables.define("RequestBody", requestBody);
MapValue multipartBody = new MapValue(10);
multipartBody.set("ALTERNATIVE", new StringValue(MultipartBody.ALTERNATIVE.toString()));
multipartBody.set("DIGEST", new StringValue(MultipartBody.DIGEST.toString()));
multipartBody.set("FORM", new StringValue(MultipartBody.FORM.toString()));
multipartBody.set("MIXED", new StringValue(MultipartBody.MIXED.toString()));
multipartBody.set("PARALLEL", new StringValue(MultipartBody.PARALLEL.toString()));
multipartBody.set("builder", args -> new MultipartBodyBuilderValue());
Variables.define("MultipartBody", multipartBody);
MapValue okhttp = new MapValue(5);
okhttp.set("client", defaultClient);
okhttp.set("request", args -> new RequestBuilderValue());
Variables.define("okhttp", okhttp);
}
@Override
public void init() {
initConstants();
}
}
package com.annimon.ownlang.modules.okhttp;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.ValueUtils;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.modules.Module;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
public final class okhttp implements Module {
private static final HttpClientValue defaultClient = new HttpClientValue(new OkHttpClient());
public static void initConstants() {
MapValue requestBody = new MapValue(5);
requestBody.set("bytes", args -> {
Arguments.checkOrOr(2, 4, args.length);
if (args[1].type() != Types.ARRAY) {
throw new TypeException("Array of bytes expected at second argument");
}
final byte[] bytes = ValueUtils.toByteArray((ArrayValue) args[1]);
final int offset;
final int bytesCount;
if (args.length == 2) {
offset = 0;
bytesCount = bytes.length;
} else {
offset = args[2].asInt();
bytesCount = args[3].asInt();
}
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
bytes, offset, bytesCount
));
});
requestBody.set("file", args -> {
Arguments.check(2, args.length);
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
Console.fileInstance(args[1].asString())
));
});
requestBody.set("string", args -> {
Arguments.check(2, args.length);
return new RequestBodyValue(RequestBody.create(
MediaType.parse(args[0].asString()),
args[1].asString()
));
});
Variables.define("RequestBody", requestBody);
MapValue multipartBody = new MapValue(10);
multipartBody.set("ALTERNATIVE", new StringValue(MultipartBody.ALTERNATIVE.toString()));
multipartBody.set("DIGEST", new StringValue(MultipartBody.DIGEST.toString()));
multipartBody.set("FORM", new StringValue(MultipartBody.FORM.toString()));
multipartBody.set("MIXED", new StringValue(MultipartBody.MIXED.toString()));
multipartBody.set("PARALLEL", new StringValue(MultipartBody.PARALLEL.toString()));
multipartBody.set("builder", args -> new MultipartBodyBuilderValue());
Variables.define("MultipartBody", multipartBody);
MapValue okhttp = new MapValue(5);
okhttp.set("client", defaultClient);
okhttp.set("request", args -> new RequestBuilderValue());
Variables.define("okhttp", okhttp);
}
@Override
public void init() {
initConstants();
}
}

View File

@ -1,6 +1,7 @@
package com.annimon.ownlang.modules.std;
import com.annimon.ownlang.Main;
import com.annimon.ownlang.Shared;
import com.annimon.ownlang.Version;
import com.annimon.ownlang.lib.*;
import com.annimon.ownlang.modules.Module;
@ -13,17 +14,17 @@ public final class std implements Module {
public static void initConstants() {
MapValue ownlang = new MapValue(5);
ownlang.set("PLATFORM", new StringValue("desktop"));
ownlang.set("VERSION", new StringValue(Main.VERSION));
ownlang.set("VERSION_MAJOR", NumberValue.of(Main.VERSION_MAJOR));
ownlang.set("VERSION_MINOR", NumberValue.of(Main.VERSION_MINOR));
ownlang.set("VERSION_PATCH", NumberValue.of(Main.VERSION_PATCH));
ownlang.set("VERSION", new StringValue(Version.VERSION));
ownlang.set("VERSION_MAJOR", NumberValue.of(Version.VERSION_MAJOR));
ownlang.set("VERSION_MINOR", NumberValue.of(Version.VERSION_MINOR));
ownlang.set("VERSION_PATCH", NumberValue.of(Version.VERSION_PATCH));
Variables.define("OwnLang", ownlang);
}
@Override
public void init() {
initConstants();
Variables.define("ARGS", ArrayValue.of(Main.getOwnlangArgs())); // is not constant
Variables.define("ARGS", ArrayValue.of(Shared.getOwnlangArgs())); // is not constant
Functions.set("echo", new std_echo());
Functions.set("readln", new std_readln());
Functions.set("length", new std_length());

View File

@ -22,11 +22,7 @@ public final class std_length implements Function {
break;
case Types.FUNCTION:
final Function func = ((FunctionValue) val).getValue();
if (func instanceof UserDefinedFunction) {
length = ((UserDefinedFunction) func).getArgsCount();
} else {
length = 0;
}
length = func.getArgsCount();
break;
default:
length = 0;

Some files were not shown because too many files have changed in this diff Show More