rest

package
v0.0.0-...-4f9d474 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const UNKNOWN = "unknown"

Variables

View Source
var NameMayNotBe = []string{".", ".."}
View Source
var NameMayNotContain = []string{"/", "%"}

Functions

func DefaultServerURL

func DefaultServerURL(
	host, apiPath string,
	groupVersion scheme.GroupVersion,
	defaultTLS bool,
) (*url.URL, string, error)

func DefaultUserAgent

func DefaultUserAgent() string

func IsValidPathSegmentName

func IsValidPathSegmentName(name string) []string

func LoadTLSFiles

func LoadTLSFiles(c *Config) error

LoadTLSFiles 将数据从 CertFile、KeyFile 和 CAFile 复制到 CertData、KeyData 和 CAFile 中,或返回错误.

func SetSKTDefaults

func SetSKTDefaults(config *Config) error

func TLSConfigFor

func TLSConfigFor(c *Config) (*tls.Config, error)

Types

type ClientContentConfig

type ClientContentConfig struct {
	Username string
	Password string

	SecretID  string
	SecretKey string

	BearerToken     string
	BearerTokenFile string

	TLSClientConfig

	AcceptContentTypes string
	ContentType        string
	GroupVersion       scheme.GroupVersion
	Negotiator         runtime.ClientNegotiator
}

ClientContentConfig 控制 RESTClient 与服务器的通信方式.

func (*ClientContentConfig) HasBasicAuth

func (c *ClientContentConfig) HasBasicAuth() bool

HasBasicAuth 返回配置是否具有 Basic 身份验证.

func (*ClientContentConfig) HasKeyAuth

func (c *ClientContentConfig) HasKeyAuth() bool

HasKeyAuth 返回配置是否具有 secretId/secretKey 身份验证.

func (*ClientContentConfig) HasTokenAuth

func (c *ClientContentConfig) HasTokenAuth() bool

HasTokenAuth 返回配置是否具有 Token 身份验证.

type Config

type Config struct {
	Host    string
	APIPath string
	ContentConfig

	// Server requires Basic authentication
	Username string
	Password string

	SecretID  string
	SecretKey string

	// Server requires Bearer authentication. This client will not attempt to use
	// refresh tokens for an OAuth2 flow.
	// TODO: demonstrate an OAuth2 compatible client.
	BearerToken string

	// Path to a file containing a BearerToken.
	// If set, the contents are periodically read.
	// The last successfully read value takes precedence over BearerToken.
	BearerTokenFile string

	// TLSClientConfig contains settings to enable transport layer security
	TLSClientConfig

	// UserAgent is an optional field that specifies the caller of this request.
	UserAgent string
	// The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.
	Timeout       time.Duration
	MaxRetries    int
	RetryInterval time.Duration
}

Config 包含可以在初始化时传递给 SKT 客户端的通用属性.

type ContentConfig

type ContentConfig struct {
	ServiceName        string
	AcceptContentTypes string
	ContentType        string
	GroupVersion       *scheme.GroupVersion
	Negotiator         runtime.ClientNegotiator
}

type Interface

type Interface interface {
	Verb(verb string) *Request
	Post() *Request
	Put() *Request
	Get() *Request
	Delete() *Request
	APIVersion() scheme.GroupVersion
}

Interface 定义了和 SKT API 的全部交互方法.

type RESTClient

type RESTClient struct {
	Client *gorequest.SuperAgent // Client 是通用的请求对象
	// contains filtered or unexported fields
}

RESTClient 是执行通用 REST 请求的 Client.

func NewRESTClient

func NewRESTClient(baseURL *url.URL, versionedAPIPath string,
	config ClientContentConfig, client *gorequest.SuperAgent,
) (*RESTClient, error)

NewRESTClient 创建一个新的 RESTClient.

func RESTClientFor

func RESTClientFor(config *Config) (*RESTClient, error)

func (*RESTClient) APIVersion

func (c *RESTClient) APIVersion() scheme.GroupVersion

APIVersion 返回当前 RESTClient 期望使用的 API 版本.

func (*RESTClient) Delete

func (c *RESTClient) Delete() *Request

func (*RESTClient) Get

func (c *RESTClient) Get() *Request

func (*RESTClient) Post

func (c *RESTClient) Post() *Request

func (*RESTClient) Put

func (c *RESTClient) Put() *Request

func (*RESTClient) Verb

func (c *RESTClient) Verb(verb string) *Request

type Request

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

func NewRequest

func NewRequest(c *RESTClient) *Request

NewRequest 创建一个新的 Request.

func (*Request) AbsPath

func (r *Request) AbsPath(segments ...string) *Request

AbsPath 用给定的 segments 重写现有路径.

func (*Request) Body

func (r *Request) Body(obj interface{}) *Request

func (*Request) Do

func (r *Request) Do(ctx context.Context) Result

func (*Request) Name

func (r *Request) Name(resourceName string) *Request

func (*Request) Resource

func (r *Request) Resource(resource string) *Request

func (*Request) SetHeader

func (r *Request) SetHeader(key string, values ...string) *Request

SetHeader 为一个 HTTP Request 设置 Header.

func (*Request) URL

func (r *Request) URL() *url.URL

URL 返回当前请求的 URL.

func (*Request) Verb

func (r *Request) Verb(verb string) *Request

Verb 设置 Request 的动作.

type Result

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

Result 包含调用 Request.Do() 的返回结果.

func (Result) Error

func (r Result) Error() error

Error 实现了 error 接口.

func (Result) Into

func (r Result) Into(v interface{}) error

Into 将结果存储到对象 v 中.

func (Result) Raw

func (r Result) Raw() ([]byte, error)

Raw 返回原始结果.

type TLSClientConfig

type TLSClientConfig struct {
	// Server should be accessed without verifying the TLS certificate. For testing only.
	Insecure bool
	// ServerName is passed to the server for SNI and is used in the client to check server
	// ceritificates against. If ServerName is empty, the hostname used to contact the
	// server is used.
	ServerName string

	// Server requires TLS client certificate authentication
	CertFile string
	// Server requires TLS client certificate authentication
	KeyFile string
	// Trusted root certificates for server
	CAFile string

	// CertData holds PEM-encoded bytes (typically read from a client certificate file).
	// CertData takes precedence over CertFile
	CertData []byte
	// KeyData holds PEM-encoded bytes (typically read from a client certificate key file).
	// KeyData takes precedence over KeyFile
	KeyData []byte
	// CAData holds PEM-encoded bytes (typically read from a root certificates bundle).
	// CAData takes precedence over CAFile
	CAData []byte

	// NextProtos is a list of supported application level protocols, in order of preference.
	// Used to populate tls.Config.NextProtos.
	// To indicate to the server http/1.1 is preferred over http/2, set to ["http/1.1", "h2"] (though the server is free
	// to ignore that preference).
	// To use only http/1.1, set to ["http/1.1"].
	NextProtos []string
}

TLSClientConfig 包含启用传输层安全的设置.

func (TLSClientConfig) HasCA

func (c TLSClientConfig) HasCA() bool

HasCA 返回配置是否具有证书颁发机构.

func (TLSClientConfig) HasCertAuth

func (c TLSClientConfig) HasCertAuth() bool

HasCertAuth 返回配置是否具有证书身份验证.

Jump to

Keyboard shortcuts

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