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 fe05caf21db5779477c77ef28f18e167010d8fbf Author: izliang <[email protected]> AuthorDate: Wed Nov 1 11:34:55 2023 +0800 add property apis Change-Id: I0ce53e64effc7d2affcac577e9c4d0405129ddf1 --- hugegraph-client-go/api/v1/api._.go | 19 +++ hugegraph-client-go/api/v1/api.gremlin.post.go | 24 +++- .../api/v1/api.gremlin.post_test.go | 17 +++ .../api/v1/api.propertykey.create.go | 126 +++++++++++++++++ .../api/v1/api.propertykey.create_test.go | 61 +++++++++ .../api/v1/api.propertykey.delete_by_name.go | 105 ++++++++++++++ .../v1/api.propertykey.delete_by_name_test.go} | 13 +- .../api/v1/api.propertykey.get_all.go | 101 ++++++++++++++ .../v1/api.propertykey.get_all_test.go} | 24 +++- .../api/v1/api.propertykey.get_by_name.go | 117 ++++++++++++++++ .../v1/api.propertykey.get_by_name_test.go} | 24 +++- hugegraph-client-go/api/v1/api.schema.go | 151 +++++++++++++++++++++ .../model/model.go => api/v1/api.schema_test.go} | 24 +++- hugegraph-client-go/api/v1/api.veretx.create.go | 1 + hugegraph-client-go/api/v1/api.version_test.go | 56 +------- hugegraph-client-go/internal/model/model.go | 10 ++ 16 files changed, 794 insertions(+), 79 deletions(-) diff --git a/hugegraph-client-go/api/v1/api._.go b/hugegraph-client-go/api/v1/api._.go index e509377c..759791a4 100644 --- a/hugegraph-client-go/api/v1/api._.go +++ b/hugegraph-client-go/api/v1/api._.go @@ -24,18 +24,26 @@ import ( // API contains the Hugegraph APIs type APIV1 struct { Version Version + Schema Schema Vertex struct { Create } Gremlin struct { GremlinPost } + PropertyKey struct { + PropertyKeyGetAll + PropertyKeyGetByName + PropertyKeyCreate + PropertyKeyDeleteByName + } } // New creates new API func New(t api.Transport) *APIV1 { return &APIV1{ Version: newVersionFunc(t), + Schema: newSchemaFunc(t), Vertex: struct { Create }{ @@ -46,5 +54,16 @@ func New(t api.Transport) *APIV1 { }{ GremlinPost: newGremlinPostFunc(t), }, + PropertyKey: struct { + PropertyKeyGetAll + PropertyKeyGetByName + PropertyKeyCreate + PropertyKeyDeleteByName + }{ + PropertyKeyGetAll: newPropertyKeyGetAllFunc(t), + PropertyKeyGetByName: newPropertyKeyGetByNameFunc(t), + PropertyKeyCreate: newPropertyKeyCreateFunc(t), + PropertyKeyDeleteByName: newPropertyKeyDeleteByNameFunc(t), + }, } } diff --git a/hugegraph-client-go/api/v1/api.gremlin.post.go b/hugegraph-client-go/api/v1/api.gremlin.post.go index 846ab39f..17ccb6a5 100644 --- a/hugegraph-client-go/api/v1/api.gremlin.post.go +++ b/hugegraph-client-go/api/v1/api.gremlin.post.go @@ -1,3 +1,20 @@ +/* + * 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 ( @@ -83,22 +100,21 @@ func (g GremlinPostRequest) Do(ctx context.Context, transport api.Transport) (*G 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 - + reader := strings.NewReader(string(byteBody)) req, _ := api.NewRequest("POST", "/gremlin", reader) if ctx != nil { diff --git a/hugegraph-client-go/api/v1/api.gremlin.post_test.go b/hugegraph-client-go/api/v1/api.gremlin.post_test.go index eb4c65d4..d67f521c 100644 --- a/hugegraph-client-go/api/v1/api.gremlin.post_test.go +++ b/hugegraph-client-go/api/v1/api.gremlin.post_test.go @@ -1,3 +1,20 @@ +/* + * 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 ( diff --git a/hugegraph-client-go/api/v1/api.propertykey.create.go b/hugegraph-client-go/api/v1/api.propertykey.create.go new file mode 100644 index 00000000..f903e8f8 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.create.go @@ -0,0 +1,126 @@ +/* + * 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, PropertyKeyCreate 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" + "strings" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// Create a PropertyKey of HugeGraph +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/propertykey/#121-create-a-propertykey +func newPropertyKeyCreateFunc(t api.Transport) PropertyKeyCreate { + return func(o ...func(*PropertyKeyCreateRequest)) (*PropertyKeyCreateResponse, error) { + var r = PropertyKeyCreateRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type PropertyKeyCreate func(o ...func(*PropertyKeyCreateRequest)) (*PropertyKeyCreateResponse, error) + +type PropertyKeyCreateRequest struct { + Body io.Reader + ctx context.Context + reqData PropertyKeyCreateRequestData +} + +type PropertyKeyCreateRequestData struct { + Name string `json:"name"` + DataType model.PropertyDataType `json:"data_type"` + Cardinality model.PropertyCardinality `json:"cardinality"` +} + +type PropertyKeyCreateResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + PropertyKeyCreates PropertyKeyCreateResponseData `json:"versions"` +} + +type PropertyKeyCreateResponseData struct { + PropertyKeyCreate string `json:"version"` // hugegraph version + Core string `json:"core"` // hugegraph core version + Gremlin string `json:"gremlin"` // hugegraph gremlin version + API string `json:"api"` // hugegraph api version +} + +func (r PropertyKeyCreateRequest) Do(ctx context.Context, transport api.Transport) (*PropertyKeyCreateResponse, error) { + + if len(r.reqData.Name) <= 0 { + return nil, errors.New("create property must set name") + } + if len(r.reqData.DataType) <= 0 { + return nil, errors.New("create property must set dataType") + } + if len(r.reqData.Cardinality) <= 0 { + return nil, errors.New("create property must set cardinality") + } + + byteBody, err := json.Marshal(&r.reqData) + if err != nil { + return nil, err + } + reader := strings.NewReader(string(byteBody)) + + req, err := api.NewRequest("POST", fmt.Sprintf("/graphs/%s/schema/propertykeys", transport.GetConfig().Graph), 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 + } + + versionResp := &PropertyKeyCreateResponse{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + err = json.Unmarshal(bytes, versionResp) + if err != nil { + return nil, err + } + versionResp.StatusCode = res.StatusCode + versionResp.Header = res.Header + versionResp.Body = res.Body + return versionResp, nil +} + +func (r PropertyKeyCreate) WithReqData(reqData PropertyKeyCreateRequestData) func(request *PropertyKeyCreateRequest) { + return func(r *PropertyKeyCreateRequest) { + r.reqData = reqData + } +} diff --git a/hugegraph-client-go/api/v1/api.propertykey.create_test.go b/hugegraph-client-go/api/v1/api.propertykey.create_test.go new file mode 100644 index 00000000..6b055f34 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.create_test.go @@ -0,0 +1,61 @@ +/* + * 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" + "math/rand" + "testing" + "time" +) + +func TestPropertyKeyCreateRequest_Do(t *testing.T) { + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + rand.Seed(time.Now().UnixNano()) + name := fmt.Sprintf("testProperty%d", rand.Intn(99999)) + resp, err := client.PropertyKey.PropertyKeyCreate( + client.PropertyKey.PropertyKeyCreate.WithReqData( + v1.PropertyKeyCreateRequestData{ + Name: name, + DataType: model.PropertyDataTypeInt, + Cardinality: model.PropertyCardinalitySingle, + }, + ), + ) + if err != nil { + log.Println(err) + } + + fmt.Println(resp) + + // propertyKey delete after create + respDelete, err := client.PropertyKey.PropertyKeyDeleteByName( + client.PropertyKey.PropertyKeyDeleteByName.WithName(name), + ) + if err != nil { + log.Println(err) + } + fmt.Println(respDelete) +} 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 new file mode 100644 index 00000000..d6a86784 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.delete_by_name.go @@ -0,0 +1,105 @@ +/* + * 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, PropertyKeyDeleteByName 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" + "io" + "io/ioutil" + "net/http" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// Delete PropertyKey according to name +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/propertykey/#125-delete-propertykey-according-to-name +func newPropertyKeyDeleteByNameFunc(t api.Transport) PropertyKeyDeleteByName { + return func(o ...func(*PropertyKeyDeleteByNameRequest)) (*PropertyKeyDeleteByNameResponse, error) { + var r = PropertyKeyDeleteByNameRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type PropertyKeyDeleteByName func(o ...func(*PropertyKeyDeleteByNameRequest)) (*PropertyKeyDeleteByNameResponse, error) + +type PropertyKeyDeleteByNameRequest struct { + Body io.Reader + ctx context.Context + name string +} + +type PropertyKeyDeleteByNameResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + PropertyKeyDeleteByNames PropertyKeyDeleteByNameResponseData `json:"versions"` +} + +type PropertyKeyDeleteByNameResponseData struct { + PropertyKeyDeleteByName string `json:"version"` // hugegraph version + Core string `json:"core"` // hugegraph core version + Gremlin string `json:"gremlin"` // hugegraph gremlin version + API string `json:"api"` // hugegraph api version +} + +func (r PropertyKeyDeleteByNameRequest) Do(ctx context.Context, transport api.Transport) (*PropertyKeyDeleteByNameResponse, error) { + + 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) + if err != nil { + return nil, err + } + if ctx != nil { + req = req.WithContext(ctx) + } + + res, err := transport.Perform(req) + if err != nil { + return nil, err + } + + versionResp := &PropertyKeyDeleteByNameResponse{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + err = json.Unmarshal(bytes, versionResp) + if err != nil { + return nil, err + } + versionResp.StatusCode = res.StatusCode + versionResp.Header = res.Header + versionResp.Body = res.Body + return versionResp, nil +} + +func (p PropertyKeyDeleteByName) WithName(name string) func(request *PropertyKeyDeleteByNameRequest) { + return func(r *PropertyKeyDeleteByNameRequest) { + r.name = name + } +} diff --git a/hugegraph-client-go/internal/model/model.go b/hugegraph-client-go/api/v1/api.propertykey.delete_by_name_test.go similarity index 84% copy from hugegraph-client-go/internal/model/model.go copy to hugegraph-client-go/api/v1/api.propertykey.delete_by_name_test.go index 733a466b..c6735076 100644 --- a/hugegraph-client-go/internal/model/model.go +++ b/hugegraph-client-go/api/v1/api.propertykey.delete_by_name_test.go @@ -15,11 +15,12 @@ * under the License. */ -package model +package v1_test -// Vertex models that support generic types -type Vertex[T any] struct { - ID string - Label string - Properties T +import ( + "testing" +) + +func TestPropertyKeyDeleteByNameRequest_Do(t *testing.T) { + // this test in TestPropertyKeyCreateRequest_Do } diff --git a/hugegraph-client-go/api/v1/api.propertykey.get_all.go b/hugegraph-client-go/api/v1/api.propertykey.get_all.go new file mode 100644 index 00000000..4005b355 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.get_all.go @@ -0,0 +1,101 @@ +/* + * 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, PropertyKeyGetAll 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" + "fmt" + "io" + "io/ioutil" + "net/http" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// Get all PropertyKeys of HugeGraph +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/propertykey/#123-get-all-propertykeys +func newPropertyKeyGetAllFunc(t api.Transport) PropertyKeyGetAll { + return func(o ...func(*PropertyKeyGetAllRequest)) (*PropertyKeyGetAllResponse, error) { + var r = PropertyKeyGetAllRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type PropertyKeyGetAll func(o ...func(*PropertyKeyGetAllRequest)) (*PropertyKeyGetAllResponse, error) + +type PropertyKeyGetAllRequest struct { + Body io.Reader + ctx context.Context +} + +type PropertyKeyGetAllResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + PropertyKeyGetAll PropertyKeyGetAllResponseData `json:"-"` +} + +type PropertyKeyGetAllResponseData struct { + Propertykeys []struct { + ID int `json:"id"` + Name string `json:"name"` + DataType string `json:"data_type"` + Cardinality string `json:"cardinality"` + Properties []interface{} `json:"properties"` + UserData struct { + } `json:"user_data"` + } `json:"propertykeys"` +} + +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) + 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 := &PropertyKeyGetAllResponse{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + respData := PropertyKeyGetAllResponseData{} + err = json.Unmarshal(bytes, &respData) + if err != nil { + return nil, err + } + resp.StatusCode = res.StatusCode + resp.Header = res.Header + resp.Body = res.Body + resp.PropertyKeyGetAll = respData + return resp, nil +} diff --git a/hugegraph-client-go/internal/model/model.go b/hugegraph-client-go/api/v1/api.propertykey.get_all_test.go similarity index 64% copy from hugegraph-client-go/internal/model/model.go copy to hugegraph-client-go/api/v1/api.propertykey.get_all_test.go index 733a466b..270c2c4f 100644 --- a/hugegraph-client-go/internal/model/model.go +++ b/hugegraph-client-go/api/v1/api.propertykey.get_all_test.go @@ -15,11 +15,23 @@ * under the License. */ -package model +package v1_test -// Vertex models that support generic types -type Vertex[T any] struct { - ID string - Label string - Properties T +import ( + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" + "log" + "testing" +) + +func TestPropertyKeyGetAllRequest_Do(t *testing.T) { + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + resp, err := client.PropertyKey.PropertyKeyGetAll() + if err != nil { + log.Println(err) + } + fmt.Println(resp.PropertyKeyGetAll) } 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 new file mode 100644 index 00000000..13acf8a0 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.propertykey.get_by_name.go @@ -0,0 +1,117 @@ +/* + * 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, PropertyKeyGetByName 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" + "io" + "io/ioutil" + "net/http" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// Get PropertyKey according to name of HugeGraph +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/propertykey/#124-get-propertykey-according-to-name +func newPropertyKeyGetByNameFunc(t api.Transport) PropertyKeyGetByName { + return func(o ...func(*PropertyKeyGetByNameRequest)) (*PropertyKeyGetByNameResponse, error) { + var r = PropertyKeyGetByNameRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type PropertyKeyGetByName func(o ...func(*PropertyKeyGetByNameRequest)) (*PropertyKeyGetByNameResponse, error) + +type PropertyKeyGetByNameRequest struct { + Body io.Reader + ctx context.Context + name string +} + +type PropertyKeyGetByNameResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + PropertyKeyGetByName PropertyKeyGetByNameResponseData `json:"-"` +} + +type PropertyKeyGetByNameResponseData 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"` +} + +func (r PropertyKeyGetByNameRequest) Do(ctx context.Context, transport api.Transport) (*PropertyKeyGetByNameResponse, error) { + + if len(r.name) <= 0 { + 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) + 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 := &PropertyKeyGetByNameResponse{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + respData := PropertyKeyGetByNameResponseData{} + err = json.Unmarshal(bytes, &respData) + if err != nil { + return nil, err + } + resp.StatusCode = res.StatusCode + resp.Header = res.Header + resp.Body = res.Body + resp.PropertyKeyGetByName = respData + return resp, nil +} + +func (r PropertyKeyGetByName) WithName(name string) func(request *PropertyKeyGetByNameRequest) { + return func(r *PropertyKeyGetByNameRequest) { + r.name = name + } +} diff --git a/hugegraph-client-go/internal/model/model.go b/hugegraph-client-go/api/v1/api.propertykey.get_by_name_test.go similarity index 63% copy from hugegraph-client-go/internal/model/model.go copy to hugegraph-client-go/api/v1/api.propertykey.get_by_name_test.go index 733a466b..a8067598 100644 --- a/hugegraph-client-go/internal/model/model.go +++ b/hugegraph-client-go/api/v1/api.propertykey.get_by_name_test.go @@ -15,11 +15,23 @@ * under the License. */ -package model +package v1_test -// Vertex models that support generic types -type Vertex[T any] struct { - ID string - Label string - Properties T +import ( + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" + "log" + "testing" +) + +func TestPropertyKeyGetByNameRequest_Do(t *testing.T) { + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + _, err = client.PropertyKey.PropertyKeyGetByName( + client.PropertyKey.PropertyKeyGetByName.WithName("price"), + ) + if err != nil { + log.Println(err) + } } diff --git a/hugegraph-client-go/api/v1/api.schema.go b/hugegraph-client-go/api/v1/api.schema.go new file mode 100644 index 00000000..5ae73d46 --- /dev/null +++ b/hugegraph-client-go/api/v1/api.schema.go @@ -0,0 +1,151 @@ +/* + * 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, Schema 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" + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/hgtransport" + "io" + "io/ioutil" + "net/http" + + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" +) + +// ----- API Definition ------------------------------------------------------- +// View Schema information of HugeGraph +// +// See full documentation at https://hugegraph.apache.org/docs/clients/restful-api/schema/#11-schema +func newSchemaFunc(t api.Transport) Schema { + return func(o ...func(*SchemaRequest)) (*SchemaResponse, error) { + var r = SchemaRequest{} + for _, f := range o { + f(&r) + } + return r.Do(r.ctx, t) + } +} + +type Schema func(o ...func(*SchemaRequest)) (*SchemaResponse, error) + +type SchemaRequest struct { + Body io.Reader + ctx context.Context + config hgtransport.Config +} + +type SchemaResponse struct { + StatusCode int `json:"-"` + Header http.Header `json:"-"` + Body io.ReadCloser `json:"-"` + Data SchemaResponseData `json:"-"` +} + +type SchemaResponseData struct { + Propertykeys []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 { + CreateTime string `json:"~create_time"` + } `json:"user_data"` + } `json:"propertykeys"` + Vertexlabels []struct { + ID int `json:"id"` + Name string `json:"name"` + IDStrategy string `json:"id_strategy"` + PrimaryKeys []string `json:"primary_keys"` + NullableKeys []interface{} `json:"nullable_keys"` + IndexLabels []string `json:"index_labels"` + Properties []string `json:"properties"` + Status string `json:"status"` + TTL int `json:"ttl"` + EnableLabelIndex bool `json:"enable_label_index"` + UserData struct { + CreateTime string `json:"~create_time"` + } `json:"user_data"` + } `json:"vertexlabels"` + Edgelabels []struct { + ID int `json:"id"` + Name string `json:"name"` + SourceLabel string `json:"source_label"` + TargetLabel string `json:"target_label"` + Frequency string `json:"frequency"` + SortKeys []interface{} `json:"sort_keys"` + NullableKeys []interface{} `json:"nullable_keys"` + IndexLabels []string `json:"index_labels"` + Properties []string `json:"properties"` + Status string `json:"status"` + TTL int `json:"ttl"` + EnableLabelIndex bool `json:"enable_label_index"` + UserData struct { + CreateTime string `json:"~create_time"` + } `json:"user_data"` + } `json:"edgelabels"` + Indexlabels []struct { + ID int `json:"id"` + Name string `json:"name"` + BaseType string `json:"base_type"` + BaseValue string `json:"base_value"` + IndexType string `json:"index_type"` + Fields []string `json:"fields"` + Status string `json:"status"` + UserData struct { + CreateTime string `json:"~create_time"` + } `json:"user_data"` + } `json:"indexlabels"` +} + +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) + if err != nil { + return nil, err + } + if ctx != nil { + req = req.WithContext(ctx) + } + + res, err := transport.Perform(req) + if err != nil { + return nil, err + } + + schemaRespData := SchemaResponseData{} + bytes, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + err = json.Unmarshal(bytes, &schemaRespData) + if err != nil { + return nil, err + } + schemaResp := &SchemaResponse{} + schemaResp.StatusCode = res.StatusCode + schemaResp.Header = res.Header + schemaResp.Body = res.Body + schemaResp.Data = schemaRespData + return schemaResp, nil +} diff --git a/hugegraph-client-go/internal/model/model.go b/hugegraph-client-go/api/v1/api.schema_test.go similarity index 66% copy from hugegraph-client-go/internal/model/model.go copy to hugegraph-client-go/api/v1/api.schema_test.go index 733a466b..47daab74 100644 --- a/hugegraph-client-go/internal/model/model.go +++ b/hugegraph-client-go/api/v1/api.schema_test.go @@ -15,11 +15,23 @@ * under the License. */ -package model +package v1_test -// Vertex models that support generic types -type Vertex[T any] struct { - ID string - Label string - Properties T +import ( + "fmt" + "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" + "log" + "testing" +) + +func TestSchemaRequest_Do(t *testing.T) { + client, err := hugegraph.NewDefaultCommonClient() + if err != nil { + log.Println(err) + } + schema, err := client.Schema() + if err != nil { + log.Fatalln(err) + } + fmt.Println(schema.Data) } diff --git a/hugegraph-client-go/api/v1/api.veretx.create.go b/hugegraph-client-go/api/v1/api.veretx.create.go index 2211ec76..8e22560d 100644 --- a/hugegraph-client-go/api/v1/api.veretx.create.go +++ b/hugegraph-client-go/api/v1/api.veretx.create.go @@ -14,6 +14,7 @@ * License for the specific language governing permissions and limitations * under the License. */ + package v1 import ( diff --git a/hugegraph-client-go/api/v1/api.version_test.go b/hugegraph-client-go/api/v1/api.version_test.go index 980c5158..cd3e874b 100644 --- a/hugegraph-client-go/api/v1/api.version_test.go +++ b/hugegraph-client-go/api/v1/api.version_test.go @@ -18,14 +18,9 @@ package v1_test import ( - "context" - "io" - "reflect" "testing" hugegraph "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go" - "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api" - v1 "github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1" ) func TestVersionRequest_Do(t *testing.T) { @@ -34,52 +29,11 @@ func TestVersionRequest_Do(t *testing.T) { if err != nil { t.Errorf("NewDefaultCommonClient() error = %v", err) } - ctx := context.Background() - - type fields struct { - Body io.Reader - ctx context.Context - } - type args struct { - ctx context.Context - transport api.Transport - } - tests := []struct { - name string - fields fields - args args - want v1.VersionResponseData - wantErr bool - }{ - - { - name: "test-version", - fields: fields{ - Body: nil, - ctx: ctx, - }, - args: args{ - ctx: ctx, - transport: client.Transport, - }, - want: v1.VersionResponseData{ - Version: "v1", - Core: "1.0.0", - Gremlin: "3.4.3", - API: "0.69.0.0", - }, - }, + resp, err := client.Version() + if err != nil { + t.Errorf(" client.Version() error = %v", err) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := client.Version() - if (err != nil) != tt.wantErr { - t.Errorf("Do() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got.Versions, tt.want) { - t.Errorf("Do() got = %v, want %v", got.Versions, tt.want) - } - }) + if resp.StatusCode != 200 { + t.Errorf(" client.Version() code error = %v", resp.StatusCode) } } diff --git a/hugegraph-client-go/internal/model/model.go b/hugegraph-client-go/internal/model/model.go index 733a466b..c4b3ab8b 100644 --- a/hugegraph-client-go/internal/model/model.go +++ b/hugegraph-client-go/internal/model/model.go @@ -17,6 +17,16 @@ package model +const ( + PropertyDataTypeInt PropertyDataType = "INT" // data_type int + PropertyCardinalitySingle PropertyCardinality = "SINGLE" // cardinality single + ActionAppend Action = "append" // append action +) + +type PropertyDataType string +type PropertyCardinality string +type Action string + // Vertex models that support generic types type Vertex[T any] struct { ID string
