|
| 1 | +package hgapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + _ "fmt" |
| 8 | + "io" |
| 9 | + "io/ioutil" |
| 10 | + "net/http" |
| 11 | + _ "net/url" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// ----- API Definition ------------------------------------------------------- |
| 16 | + |
| 17 | +// 向HugeGraphServer发送gremlin语句(GET),同步执行 |
| 18 | +// |
| 19 | +// See full documentation at https://hugegraph.apache.org/cn/docs/clients/restful-api/gremlin/#811-%E5%90%91hugegraphserver%E5%8F%91%E9%80%81gremlin%E8%AF%AD%E5%8F%A5get%E5%90%8C%E6%AD%A5%E6%89%A7%E8%A1%8C |
| 20 | +// |
| 21 | +func newGremlinPostFunc(t Transport) GremlinPost { |
| 22 | + return func(o ...func(*GremlinPostRequest)) (*GremlinPostResponse, error) { |
| 23 | + var r = GremlinPostRequest{} |
| 24 | + for _, f := range o { |
| 25 | + f(&r) |
| 26 | + } |
| 27 | + return r.Do(r.ctx, t) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +type GremlinPost func(o ...func(*GremlinPostRequest)) (*GremlinPostResponse, error) |
| 32 | + |
| 33 | +type GremlinPostRequest struct { |
| 34 | + ctx context.Context |
| 35 | + Body io.ReadCloser `json:"-"` |
| 36 | + GremlinPost *GremlinPostRequestReqData |
| 37 | +} |
| 38 | + |
| 39 | +type GremlinPostRequestReqData struct { |
| 40 | + Gremlin string `json:"gremlin"` |
| 41 | + Bindings map[string]string `json:"bindings,omitempty"` |
| 42 | + Language string `json:"language"` |
| 43 | + Aliases struct { |
| 44 | + Graph string `json:"graph"` |
| 45 | + G string `json:"g"` |
| 46 | + } `json:"aliases"` |
| 47 | +} |
| 48 | + |
| 49 | +type GremlinPostResponse struct { |
| 50 | + StatusCode int `json:"-"` |
| 51 | + Header http.Header `json:"-"` |
| 52 | + Body io.ReadCloser `json:"-"` |
| 53 | + Data *GremlinPostResponseData `json:"data"` |
| 54 | +} |
| 55 | + |
| 56 | +type GremlinPostResponseData struct { |
| 57 | + RequestID string `json:"requestId,omitempty"` |
| 58 | + Status struct { |
| 59 | + Message string `json:"message"` |
| 60 | + Code int `json:"code"` |
| 61 | + Attributes struct { |
| 62 | + } `json:"attributes"` |
| 63 | + } `json:"status"` |
| 64 | + Result struct { |
| 65 | + Data interface{} `json:"data"` |
| 66 | + Meta struct { |
| 67 | + } `json:"meta"` |
| 68 | + } `json:"result,omitempty"` |
| 69 | + Exception string `json:"exception,omitempty"` |
| 70 | + Message string `json:"message,omitempty"` |
| 71 | + Cause string `json:"cause,omitempty"` |
| 72 | + Trace []string `json:"trace,omitempty"` |
| 73 | +} |
| 74 | + |
| 75 | +func (r GremlinPostRequest) Do(ctx context.Context, transport Transport) (*GremlinPostResponse, error) { |
| 76 | + |
| 77 | + if len(r.GremlinPost.Gremlin) < 1 { |
| 78 | + return nil, errors.New("GremlinPostRequest param error , gremlin is empty") |
| 79 | + } |
| 80 | + |
| 81 | + if len(r.GremlinPost.Language) < 1 { |
| 82 | + r.GremlinPost.Language = "gremlin-groovy" |
| 83 | + } |
| 84 | + |
| 85 | + // 重新修改参数 |
| 86 | + r.GremlinPost.Aliases = struct { |
| 87 | + Graph string `json:"graph"` |
| 88 | + G string `json:"g"` |
| 89 | + }(struct { |
| 90 | + Graph string |
| 91 | + G string |
| 92 | + }{ |
| 93 | + Graph: "${GRAPH_SPACE_NAME}-${GRAPH_NAME}", |
| 94 | + G: "__g_${GRAPH_SPACE_NAME}-${GRAPH_NAME}", |
| 95 | + }) |
| 96 | + |
| 97 | + byteBody, err := json.Marshal(&r.GremlinPost) // 序列化 |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + reader := strings.NewReader(string(byteBody)) // 转化为reader |
| 104 | + |
| 105 | + req, _ := newRequest("POST", "/gremlin", reader) |
| 106 | + |
| 107 | + if ctx != nil { |
| 108 | + req = req.WithContext(ctx) |
| 109 | + } |
| 110 | + |
| 111 | + res, err := transport.Perform(req) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + gremlinPostResp := &GremlinPostResponse{} |
| 117 | + bytes, err := ioutil.ReadAll(res.Body) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + respData := &GremlinPostResponseData{} |
| 123 | + err = json.Unmarshal(bytes, respData) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + gremlinPostResp.StatusCode = res.StatusCode |
| 128 | + gremlinPostResp.Header = res.Header |
| 129 | + gremlinPostResp.Body = res.Body |
| 130 | + gremlinPostResp.Data = respData |
| 131 | + return gremlinPostResp, nil |
| 132 | +} |
| 133 | + |
| 134 | +func (g *GremlinPost) WithGremlinPostData(gremlin GremlinPostRequestReqData) func(*GremlinPostRequest) { |
| 135 | + return func(r *GremlinPostRequest) { |
| 136 | + r.GremlinPost = &gremlin |
| 137 | + } |
| 138 | +} |
0 commit comments