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

jin pushed a commit to branch tmp-go
in repository 
https://gitbox.apache.org/repos/asf/incubator-hugegraph-toolchain.git


The following commit(s) were added to refs/heads/tmp-go by this push:
     new fdf36c9d add Gremlin post API
fdf36c9d is described below

commit fdf36c9d9c42c10b5cd903bfce595ac23675608f
Author: izliang <[email protected]>
AuthorDate: Tue Oct 31 15:41:07 2023 +0800

    add Gremlin post API
    
    Change-Id: I12a2f6f9153d41d1ff7fae1319707b2c8c653241
---
 hugegraph-client-go/api/v1/api._.go                |   8 ++
 hugegraph-client-go/api/v1/api.gremlin.post.go     | 135 +++++++++++++++++++++
 .../api/v1/api.gremlin.post_test.go                |  24 ++++
 hugegraph-client-go/hgtransport/hgtransport.go     |  11 +-
 4 files changed, 168 insertions(+), 10 deletions(-)

diff --git a/hugegraph-client-go/api/v1/api._.go 
b/hugegraph-client-go/api/v1/api._.go
index f290a5d4..e509377c 100644
--- a/hugegraph-client-go/api/v1/api._.go
+++ b/hugegraph-client-go/api/v1/api._.go
@@ -27,6 +27,9 @@ type APIV1 struct {
     Vertex  struct {
         Create
     }
+    Gremlin struct {
+        GremlinPost
+    }
 }
 
 // New creates new API
@@ -38,5 +41,10 @@ func New(t api.Transport) *APIV1 {
         }{
             Create: newCreateFunc(t),
         },
+        Gremlin: struct {
+            GremlinPost
+        }{
+            GremlinPost: newGremlinPostFunc(t),
+        },
     }
 }
