apiutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrRedirectFailed is the error message for redirect failed.
	ErrRedirectFailed = "redirect failed"
	// ErrRedirectToNotLeader is the error message for redirect to not leader.
	ErrRedirectToNotLeader = "redirect to not leader"
)
View Source
const (
	// CorePath the core group, is at REST path `/pd/api/v1`.
	CorePath = "/pd/api/v1"
	// ExtensionsPath the named groups are REST at `/pd/apis/{GROUP_NAME}/{Version}`.
	ExtensionsPath = "/pd/apis"
)

Variables

This section is empty.

Functions

func CollectEscapeStringOption

func CollectEscapeStringOption(option string, input map[string]interface{}, collectors ...func(v string)) error

CollectEscapeStringOption is used to collect string using escaping from input map for given option

func CollectStringOption

func CollectStringOption(option string, input map[string]interface{}, collectors ...func(v string)) error

CollectStringOption is used to collect string using from input map for given option

func DeferClose

func DeferClose(c io.Closer, err *error)

DeferClose captures the error returned from closing (if an error occurs). This is designed to be used in a defer statement.

func DoDelete

func DoDelete(client *http.Client, url string) (int, error)

DoDelete is used to send delete request and return http response code.

func ErrorResp

func ErrorResp(rd *render.Render, w http.ResponseWriter, err error)

ErrorResp Respond to the client about the given error, integrating with errcode.ErrorCode.

Important: if the `err` is just an error and not an errcode.ErrorCode (given by errors.Cause), then by default an error is assumed to be a 500 Internal Error.

If the error is nil, this also responds with a 500 and logs at the error level.

func GetComponentNameOnHTTP

func GetComponentNameOnHTTP(r *http.Request) string

GetComponentNameOnHTTP returns component name from Request Header

func GetIPAddrFromHTTPRequest

func GetIPAddrFromHTTPRequest(r *http.Request) string

GetIPAddrFromHTTPRequest returns http client IP from context. Because `X-Forwarded-For ` header has been written into RFC 7239(Forwarded HTTP Extension), so `X-Forwarded-For` has the higher priority than `X-Real-IP`. And both of them have the higher priority than `RemoteAddr`

func GetJSON

func GetJSON(client *http.Client, url string, data []byte) (*http.Response, error)

GetJSON is used to send GET request to specific url

func GetRouteName

func GetRouteName(req *http.Request) string

GetRouteName return mux route name registered

func NewCustomReverseProxies

func NewCustomReverseProxies(dialClient *http.Client, urls []url.URL) http.Handler

NewCustomReverseProxies returns the custom reverse proxies.

func ParseKey

func ParseKey(name string, input map[string]interface{}) ([]byte, string, error)

ParseKey is used to parse interface into []byte and string

func PatchJSON

func PatchJSON(client *http.Client, url string, data []byte) (*http.Response, error)

PatchJSON is used to do patch request

func PostJSON

func PostJSON(client *http.Client, url string, data []byte) (*http.Response, error)

PostJSON is used to send the POST request to a specific URL

func PostJSONIgnoreResp

func PostJSONIgnoreResp(client *http.Client, url string, data []byte) error

PostJSONIgnoreResp is used to do post request with JSON body and ignore response.

func ReadJSON

func ReadJSON(r io.ReadCloser, data interface{}) error

ReadJSON reads a JSON data from r and then closes it. An error due to invalid json will be returned as a JSONError

func ReadJSONRespondError

func ReadJSONRespondError(rd *render.Render, w http.ResponseWriter, body io.ReadCloser, data interface{}) error

ReadJSONRespondError writes json into data. On error respond with a 400 Bad Request

func RegisterUserDefinedHandlers

func RegisterUserDefinedHandlers(registerMap map[string]http.Handler, group *APIServiceGroup, handler http.Handler) error

RegisterUserDefinedHandlers register the user defined handlers.

func TagJSONError

func TagJSONError(err error) error

TagJSONError wraps the JSON error to one type.

Types

type APIServiceGroup

type APIServiceGroup struct {
	Name       string
	Version    string
	IsCore     bool
	PathPrefix string
}

APIServiceGroup used to register the HTTP REST API.

func (*APIServiceGroup) Path

func (sg *APIServiceGroup) Path() string

Path returns the path of the service.

type AccessPath

type AccessPath struct {
	Path   string
	Method string
}

AccessPath is used to identify HTTP api access path including path and method

func NewAccessPath

func NewAccessPath(path, method string) AccessPath

NewAccessPath returns an AccessPath

type ComponentSignatureRoundTripper

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

ComponentSignatureRoundTripper is used to add component signature in HTTP header

func NewComponentSignatureRoundTripper

func NewComponentSignatureRoundTripper(roundTripper http.RoundTripper, componentName string) *ComponentSignatureRoundTripper

NewComponentSignatureRoundTripper returns a new ComponentSignatureRoundTripper.

func (*ComponentSignatureRoundTripper) RoundTrip

func (rt *ComponentSignatureRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip is used to implement RoundTripper

type FieldError

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

FieldError connects an error to a particular field

func ParseUint64VarsField

func ParseUint64VarsField(vars map[string]string, varName string) (uint64, *FieldError)

ParseUint64VarsField connects strconv.ParseUint with request variables It hardcodes the base to 10 and bit size to 64 Any error returned will connect the requested field to the error via FieldError

type JSONError

type JSONError struct {
	Err error
}

JSONError lets callers check for just one error type

func (JSONError) Error

func (e JSONError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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