forked from apache/incubator-hugegraph-toolchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhugegraph.go
113 lines (103 loc) · 4.02 KB
/
hugegraph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* 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 hugegraph
import (
"errors"
"fmt"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1/edgelabel"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1/gremlin"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1/propertykey"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1/vertex"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/api/v1/vertexlabel"
"net"
"net/http"
"net/url"
"os"
"github.com/apache/incubator-hugegraph-toolchain/hugegraph-client-go/hgtransport"
)
// Config 配置类型
type Config struct {
Host string // hugegraph Server host
Port int // hugegraph Server api port
GraphSpace string // graphSpace is a feature supported feature. if you not set graphSpace, please set empty string
Graph string // this name is configured in the hugegraph
Username string // server username .if you not set username ,please set empty string
Password string // server password .if you not set password ,please set empty string
Transport http.RoundTripper // The HTTP transport object.
Logger hgtransport.Logger // The logger object.
}
type CommonClient struct {
Vertex *vertex.Vertex
Gremlin *gremlin.Gremlin
Propertykey *propertykey.PropertyKey
VertexLabel *vertexlabel.VertexLabel
EdgeLabel *edgelabel.Edgelabel
*v1.APIV1
Transport hgtransport.Interface
Graph string
GraphSpace string
}
func NewDefaultCommonClient() (*CommonClient, error) {
return NewCommonClient(Config{
Host: "127.0.0.1",
Port: 8080,
GraphSpace: "",
Graph: "hugegraph",
Username: "admin",
Password: "pa",
Logger: &hgtransport.ColorLogger{
Output: os.Stdout,
EnableRequestBody: true,
EnableResponseBody: true,
},
})
}
func NewCommonClient(cfg Config) (*CommonClient, error) {
if len(cfg.Host) < 3 {
return nil, errors.New("cannot create client: host length error")
}
address := net.ParseIP(cfg.Host)
if address == nil {
return nil, errors.New("cannot create client: host is format error")
}
if cfg.Port < 1 || cfg.Port > 65535 {
return nil, errors.New("cannot create client: port is error")
}
tp := hgtransport.New(hgtransport.Config{
URL: &url.URL{
Host: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Scheme: "http",
},
Username: cfg.Username,
Password: cfg.Password,
Graph: cfg.Graph,
Transport: cfg.Transport,
Logger: cfg.Logger,
})
return &CommonClient{
Vertex: vertex.New(tp),
Gremlin: gremlin.New(tp),
Propertykey: propertykey.New(tp),
VertexLabel: vertexlabel.New(tp),
EdgeLabel: edgelabel.New(tp),
APIV1: v1.New(tp),
Transport: tp,
Graph: cfg.Graph,
GraphSpace: cfg.GraphSpace,
}, nil
}