rest

package
v0.0.0-...-d691e2b Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackendTypeInternal  = "internal"
	BackendTypeK8s       = "k8s"
	BackendTypeBce       = "cloud"
	BackendTypeOpenstack = "openstack"

	HeaderKeyConnection            = "Connection"
	HeaderValueConnectionClose     = "close"
	HeaderValueConnectionKeepAlive = "Keep-Alive"

	HeaderKeyKeepAlive          = "Keep-Alive"
	HeaderValueKeepAliveDefault = "timeout=5, max=100"
)
View Source
const (
	DefaultServerTimeout = 10
)

Variables

View Source
var (
	// DefaultInternalValidAuthTokens do internal auth verification
	DefaultInternalValidAuthTokens = []string{defaultInternalAuthToken}
)

Functions

This section is empty.

Types

type BackoffManager

type BackoffManager interface {
	UpdateBackoff(url *url.URL, err error, responseCode int)
	CalculateBackoff(url *url.URL) time.Duration
	Sleep(d time.Duration)
}

type BceError

type BceError struct {
	Code      string `json:"code"`
	Message   string `json:"message"`
	RequestID string `json:"requestId"`
}

BceError xxx

func (BceError) Error

func (e BceError) Error() string

type ContentConfig

type ContentConfig struct {
	// AcceptContentTypes specifies the types the client will accept and is optional.
	// If not set, ContentType will be used to define the Accept header
	AcceptContentTypes string
	// ContentType specifies the wire format used to communicate with the server.
	// This value will be set as the Accept header on requests made to the server, and
	// as the default content type on any object sent to the server. If not set,
	// "application/json" is used.
	ContentType string

	// BackendType xxx
	BackendType string

	// Connection specifies the Connection header
	Connection string

	// Connection specifies the KeepAlive header
	KeepAlive string

	ClientTimeout time.Duration
}

ContentConfig contains settings that affect how objects are transformed when sent to the server.

type Decoder

type Decoder interface {
	// Decode attempts to deserialize the provided data using either the innate typing of the scheme or the
	// default kind, group, and version provided. It returns a decoded object as well as the kind, group, and
	// version from the serialized data, or an error. If into is non-nil, it will be used as the target type
	// and implementations may choose to use it rather than reallocating an object. However, the object is not
	// guaranteed to be populated. The returned object is not guaranteed to match into. If defaults are
	// provided, they are applied to the data by default. If no defaults or partial defaults are provided, the
	// type of the into may be used to guide conversion decisions.
	Decode(data []byte, into interface{}) error
}

Decoder attempt to load an object from data.

type Encoder

type Encoder interface {
	// Encode writes an object to a stream. Implementations may return errors if the versions are
	// incompatible, or if no conversion is defined.
	Encode(obj interface{}, w io.Writer) error
}

Encoder write objects to a serialized form

type FastClient

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

func NewFastClient

func NewFastClient(host string) *FastClient

func (*FastClient) Do

func (c *FastClient) Do(req *fasthttp.Request) (resp *fasthttp.Response, err error)

type FastHTTPClient

type FastHTTPClient interface {
	Do(req *fasthttp.Request) (resp *fasthttp.Response, err error)
}

FastHTTPClient is an interface for testing a request object.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for testing a request object.

type Interface

type Interface interface {
	Verb(verb string) *Request
	Post() *Request
	Put() *Request
	Patch(pt PatchType) *Request
	Get() *Request
	Delete() *Request
}

Interface xxx

type NeutronError

type NeutronError struct {
	NeutronError NeutronErrorDetail `json:"NeutronError"`
}

NeutronError xxx

func (NeutronError) Error

func (e NeutronError) Error() string

Error xxx

type NeutronErrorDetail

type NeutronErrorDetail struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Detail  string `json:"detail"`
}

NeutronErrorDetail xxx

type NoBackoff

type NoBackoff struct {
}

NoBackoff is a stub implementation, can be used for mocking or else as a default.

func (*NoBackoff) CalculateBackoff

func (n *NoBackoff) CalculateBackoff(url *url.URL) time.Duration

func (*NoBackoff) Sleep

func (n *NoBackoff) Sleep(d time.Duration)

func (*NoBackoff) UpdateBackoff

func (n *NoBackoff) UpdateBackoff(url *url.URL, err error, responseCode int)

type PatchType

type PatchType string

PatchType is the type of constants to support HTTP PATCH utilized by both the client and server that didn't make sense for a whole package to be dedicated to.

const (
	// JSONPatchType defines a JSON document structure for expressing a sequence
	// of operations to apply to a JavaScript Object Notation (JSON) document.
	// See RFC 6902 (https://www.rfc-editor.org/rfc/rfc6902.txt) or
	// Jsonpatch (http://jsonpatch.com/) for more details.
	JSONPatchType PatchType = "application/json-patch+json"

	// MergePatchType defines the JSON merge patch format and processing rules.
	// See RFC 7386 (https://www.rfc-editor.org/rfc/rfc7386.txt)
	MergePatchType PatchType = "application/merge-patch+json"

	// StrategicMergePatchType xxx
	StrategicMergePatchType PatchType = "application/strategic-merge-patch+json"
)

type QueryCriteria

type QueryCriteria interface {
	AddCondition(key, value string) QueryCriteria
	Value() url.Values
	ReadFromRequest(r *http.Request) QueryCriteria
}

func NewQueryCriteria

func NewQueryCriteria() QueryCriteria

type RESTClient

type RESTClient struct {

	// Set specific behavior of the client.  If not set http.DefaultClient will be used.
	Client *http.Client
	// contains filtered or unexported fields
}

RESTClient imposes common API conventions. The baseURL is expected to point to an HTTP or HTTPS path that is the parent of one or more resources. The server should return a decodable API resource object, or an error.FinalError object which contains information about the reason for any failure.

