api

package
v0.0.0-...-ea1d8b2 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2019 License: Apache-2.0 Imports: 18 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeBody

func DecodeBody(resp *http.Response, out interface{}) error

DecodeBody is used to JSON decode a body

func EncodeBody

func EncodeBody(obj interface{}) (io.Reader, error)

EncodeBody is used to encode a request body

func NewHttpClient

func NewHttpClient(transport *http.Transport, tlsConf TLSConfig) (*http.Client, error)

NewHttpClient returns an http client configured with the given Transport and TLS config.

func RequireOK

func RequireOK(d time.Duration, resp *http.Response, e error) (time.Duration, *http.Response, error)

RequireOK is used to wrap DoRequest

func SetupTLSConfig

func SetupTLSConfig(tlsConfig *TLSConfig) (*tls.Config, error)

TLSConfig is used to generate a TLSClientConfig that's useful for talking to Rest using TLS.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides a client to the Rest API

func NewClient

func NewClient(config *Config) (*Client, error)

NewClient returns a new client

func (*Client) DoRequest

func (c *Client) DoRequest(r *Request) (time.Duration, *http.Response, error)

DoRequest does an HTTP request.

Once the crypto mode enabled, the response result will be decrypted and verified signature using crypto libary, then the final result will be return to end client.

func (*Client) GetEnterpriseSignParam

func (c *Client) GetEnterpriseSignParam() (*pki.SignatureParam, error)

NewClient returns a new client

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string) *Request

NewRequest is used to create a new request

type Config

type Config struct {
	// Address is the address of the Rest server
	Address string

	// Scheme is the URI scheme for the Rest server
	Scheme string

	// RouteTag is the route tag used by fabio to discover service
	RouteTag string

	// Transport is the Transport to use for the http client.
	Transport *http.Transport

	// HttpClient is the client to use. Default will be
	// used if not provided.
	HttpClient *http.Client

	// HttpAuth is the auth info to use for http access.
	HttpAuth *HttpBasicAuth

	// Token is used to provide a per-request ACL token
	// which overrides the agent's default token.
	Token string

	// Username is the login username used to get token from Fred service
	Username string

	// Secret is the login password encrypted by utils/AES
	// used to get token from Fred service
	Secret string

	// SecretKey is the secret key used to encrypt/decrypt Secret field
	SecretKey string

	// ApiKey is the access key for ACL access api
	ApiKey string

	// EnterpriseSignParam is enterprise sign parameters
	EnterpriseSignParam *pki.SignatureParam

	// CallbackUrl is used to receive asynchronous event notification
	// which will notify if the request succeeded or failed
	CallbackUrl string

	// TLS config
	TLSConfig TLSConfig

	// CryptoCfg is used to crypto transation between wasabi and client
	CryptoCfg *CryptoConfig

	// CacheClient is used to cache auth token
	CacheClient ICacheClient

	// TrusteeKeyPairEnable is used to set the flag of trust key pair,
	// if you want to trust, set the flag is true.
	TrusteeKeyPairEnable bool
}

Config is used to configure the creation of a client

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration for the client. By default this will pool and reuse idle connections to Rest. If you have a long-lived client object, this is the desired behavior and should make the most efficient use of the connections to Rest. If you don't reuse a client object , which is not recommended, then you may notice idle connections building up over time. To avoid this, use the DefaultNonPooledConfig() instead.

func DefaultNonPooledConfig

func DefaultNonPooledConfig() *Config

DefaultNonPooledConfig returns a default configuration for the client which does not pool connections. This isn't a recommended configuration because it will reconnect to Rest on every request, but this is useful to avoid the accumulation of idle connections if you make many client objects during the lifetime of your application.

type CryptoConfig

type CryptoConfig struct {
	// Enable flag of transation mode
	// true is crypto transation, false is origin transation
	Enable bool

	// CertsStorePath path of certificate file
	CertsStorePath string

	// EncryptType crypto mothed
	// 0: ecc, 1: rsa
	EncryptType crypto.EncryptType

	// SecurityLevel sets the security level
	SecurityLevel int

	// HashAlgorithm hash method
	// default is  "sha256"
	HashAlgorithm string
}

CryptoConfig is used to crypto transation between wasabi and client

type HttpBasicAuth

type HttpBasicAuth struct {
	// Username to use for HTTP Basic Authentication
	Username string

	// Password to use for HTTP Basic Authentication
	Password string
}

HttpBasicAuth is used to authenticate http client with HTTP Basic Authentication

type ICacheClient

type ICacheClient interface {
	HGet(key, field string) ([]byte, error)
	HSet(key, field string, data []byte) error
	HDel(key string, fields ...string) error
}

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request is used to help build up a request

func (*Request) GetHeader

func (r *Request) GetHeader(k string) string

GetHeader is used to get one header value

func (*Request) GetParam

func (r *Request) GetParam(k string) string

GetParam is used to get one query param

func (*Request) SetBody

func (r *Request) SetBody(obj interface{}) error

SetBody is used to set Request body.

The input 'obj' arg can be '[]byte' type binary data, also it can be structure object. When it is the structure object, it will be converted to binary data as JSON using json.Marshal.

Once the crypto mode enabled, the binary data will be signed and encrypted using crypto libary, then the final result set to the request body.

func (*Request) SetHeader

func (r *Request) SetHeader(k, v string)

SetHeader is used to set one header KV pair

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers http.Header)

SeHeaders is used to add multiple header KV pairs

func (*Request) SetParam

func (r *Request) SetParam(k, v string)

SetParam is used to set one query param

func (*Request) SetParams

func (r *Request) SetParams(params url.Values)

SetParams is used to set multiple query params

func (*Request) ToHTTP

func (r *Request) ToHTTP() (*http.Request, error)

ToHTTP is used to convert the Request object to an standard HTTP request object.

type TLSConfig

type TLSConfig struct {
	// Address is the optional address of the Rest server. The port, if any
	// will be removed from here and this will be set to the ServerName of the
	// resulting config.
	Address string

	// CAFile is the optional path to the CA certificate used for Rest
	// communication, defaults to the system bundle if not specified.
	CAFile string

	// CAPath is the optional path to a directory of CA certificates to use for
	// Rest communication, defaults to the system bundle if not specified.
	CAPath string

	// CertFile is the optional path to the certificate for Rest
	// communication. If this is set then you need to also set KeyFile.
	CertFile string

	// KeyFile is the optional path to the private key for Rest communication.
	// If this is set then you need to also set CertFile.
	KeyFile string

	// InsecureSkipVerify if set to true will disable TLS host verification.
	InsecureSkipVerify bool
}

TLSConfig is used to generate a TLSClientConfig that's useful for talking to Rest using TLS.

Jump to

Keyboard shortcuts

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