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:29:49 UTC

[incubator-nlpcraft] branch NLPCRAFT-91 updated (263b798 -> d000caf)

This is an automated email from the ASF dual-hosted git repository.

ifropc pushed a change to branch NLPCRAFT-91
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git.


 discard 263b798  NLPCRAFT-91: Add sign in to mod
     new d000caf  NLPCRAFT-91: Add sign in to mod

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (263b798)
            \
             N -- N -- N   refs/heads/NLPCRAFT-91 (d000caf)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/main/java/org/apache/nplcraft/example/ExampleMod.java         | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


[incubator-nlpcraft] 01/01: NLPCRAFT-91: Add sign in to mod

Posted by if...@apache.org.
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

commit d000caf3b737cb16495c66169b911039f95c3aae
Author: Ifropc <if...@apache.org>
AuthorDate: Wed Dec 23 20:29:37 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..12b5039 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.passwd = "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 passwd;
+    }
+
+    private class NCSignResponse {
+        private String acsTok;
+    }
 }