client

package
v0.0.0-...-e1628ab Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasAuthError

func HasAuthError(errors []ResponseError) bool

Types

type Client

type Client struct {
	URL      string    `yaml:"url"`
	UserInfo *UserInfo `yaml:"user_info"`

	// AllowInsecure set whether to check the server certificates
	AllowInsecure bool

	// Dialer dial websocket connecting to graphql server for subscription.
	// If nil, websocket.DefaultDialer will be used.
	Dialer *websocket.Dialer

	// HTTPClient dial http connecting to graphql server for query.
	// If nil, http.DefaultClient will be used.
	HTTPClient *http.Client

	// If set, mutation will wait task down.
	TaskMonitor TaskMonitor
	// contains filtered or unexported fields
}

func (*Client) Auth

func (c *Client) Auth() (string, error)

Auth send login request to tower, and save token

func (*Client) Query

func (c *Client) Query(req *Request) (*Response, error)

Query send query request to tower

func (*Client) SetToken

func (c *Client) SetToken(token string)

func (*Client) Subscription

func (c *Client) Subscription(req *Request) (respCh <-chan Response, stopWatch func(), err error)

Subscription subscribe change of objects, subscribe will stop when get response error, subscribe also could be stopped by run return function stopWatch().

type ErrorCode

type ErrorCode string
const (
	PermissionDenied      ErrorCode = "PERMISSION_DENIED"
	LoginFailed           ErrorCode = "LOGIN_FAILED"
	UserNotFound          ErrorCode = "USER_NOT_FOUND"
	UserPasswordIncorrect ErrorCode = "USER_PASSWORD_INCORRECT"
	NotMatchUser          ErrorCode = "NOT_MATCH_USER"
	LoadTokenFailed       ErrorCode = "LOAD_TOKEN_FAILED" // #nosec
	WebsocketConnectError ErrorCode = "WEBSOCKET_CONNECT_ERROR"
)

type JSONRequest

type JSONRequest []byte

func (JSONRequest) ContentType

func (r JSONRequest) ContentType() string

func (JSONRequest) GetReader

func (r JSONRequest) GetReader() io.Reader

func (JSONRequest) String

func (r JSONRequest) String() string

type Message

type Message struct {
	ID   string      `json:"id,omitempty"`
	Type MessageType `json:"type"`

	PayLoad json.RawMessage `json:"payload,omitempty"`
}

Message is the request/response type when use the websocket connection

type MessageType

type MessageType string
const (
	ConnectionInitMsg      MessageType = "connection_init"      // Client -> Server
	ConnectionTerminateMsg MessageType = "connection_terminate" // Client -> Server
	StartMsg               MessageType = "start"                // Client -> Server
	StopMsg                MessageType = "stop"                 // Client -> Server
	ConnectionAckMsg       MessageType = "connection_ack"       // Server -> Client
	ConnectionErrorMsg     MessageType = "connection_error"     // Server -> Client
	DataMsg                MessageType = "data"                 // Server -> Client
	ErrorMsg               MessageType = "error"                // Server -> Client
	CompleteMsg            MessageType = "complete"             // Server -> Client
	ConnectionKeepAliveMsg MessageType = "ka"                   // Server -> Client
)

type MultipartWriterRequest

type MultipartWriterRequest struct {
	*multipart.Writer
	// contains filtered or unexported fields
}

MultipartWriterRequest generates multipart messages as RequestInterface, it wraps multipart writer. But differently, you can CreateFormFile with file reader directly.

func NewMultipartWriterRequest

func NewMultipartWriterRequest() *MultipartWriterRequest

NewMultipartWriterRequest returns a new MultipartWriterRequest

func (*MultipartWriterRequest) ContentType

func (m *MultipartWriterRequest) ContentType() string

func (*MultipartWriterRequest) CreateFormFile

func (m *MultipartWriterRequest) CreateFormFile(fieldname, filename string, reader io.Reader) error

func (*MultipartWriterRequest) GetReader

func (m *MultipartWriterRequest) GetReader() io.Reader

func (*MultipartWriterRequest) String

func (m *MultipartWriterRequest) String() string

func (*MultipartWriterRequest) Write

func (m *MultipartWriterRequest) Write(data []byte) (int, error)

type Request

type Request struct {
	Query         string                 `json:"query"`
	OperationName string                 `json:"operationName,omitempty"`
	Variables     map[string]interface{} `json:"variables,omitempty"`
}

type RequestInterface

type RequestInterface interface {
	String() string       // humanreadable string
	GetReader() io.Reader // request body reader
	ContentType() string  // content-type in header
}

RequestInterface provides metadata of a http request

func EncodeRequest

func EncodeRequest(req *Request) (RequestInterface, error)

EncodeRequest encode graphql request to http request. If req contains an upload file, it encode message as multipart/form-data, and the file will not be copied

type Response

type Response struct {
	Data   json.RawMessage `json:"data"`
	Errors []ResponseError `json:"errors,omitempty"`
}

type ResponseError

type ResponseError struct {
	Message string    `json:"message"`
	Code    ErrorCode `json:"code,omitempty"`
}

func (ResponseError) Error

func (e ResponseError) Error() string

func (*ResponseError) UnmarshalJSON

func (e *ResponseError) UnmarshalJSON(data []byte) error

type TaskMonitor

type TaskMonitor interface {
	WaitForTask(ctx context.Context, taskID string) (*schema.Task, error)
}

type Upload

type Upload struct {
	FileName string
	File     io.Reader
}

Upload scalar to add support for GraphQL multipart requests

func (*Upload) MarshalJSON

func (*Upload) MarshalJSON() ([]byte, error)

type UserInfo

type UserInfo struct {
	Username string `json:"username" yaml:"username"`
	Password string `json:"password" yaml:"password"`
	Source   string `json:"source" yaml:"source"`
}

Jump to

Keyboard shortcuts

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