You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by if...@apache.org on 2020/12/24 04:07:38 UTC
[incubator-nlpcraft] branch NLPCRAFT-91 updated: NLPCRAFT-91: Add
sign in to mod
This is an automated email from the ASF dual-hosted git repository.
ifropc pushed a commit to branch NLPCRAFT-91
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-91 by this push:
new 263b798 NLPCRAFT-91: Add sign in to mod
263b798 is described below
commit 263b7988fc5a7c59db35d0c3c3c6b90a7e899670
Author: Ifropc <if...@apache.org>
AuthorDate: Wed Dec 23 20:07:11 2020 -0800
NLPCRAFT-91: Add sign in to mod
---
.../org/apache/nplcraft/example/ExampleMod.java | 53 +++++++++++++++++-----
1 file changed, 41 insertions(+), 12 deletions(-)
diff --git a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java
index e92966a..e307341 100644
--- a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java
+++ b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java
@@ -40,6 +40,7 @@ public class ExampleMod {
private static final Logger LOGGER = LogManager.getLogger();
private final Gson gson = new Gson();
private MinecraftServer server;
+ private Optional<String> token = Optional.empty();
private boolean inRecursion = false;
public ExampleMod() {
@@ -71,24 +72,44 @@ public class ExampleMod {
}
private Optional<NCResponse> askProbe(String txt) {
- try {
- AskParams params = new AskParams();
- params.txt = txt.startsWith("/") ? txt.substring(1) : txt;
+ AskParams params = new AskParams();
+ params.txt = txt.startsWith("/") ? txt.substring(1) : txt;
+
+ Optional<String> optional = getToken();
+ if (!optional.isPresent()) {
+ return Optional.empty();
+ }
+ params.acsTok = optional.get();
+
+ return post("ask/sync", gson.toJson(params), NCResponse.class);
+ }
+
+ private Optional<String> getToken() {
+ if (!token.isPresent()) {
+ // TODO
+ NCSignIn sign = new NCSignIn();
+ sign.email = "admin@admin.com";
+ sign.password = "admin";
+
+ token = post("signin", gson.toJson(sign), NCSignResponse.class).map(x -> x.acsTok);
+ }
- String str = "http://0.0.0.0:8081/api/v1/ask/sync";
+ return token;
+ }
+
+ private <T> Optional<T> post(String url, String postJson, Class<T> clazz) {
+ try {
+ String str = "http://0.0.0.0:8081/api/v1/" + url;
- URL url = new URL(str);
- HttpURLConnection http = (HttpURLConnection) url.openConnection();
+ HttpURLConnection http = (HttpURLConnection) new URL(str).openConnection();
http.setRequestMethod("POST"); // PUT is another valid option
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.setConnectTimeout(1_000);
http.setReadTimeout(1_000);
- String postJsonData = gson.toJson(params);
-
http.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(http.getOutputStream());
- wr.writeBytes(postJsonData);
+ wr.writeBytes(postJson);
wr.flush();
wr.close();
@@ -96,7 +117,7 @@ public class ExampleMod {
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
- NCResponse response = gson.fromJson(in, NCResponse.class);
+ T response = gson.fromJson(in, clazz);
return Optional.of(response);
} catch (Exception e) {
@@ -107,9 +128,8 @@ public class ExampleMod {
}
private class AskParams {
- // TODO
- private final String acsTok = "4oe8dBpnpoSAbLo9BbzaL";
private final String mdlId = "nlpcraft.minecraft.ex";
+ private String acsTok;
private String txt;
@Override
@@ -151,4 +171,13 @@ public class ExampleMod {
'}';
}
}
+
+ private class NCSignIn {
+ String email;
+ String password;
+ }
+
+ private class NCSignResponse {
+ private String acsTok;
+ }
}