Most consumers should use client.New() to get a Kubernetes API client.

func NewRESTClient

func NewRESTClient(baseURL *url.URL, version string, config ContentConfig, client *http.Client) (*RESTClient, error)

NewRESTClient creates a new RESTClient. This client performs generic REST functions such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and decoding of responses from the server.

func (*RESTClient) APIVersion

func (c *RESTClient) APIVersion() string

APIVersion returns the APIVersion this RESTClient is expected to use.

func (*RESTClient) Delete

func (c *RESTClient) Delete() *Request

Delete begins a DELETE request. Short for c.Verb("DELETE").

func (*RESTClient) Get

func (c *RESTClient) Get() *Request

Get begins a GET request. Short for c.Verb("GET").

func (*RESTClient) Patch

func (c *RESTClient) Patch(pt PatchType) *Request

Patch begins a PATCH request. Short for c.Verb("Patch").

func (*RESTClient) Post

func (c *RESTClient) Post() *Request

Post begins a POST request. Short for c.Verb("POST").

func (*RESTClient) Put

func (c *RESTClient) Put() *Request

Put begins a PUT request. Short for c.Verb("PUT").

func (*RESTClient) Verb

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

Verb begins a request with a verb (GET, POST, PUT, DELETE).

Example usage of RESTClient's request building interface: c, err := rest.NewRESTClient(...) if err != nil { ... } resp, err := c.Verb("GET").

Path("pods").
Timeout(10*time.Second).
Do()

if err != nil { ... } list, ok := resp.(*api.PodList)

type Request

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

Request allows for building up a request to a server in a chained fashion. Any errors are stored until the end of your call, so you only have to check once.

func NewRequest

func NewRequest(client HTTPClient, verb string, baseURL *url.URL, version string, content ContentConfig, backoff BackoffManager, timeout time.Duration) *Request

NewRequest creates a new request helper object for accessing runtime.Objects on a server. TODO: add client-side throttle & backoff like k8s TODO: more serializers

func (*Request) BaseURL

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

func (*Request) Body

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

Body makes the request use obj as the body. Optional. If obj is a string, try to read a file of that name. If obj is a []byte, send it directly. If obj is an io.Reader, use it directly. If obj is a object, marshal it with json, and set Content-Type header. TODO: more serializers

func (*Request) ClientTimeout

func (r *Request) ClientTimeout(d time.Duration) *Request

ClientTimeout

func (*Request) Context

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

Context adds a context to the request. Contexts are only used for timeouts, deadlines, and cancellations.

func (*Request) Criteria

func (r *Request) Criteria(criteria QueryCriteria) *Request

Criteria adds special parameters into this request

func (*Request) Do

func (r *Request) Do() Result

Do formats and executes the request. Returns a Result object for easy response processing.

Error type:

  • If the request can't be constructed, or an error happened earlier while building its arguments: *RequestConstructionError
  • If the server responds with a status: *errors.StatusError or *errors.UnexpectedObjectError
  • http.Client.Do errors are returned directly.

func (*Request) GetParams

func (r *Request) GetParams() url.Values

func (*Request) Header

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

func (*Request) Host

func (r *Request) Host() string

func (*Request) Namespace

func (r *Request) Namespace(namespace string) *Request

Namespace applies the namespace scope to a request (<resource>/[ns/<namespace>/]<name>)

func (*Request) Param

func (r *Request) Param(paramName, s string) *Request

Param creates a query parameter with the given string value.

func (*Request) Resource

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

Resource sets the resource to access (<resource>/[ns/<namespace>/]<name>)

func (*Request) SetHeader

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

func (*Request) SetHost

func (r *Request) SetHost(host string) *Request

func (*Request) Timeout

func (r *Request) Timeout(d time.Duration) *Request

Timeout makes the request use the given duration as a timeout. Sets the "timeout" parameter.

func (*Request) URL

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

URL returns the current working URL.

func (*Request) Verb

func (r *Request) Verb() string

type ResponseWrapper

type ResponseWrapper interface {
	DoRaw() ([]byte, error)
	Stream() (io.ReadCloser, error)
}

ResponseWrapper is an interface for getting a response. The response may be either accessed as a raw data (the whole output is put into memory) or as a stream.

type Result

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

Result contains the result of calling Request.Do().

func (Result) Error

func (r Result) Error() error

Error returns the error executing the request, nil if no error occurred. If the returned object is of type Status and has Status != StatusSuccess, the additional information in Status will be used to enrich the error. See the Request.Do() comment for what errors you might get.

func (Result) GetStatusCode

func (r Result) GetStatusCode() int

func (Result) Header

func (r Result) Header() http.Header

Header returns HTTP Header

func (Result) Into

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

Into stores the result into obj, if possible. If obj is nil it is ignored. If the returned object is of type Status and has .Status != StatusSuccess, the additional information in Status will be used to enrich the error.

func (Result) Raw

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

Raw returns the raw result.

func (*Result) SetError

func (r *Result) SetError(err error)

SetError for UT purpose

func (Result) StatusCode

func (r Result) StatusCode(statusCode *int) Result

StatusCode returns the HTTP status code of the request. (Only valid if no error was returned.)

type Serializer

type Serializer interface {
	Encoder
	Decoder
}

Serializer is the core interface for transforming objects into a serialized format and back. Implementations may choose to perform conversion of the object, but no assumptions should be made.

type StatusError

type StatusError struct {
	ErrStatus metav1.Status
}

StatusError xxx

func (StatusError) Error

func (e StatusError) Error() string

Error implements the Error interface.

Directories

Path Synopsis
serializer

Jump to

Keyboard shortcuts

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