diff --git a/hugegraph-client-go/api/v1/api.gremlin.post.go 
b/hugegraph-client-go/api/v1/api.gremlin.post.go
new file mode 100644
index 00000000..846ab39f
--- /dev/null
+++ b/hugegraph-client-go/api/v1/api.gremlin.post.go
@@ -0,0 +1,135 @@
+package v1
+
+import (
+    "context"
+    "encoding/json"
+    "errors"
+    "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api"
+    "io"
+    "io/ioutil"
+    "net/http"
+    "strings"
+)
+
+// ----- API Definition -------------------------------------------------------
+// View Create a vertex
+//
+// See full documentation at 
https://hugegraph.apache.org/docs/clients/restful-api/vertex/#211-create-a-vertex
+func newGremlinPostFunc(t api.Transport) GremlinPost {
+    return func(o ...func(*GremlinPostRequest)) (*GremlinPostResponse, error) {
+        var r = GremlinPostRequest{}
+        for _, f := range o {
+            f(&r)
+        }
+        return r.Do(r.ctx, t)
+    }
+}
+
+type GremlinPost func(o ...func(*GremlinPostRequest)) (*GremlinPostResponse, 
error)
+
+type GremlinPostRequest struct {
+    ctx      context.Context
+    body     io.ReadCloser
+    gremlin  string
+    bindings map[string]string
+    language string
+    aliases  struct {
+        //Graph string `json:"graph"`
+        //G     string `json:"g"`
+    }
+}
+
+type GremlinPostRequestData struct {
+    Gremlin  string            `json:"gremlin"`
+    Bindings map[string]string `json:"bindings,omitempty"`
+    Language string            `json:"language,omitempty"`
+    Aliases  struct {
+        //Graph string `json:"graph"`
+        //G     string `json:"g"`
+    } `json:"aliases,omitempty"`
+}
+
+type GremlinPostResponse struct {
+    StatusCode int                      `json:"-"`
+    Header     http.Header              `json:"-"`
+    Body       io.ReadCloser            `json:"-"`
+    Data       *GremlinPostResponseData `json:"data"`
+}
+
+type GremlinPostResponseData struct {
+    RequestID string `json:"requestId,omitempty"`
+    Status    struct {
+        Message    string `json:"message"`
+        Code       int    `json:"code"`
+        Attributes struct {
+        } `json:"attributes"`
+    } `json:"status"`
+    Result struct {
+        Data interface{} `json:"data"`
+        Meta interface{} `json:"meta"`
+    } `json:"result,omitempty"`
+    Exception string   `json:"exception,omitempty"`
+    Message   string   `json:"message,omitempty"`
+    Cause     string   `json:"cause,omitempty"`
+    Trace     []string `json:"trace,omitempty"`
+}
+
+func (g GremlinPostRequest) Do(ctx context.Context, transport api.Transport) 
(*GremlinPostResponse, error) {
+
+    if len(g.gremlin) < 1 {
+        return nil, errors.New("GremlinPostRequest param error , gremlin is 
empty")
+    }
+
+    if len(g.language) < 1 {
+        g.language = "gremlin-groovy"
+    }
+    
+    gd := &GremlinPostRequestData{
+        Gremlin:  g.gremlin,
+        Bindings: g.bindings,
+        Language: g.language,
+        Aliases:  g.aliases,
+    }
+    
+    byteBody, err := json.Marshal(&gd) // 序列化
+
+    if err != nil {
+        return nil, err
+    }
+
+    reader := strings.NewReader(string(byteBody)) // 转化为reader
+
+    req, _ := api.NewRequest("POST", "/gremlin", reader)
+
+    if ctx != nil {
+        req = req.WithContext(ctx)
+    }
+
+    res, err := transport.Perform(req)
+    if err != nil {
+        return nil, err
+    }
+
+    gremlinPostResp := &GremlinPostResponse{}
+    bytes, err := ioutil.ReadAll(res.Body)
+    if err != nil {
+        return nil, err
+    }
+
+    respData := &GremlinPostResponseData{}
+    err = json.Unmarshal(bytes, respData)
+    if err != nil {
+        return nil, err
+    }
+    gremlinPostResp.StatusCode = res.StatusCode
+    gremlinPostResp.Header = res.Header
+    gremlinPostResp.Body = res.Body
+    gremlinPostResp.Data = respData
+    return gremlinPostResp, nil
+}
+
+func (g GremlinPost) WithGremlin(gremlin string) func(request 
*GremlinPostRequest) {
+    return func(r *GremlinPostRequest) {
+        r.gremlin = gremlin
+    }
+}
diff --git a/hugegraph-client-go/api/v1/api.gremlin.post_test.go 
b/hugegraph-client-go/api/v1/api.gremlin.post_test.go
new file mode 100644
index 00000000..eb4c65d4
--- /dev/null
+++ b/hugegraph-client-go/api/v1/api.gremlin.post_test.go
@@ -0,0 +1,24 @@
+package v1_test
+
+import (
+    "fmt"
+    "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go"
+    "log"
+    "testing"
+)
+
+func TestGremlinPostRequest_Do(t *testing.T) {
+
+    client, err := hugegraph.NewDefaultCommonClient()
+    if err != nil {
+        log.Println(err)
+    }
+    resp, err := client.Gremlin.GremlinPost(
+        
client.Gremlin.GremlinPost.WithGremlin("hugegraph.traversal().V().limit(3)"),
+    )
+    if err != nil {
+        log.Fatalln(err)
+    }
+    fmt.Println(resp.Data.Result.Data)
+
+}
diff --git a/hugegraph-client-go/hgtransport/hgtransport.go 
b/hugegraph-client-go/hgtransport/hgtransport.go
index 04ac3fe3..8c669c8b 100644
--- a/hugegraph-client-go/hgtransport/hgtransport.go
+++ b/hugegraph-client-go/hgtransport/hgtransport.go
@@ -19,7 +19,6 @@ package hgtransport
 
 import (
     "bytes"
-    "fmt"
     "io/ioutil"
     "net/http"
     "net/url"
@@ -110,13 +109,11 @@ func (c *Client) Perform(req *http.Request) 
(*http.Response, error) {
     c.setUserAgent(req)
     c.setHost(req)
     c.setContentTypeJSON(req)
-    c.setGraph(req)
 
     if _, ok := req.Header["Authorization"]; !ok {
         c.setBasicAuth(u, req)
     }
 
-    fmt.Println(req.Header)
     var dupReqBody *bytes.Buffer
     if c.logger != nil && c.logger.RequestBodyEnabled() {
         if req.Body != nil && req.Body != http.NoBody {
@@ -195,13 +192,7 @@ func (c *Client) setHost(req *http.Request) *http.Request {
 }
 
 func (c *Client) setContentTypeJSON(req *http.Request) *http.Request {
-    req.Header.Set("Content-Type", "application/json")
-    return req
-}
-
-func (c *Client) setGraph(req *http.Request) *http.Request {
-    req.URL.RawQuery = strings.ReplaceAll(req.URL.RawQuery, 
url.QueryEscape("${GRAPH_NAME}"), c.graph)
-    req.URL.Path = strings.ReplaceAll(req.URL.Path, "${GRAPH_NAME}", c.graph)
+    req.Header.Set("Content-Type", "application/json;charset=UTF-8")
     return req
 }
 

Reply via email to