gremlin

package module
v0.0.0-...-4855079 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 12 Imported by: 0

README

go-gremlin

Installation

go get github.com/go-gremlin/gremlin

Usage

Import the library eg import "github.com/go-gremlin/gremlin".

	if err := gremlin.NewCluster("ws://dev.local:8182/gremlin", "ws://staging.local:8182/gremlin"); err != nil {
		// handle error
	}

To actually run queries against the database, make sure the package is imported and issue a gremlin query like this:-

	data, err := gremlin.Query(`g.V()`).Exec()
	if err != nil  {
		// handle error
	}

You can also execute a query with bindings like this:-

	data, err := gremlin.Query(`g.V().has("name", userName).valueMap()`).Bindings(gremlin.Bind{"userName": "john"}).Exec()

You can also execute a query with Session, Transaction and Aliases

	aliases := make(map[string]string)
	aliases["g"] = fmt.Sprintf("%s.g", "demo-graph")
	session := "de131c80-84c0-417f-abdf-29ad781a7d04"  //use UUID generator
	data, err := gremlin.Query(`g.V().has("name", userName).valueMap()`).Bindings(gremlin.Bind{"userName": "john"}).Session(session).ManageTransaction(true).SetProcessor("session").Aliases(aliases).Exec()

Authentication

	auth := gremlin.OptAuthUserPass("myusername", "mypass")
	client, err := gremlin.NewClient("ws://remote.example.com:443/gremlin", auth)
	data, err = client.ExecQuery(`g.V()`)
	if err != nil {
		panic(err)
	}
	doStuffWith(data)

Documentation

Index

Constants

View Source
const (
	// StatusSuccess ...
	StatusSuccess = 200
	// StatusNoContent ...
	StatusNoContent = 204
	// StatusPartialContent ...
	StatusPartialContent = 206
	// StatusUnauthorized ...
	StatusUnauthorized = 401
	// StatusAuthenticate ...
	StatusAuthenticate = 407
	// StatusMalformedRequest ...
	StatusMalformedRequest = 498
	// StatusInvalidRequestArguments ...
	StatusInvalidRequestArguments = 499
	// StatusServerError ...
	StatusServerError = 500
	// StatusScriptEvaluationError ...
	StatusScriptEvaluationError = 597
	// StatusServerTimeout ...
	StatusServerTimeout = 598
	// StatusServerSerializationError ...
	StatusServerSerializationError = 599
)

Variables

View Source
var ErrorMsg = map[int]string{
	StatusUnauthorized:             "Unauthorized",
	StatusAuthenticate:             "Authenticate",
	StatusMalformedRequest:         "Malformed Request",
	StatusInvalidRequestArguments:  "Invalid Request Arguments",
	StatusServerError:              "Server Error",
	StatusScriptEvaluationError:    "Script Evaluation Error",
	StatusServerTimeout:            "Server Timeout",
	StatusServerSerializationError: "Server Serialization Error",
}

ErrorMsg ...

Functions

func CreateConnection

func CreateConnection() (conn net.Conn, server *url.URL, err error)

func GraphSONSerializer

func GraphSONSerializer(req *Request) ([]byte, error)

GraphSONSerializer ...

func NewCluster

func NewCluster(s ...string) (err error)

NewCluster ...

Types

type AuthInfo

type AuthInfo struct {
	ChallengeID string
	User        string
	Pass        string
}

AuthInfo includes all info related with SASL authentication with the Gremlin server

func NewAuthInfo

func NewAuthInfo(options ...OptAuth) (*AuthInfo, error)

NewAuthInfo is Constructor for different authentication possibilities

type Bind

type Bind map[string]interface{}

Bind ...

type Client

type Client struct {
	Remote *url.URL
	Ws     *websocket.Conn
	Auth   []OptAuth
}

Client include the necessary info to connect to the server and the underlying socket

func NewClient

func NewClient(urlStr string, options ...OptAuth) (*Client, error)

NewClient ...

func (*Client) Authenticate

func (c *Client) Authenticate(requestID string) ([]byte, error)

Authenticate ...

func (*Client) Exec

func (c *Client) Exec(req *Request) ([]byte, error)

Exec ...

func (*Client) ExecQuery

func (c *Client) ExecQuery(query string) ([]byte, error)

ExecQuery executes the provided request

func (*Client) ReadResponse

func (c *Client) ReadResponse() ([]byte, error)

ReadResponse ...

type FormattedReq

type FormattedReq struct {
	Op        string       `json:"op"`
	RequestID interface{}  `json:"requestId"`
	Args      *RequestArgs `json:"args"`
	Processor string       `json:"processor"`
}

FormattedReq the requests in the appropriate way

func NewFormattedReq

func NewFormattedReq(req *Request) FormattedReq

NewFormattedReq ...

type OptAuth

type OptAuth func(*AuthInfo) error

OptAuth ...

func OptAuthEnv

func OptAuthEnv() OptAuth

OptAuthEnv is Sets authentication info from environment variables GREMLIN_USER and GREMLIN_PASS

func OptAuthUserPass

func OptAuthUserPass(user, pass string) OptAuth

OptAuthUserPass ...

type Request

type Request struct {
	RequestID string       `json:"requestId"`
	Op        string       `json:"op"`
	Processor string       `json:"processor"`
	Args      *RequestArgs `json:"args"`
}

Request ...

func BuildQuery

func BuildQuery(query string) (*Request, error)

BuildQuery ...

func (*Request) Aliases

func (req *Request) Aliases(aliases map[string]string) *Request

Aliases is args setter

func (*Request) Bindings

func (req *Request) Bindings(bindings Bind) *Request

Bindings is args setter

func (*Request) ManageTransaction

func (req *Request) ManageTransaction(flag bool) *Request

ManageTransaction is args setter

func (*Request) Session

func (req *Request) Session(session string) *Request

Session is args setter

func (*Request) SetProcessor

func (req *Request) SetProcessor(processor string) *Request

SetProcessor is args setter

type RequestArgs

type RequestArgs struct {
	Gremlin           string            `json:"gremlin,omitempty"`
	Session           string            `json:"session,omitempty"`
	Bindings          Bind              `json:"bindings,omitempty"`
	Language          string            `json:"language,omitempty"`
	Rebindings        Bind              `json:"rebindings,omitempty"`
	Sasl              string            `json:"sasl,omitempty"`
	BatchSize         int               `json:"batchSize,omitempty"`
	ManageTransaction bool              `json:"manageTransaction,omitempty"`
	Aliases           map[string]string `json:"aliases,omitempty"`
}

RequestArgs ...

type Response

type Response struct {
	RequestID string          `json:"requestId"`
	Status    *ResponseStatus `json:"status"`
	Result    *ResponseResult `json:"result"`
}

Response ...

func (Response) String

func (r Response) String() string

Implementation of the stringer interface. Useful for exploration

type ResponseResult

type ResponseResult struct {
	Data json.RawMessage        `json:"data"`
	Meta map[string]interface{} `json:"meta"`
}

ResponseResult ...

type ResponseStatus

type ResponseStatus struct {
	Code       int                    `json:"code"`
	Attributes map[string]interface{} `json:"attributes"`
	Message    string                 `json:"message"`
}

ResponseStatus ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL