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
commit 3f71ec0c59580f1a57f3eb168160853f14985a50 Author: izliang <[email protected]> AuthorDate: Wed Nov 1 13:00:15 2023 +0800 add apis Change-Id: Icfee0decd5a6753766c62551f7cf643e522fa75f --- hugegraph-client-go/api/api.resquest.go | 11 +- hugegraph-client-go/api/v1/api._.go | 6 + hugegraph-client-go/api/v1/api.gremlin.get.go | 120 +++++++++++++++++ hugegraph-client-go/api/v1/api.gremlin.get_test.go | 58 +++++++++ hugegraph-client-go/api/v1/api.gremlin.post.go | 2 +- .../api/v1/api.propertykey.create.go | 2 +- .../api/v1/api.propertykey.delete_by_name.go | 2 +- .../api/v1/api.propertykey.get_all.go | 2 +- .../api/v1/api.propertykey.get_by_name.go | 2 +- .../api/v1/api.propertykey.update_userdata.go | 144 +++++++++++++++++++++ .../api/v1/api.propertykey.update_userdata_test.go | 54 ++++++++ hugegraph-client-go/api/v1/api.schema.go | 2 +- hugegraph-client-go/api/v1/api.veretx.create.go | 2 +- hugegraph-client-go/api/v1/api.version.go | 2 +- 14 files changed, 399 insertions(+), 10 deletions(-) diff --git a/hugegraph-client-go/api/api.resquest.go b/hugegraph-client-go/api/api.resquest.go index 6f14e80a..9d08acfe 100644 --- a/hugegraph-client-go/api/api.resquest.go +++ b/hugegraph-client-go/api/api.resquest.go @@ -41,10 +41,17 @@ type Request interface { } // newRequest creates an HTTP request. -func NewRequest(method, path string, body io.Reader) (*http.Request, error) { +func NewRequest(method, path string, params *url.Values, body io.Reader) (*http.Request, error) { + + u := &url.URL{ + Path: path, + } + if params != nil { + u.RawQuery = params.Encode() + } r := http.Request{ Method: method, - URL: &url.URL{Path: path}, + URL: u, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1, diff --git a/hugegraph-client-go/api/v1/api._.go b/hugegraph-client-go/api/v1/api._.go index 759791a4..a68e0359 100644 --- a/hugegraph-client-go/api/v1/api._.go +++ b/hugegraph-client-go/api/v1/api._.go @@ -29,6 +29,7 @@ type APIV1 struct { Create } Gremlin struct { + GremlinGet GremlinPost } PropertyKey struct { @@ -36,6 +37,7 @@ type APIV1 struct { PropertyKeyGetByName PropertyKeyCreate PropertyKeyDeleteByName + PropertyKeyUpdateUserdata } } @@ -50,8 +52,10 @@ func New(t api.Transport) *APIV1 { Create: newCreateFunc(t), }, Gremlin: struct { + GremlinGet GremlinPost }{ + GremlinGet: newGremlinGetFunc(t), GremlinPost: newGremlinPostFunc(t), }, PropertyKey: struct { @@ -59,11 +63,13 @@ func New(t api.Transport) *APIV1 { PropertyKeyGetByName PropertyKeyCreate PropertyKeyDeleteByName + PropertyKeyUpdateUserdata }{ PropertyKeyGetAll: newPropertyKeyGetAllFunc(t), PropertyKeyGetByName: newPropertyKeyGetByNameFunc(t), PropertyKeyCreate: newPropertyKeyCreateFunc(t), PropertyKeyDeleteByName: newPropertyKeyDeleteByNameFunc(t), + PropertyKeyUpdateUserdata: newPropertyKeyUpdateUserdataFunc(t), }, } } diff --git a/hugegraph-client-go/api/v1/api.gremlin.get.go b/hugegraph-client-go/api/v1/api.gremlin.get.go new file mode 100644 index 00000000..a6e148be --- /dev/null +++ b/hugegraph-client-go/api/v1/api.gremlin.get.go @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package v1 + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" + "io" + "io/ioutil" + "net/http" + url2 "net/url" +) + +// ----- API Definition ------------------------------------------------------- +// View Create a vertex +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/vertex/#211-create-a-vertex +func newGremlinGetFunc(t api.Transport) GremlinGet { + return func(o ...func(*GremlinGetRequest)) (*GremlinGetResponse, error) { + var r = GremlinGetRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type GremlinGet func(o ...func(*GremlinGetRequest)) (*GremlinGetResponse, error) + +type GremlinGetRequest struct { + ctx context.Context + gremlin string + bindings map[string]string + language string + aliases map[string]string +} + +type GremlinGetResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` +} + +func (g GremlinGetRequest) Do(ctx context.Context, transport api.Transport) (*GremlinGetResponse, error) { + + url := "/gremlin" + params := &url2.Values{} + if len(g.gremlin) <= 0 { + return nil, errors.New("please set gremlin") + } else { + params.Add("gremlin", g.gremlin) + } + if len(g.language) > 0 { + params.Add("language", g.language) + } + + if g.aliases != nil && len(g.aliases) >= 0 { + aliasesJsonStr, err := json.Marshal(g.aliases) + if err != nil { + return nil, err + } + params.Add("aliases", string(aliasesJsonStr)) + } + + if g.bindings != nil && len(g.bindings) >= 0 { + bindingsJsonStr, err := json.Marshal(g.bindings) + if err != nil { + return nil, err + } + params.Add("bindings", string(bindingsJsonStr)) + } + + req, err := api.NewRequest("GET", url, params, nil) + if err != nil { + return nil, err + } + if ctx != nil { + req = req.WithContext(ctx) + } + + res, err := transport.Perform(req) + if err != nil { + return nil, err + } + + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + + fmt.Println(string(bytes)) + + gremlinGetResponse := &GremlinGetResponse{} + gremlinGetResponse.StatusCode = res.StatusCode + return gremlinGetResponse, nil +} + +func (g GremlinGet) WithGremlin(gremlin string) func(request *GremlinGetRequest) { + return func(r *GremlinGetRequest) { + r.gremlin = gremlin + } +} diff --git a/hugegraph-client-go/api/v1/api.gremlin.get_test.go b/hugegraph-client-go/api/v1/api.gremlin.get_test.go new file mode 100644 index 00000000..de7de4c5 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.gremlin.get_test.go @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package v1_test + +import ( + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" + "io/ioutil" + "log" + "net/http" + "testing" +) + +func TestGremlinGetRequest_Do(t *testing.T) { + + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + resp, err := client.Gremlin.GremlinGet( + client.Gremlin.GremlinGet.WithGremlin("hugegraph.traversal().V().limit(3)"), + ) + if err != nil { + log.Fatalln(err) + } + fmt.Println(resp.Body) + + t1() +} + +func t1() { + url := "http://82.157.70.33:18080/gremlin?gremlin=hugegraph.traversal().V().limit(3)" + + req, _ := http.NewRequest("GET", url, nil) + + res, _ := http.DefaultClient.Do(req) + + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + + fmt.Println(res) + fmt.Println(string(body)) +} \ No newline at end of file diff --git a/hugegraph-client-go/api/v1/api.gremlin.post.go b/hugegraph-client-go/api/v1/api.gremlin.post.go index 17ccb6a5..ed02249a 100644 --- a/hugegraph-client-go/api/v1/api.gremlin.post.go +++ b/hugegraph-client-go/api/v1/api.gremlin.post.go @@ -115,7 +115,7 @@ func (g GremlinPostRequest) Do(ctx context.Context, transport api.Transport) (*G } reader := strings.NewReader(string(byteBody)) - req, _ := api.NewRequest("POST", "/gremlin", reader) + req, _ := api.NewRequest("POST", "/gremlin", nil, reader) if ctx != nil { req = req.WithContext(ctx) diff --git a/hugegraph-client-go/api/v1/api.propertykey.create.go b/hugegraph-client-go/api/v1/api.propertykey.create.go index f903e8f8..02f62770 100644 --- a/hugegraph-client-go/api/v1/api.propertykey.create.go +++ b/hugegraph-client-go/api/v1/api.propertykey.create.go @@ -91,7 +91,7 @@ func (r PropertyKeyCreateRequest) Do(ctx context.Context, transport api.Transpor } reader := strings.NewReader(string(byteBody)) - req, err := api.NewRequest("POST", fmt.Sprintf("/graphs/%s/schema/propertykeys", transport.GetConfig().Graph), reader) + req, err := api.NewRequest("POST", fmt.Sprintf("/graphs/%s/schema/propertykeys", transport.GetConfig().Graph), nil, reader) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.propertykey.delete_by_name.go b/hugegraph-client-go/api/v1/api.propertykey.delete_by_name.go index d6a86784..b85cd5f3 100644 --- a/hugegraph-client-go/api/v1/api.propertykey.delete_by_name.go +++ b/hugegraph-client-go/api/v1/api.propertykey.delete_by_name.go @@ -70,7 +70,7 @@ func (r PropertyKeyDeleteByNameRequest) Do(ctx context.Context, transport api.Tr if len(r.name) <= 0 { return nil, errors.New("delete by name ,please set name") } - req, err := api.NewRequest("DELETE", fmt.Sprintf("/graphs/%s/schema/propertykeys/%s", transport.GetConfig().Graph, r.name), r.Body) + req, err := api.NewRequest("DELETE", fmt.Sprintf("/graphs/%s/schema/propertykeys/%s", transport.GetConfig().Graph, r.name), nil, r.Body) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.propertykey.get_all.go b/hugegraph-client-go/api/v1/api.propertykey.get_all.go index 4005b355..28735269 100644 --- a/hugegraph-client-go/api/v1/api.propertykey.get_all.go +++ b/hugegraph-client-go/api/v1/api.propertykey.get_all.go @@ -70,7 +70,7 @@ type PropertyKeyGetAllResponseData struct { func (r PropertyKeyGetAllRequest) Do(ctx context.Context, transport api.Transport) (*PropertyKeyGetAllResponse, error) { - req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema/propertykeys", transport.GetConfig().Graph), r.Body) + req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema/propertykeys", transport.GetConfig().Graph), nil, r.Body) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.propertykey.get_by_name.go b/hugegraph-client-go/api/v1/api.propertykey.get_by_name.go index 13acf8a0..2390f274 100644 --- a/hugegraph-client-go/api/v1/api.propertykey.get_by_name.go +++ b/hugegraph-client-go/api/v1/api.propertykey.get_by_name.go @@ -80,7 +80,7 @@ func (r PropertyKeyGetByNameRequest) Do(ctx context.Context, transport api.Trans return nil, errors.New("get_by_name must set name") } - req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema/propertykeys/%s", transport.GetConfig().Graph, r.name), r.Body) + req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema/propertykeys/%s", transport.GetConfig().Graph, r.name), nil, r.Body) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.propertykey.update_userdata.go b/hugegraph-client-go/api/v1/api.propertykey.update_userdata.go new file mode 100644 index 00000000..89711355 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.update_userdata.go @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, PropertyKeyUpdateUserdata 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package v1 + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/internal/model" + "io" + "io/ioutil" + "net/http" + "net/url" + "strings" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// Add or Remove userdata for an existing PropertyKey +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/propertykey/#122-add-or-remove-userdata-for-an-existing-propertykey +func newPropertyKeyUpdateUserdataFunc(t api.Transport) PropertyKeyUpdateUserdata { + return func(o ...func(*PropertyKeyUpdateUserdataRequest)) (*PropertyKeyUpdateUserdataResponse, error) { + var r = PropertyKeyUpdateUserdataRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type PropertyKeyUpdateUserdata func(o ...func(*PropertyKeyUpdateUserdataRequest)) (*PropertyKeyUpdateUserdataResponse, error) + +type PropertyKeyUpdateUserdataRequest struct { + Body io.Reader + ctx context.Context + reqData PropertyKeyUpdateUserdataRequestData +} + +type PropertyKeyUpdateUserdataRequestData struct { + Action model.Action `json:"-"` + Name string `json:"name"` + UserData struct { + Min int `json:"min"` + Max int `json:"max"` + } `json:"user_data"` +} + +type PropertyKeyUpdateUserdataResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + PropertyKeyUpdateUserdata PropertyKeyUpdateUserdataResponseData `json:"-"` +} + +type PropertyKeyUpdateUserdataResponseData struct { + PropertyKey struct { + ID int `json:"id"` + Name string `json:"name"` + DataType string `json:"data_type"` + Cardinality string `json:"cardinality"` + AggregateType string `json:"aggregate_type"` + WriteType string `json:"write_type"` + Properties []interface{} `json:"properties"` + Status string `json:"status"` + UserData struct { + Min int `json:"min"` + Max int `json:"max"` + CreateTime string `json:"~create_time"` + } `json:"user_data"` + } `json:"property_key"` + TaskID int `json:"task_id"` +} + +func (r PropertyKeyUpdateUserdataRequest) Do(ctx context.Context, transport api.Transport) (*PropertyKeyUpdateUserdataResponse, error) { + + params := &url.Values{} + if len(r.reqData.Action) <= 0 { + return nil, errors.New("property update userdata must set action") + } else { + params.Add("action", string(r.reqData.Action)) + } + if len(r.reqData.Name) <= 0 { + return nil, errors.New("property update userdata must set name") + } + + byteBody, err := json.Marshal(&r.reqData) + if err != nil { + return nil, err + } + reader := strings.NewReader(string(byteBody)) + + req, err := api.NewRequest("PUT", fmt.Sprintf("/graphs/%s/schema/propertykeys/%s", transport.GetConfig().Graph, r.reqData.Name), params, reader) + if err != nil { + return nil, err + } + if ctx != nil { + req = req.WithContext(ctx) + } + + res, err := transport.Perform(req) + if err != nil { + return nil, err + } + + resp := &PropertyKeyUpdateUserdataResponse{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + respData := PropertyKeyUpdateUserdataResponseData{} + err = json.Unmarshal(bytes, &respData) + if err != nil { + return nil, err + } + resp.StatusCode = res.StatusCode + resp.Header = res.Header + resp.Body = res.Body + resp.PropertyKeyUpdateUserdata = respData + return resp, nil +} + +func (r PropertyKeyUpdateUserdata) WithReqData(reqData PropertyKeyUpdateUserdataRequestData) func(request *PropertyKeyUpdateUserdataRequest) { + return func(r *PropertyKeyUpdateUserdataRequest) { + r.reqData = reqData + } +} diff --git a/hugegraph-client-go/api/v1/api.propertykey.update_userdata_test.go b/hugegraph-client-go/api/v1/api.propertykey.update_userdata_test.go new file mode 100644 index 00000000..a056a83d --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.update_userdata_test.go @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package v1_test + +import ( + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" + v1 "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/internal/model" + "log" + "testing" +) + +func TestPropertyKeyUpdateUserdataRequest_Do(t *testing.T) { + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + resp, err := client.PropertyKey.PropertyKeyUpdateUserdata( + client.PropertyKey.PropertyKeyUpdateUserdata.WithReqData( + v1.PropertyKeyUpdateUserdataRequestData{ + Action: model.ActionAppend, + Name: "age", + UserData: struct { + Min int `json:"min"` + Max int `json:"max"` + }(struct { + Min int + Max int + }{Min: 0, Max: 101}), + }, + ), + ) + if err != nil { + log.Println(err) + } + + fmt.Println(resp) +} diff --git a/hugegraph-client-go/api/v1/api.schema.go b/hugegraph-client-go/api/v1/api.schema.go index 5ae73d46..f1a95ba0 100644 --- a/hugegraph-client-go/api/v1/api.schema.go +++ b/hugegraph-client-go/api/v1/api.schema.go @@ -120,7 +120,7 @@ type SchemaResponseData struct { func (r SchemaRequest) Do(ctx context.Context, transport api.Transport) (*SchemaResponse, error) { - req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema", transport.GetConfig().Graph), r.Body) + req, err := api.NewRequest("GET", fmt.Sprintf("/graphs/%s/schema", transport.GetConfig().Graph), nil, r.Body) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.veretx.create.go b/hugegraph-client-go/api/v1/api.veretx.create.go index 8e22560d..3fbbd4c8 100644 --- a/hugegraph-client-go/api/v1/api.veretx.create.go +++ b/hugegraph-client-go/api/v1/api.veretx.create.go @@ -79,7 +79,7 @@ func (r CreateRequest) Do(ctx context.Context, transport api.Transport) (*Create url = fmt.Sprintf("/graphs/%s/graph/vertices", config.Graph) } - req, err := api.NewRequest("POST", url, r.Body) + req, err := api.NewRequest("POST", url,nil, r.Body) if err != nil { return nil, err } diff --git a/hugegraph-client-go/api/v1/api.version.go b/hugegraph-client-go/api/v1/api.version.go index e2237025..60507367 100644 --- a/hugegraph-client-go/api/v1/api.version.go +++ b/hugegraph-client-go/api/v1/api.version.go @@ -66,7 +66,7 @@ type VersionResponseData struct { func (r VersionRequest) Do(ctx context.Context, transport api.Transport) (*VersionResponse, error) { - req, err := api.NewRequest("GET", "/versions", r.Body) + req, err := api.NewRequest("GET", "/versions", nil,r.Body) if err != nil { return nil, err }
