apiutil

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// PDRedirectorHeader is used to mark which PD redirected this request.
	PDRedirectorHeader = "PD-Redirector"
	// PDAllowFollowerHandleHeader is used to mark whether this request is allowed to be handled by the follower PD.
	PDAllowFollowerHandleHeader = "PD-Allow-follower-handle" // #nosec G101
	// XForwardedForHeader is used to mark the client IP.
	XForwardedForHeader = "X-Forwarded-For"
	// XForwardedPortHeader is used to mark the client port.
	XForwardedPortHeader = "X-Forwarded-Port"
	// XRealIPHeader is used to mark the real client IP.
	XRealIPHeader = "X-Real-Ip"
	// XCallerIDHeader is used to mark the caller ID.
	XCallerIDHeader = "X-Caller-ID"
	// XForbiddenForwardToMicroServiceHeader is used to indicate that forwarding the request to a microservice is explicitly disallowed.
	XForbiddenForwardToMicroServiceHeader = "X-Forbidden-Forward-To-MicroService"
	// XForwardedToMicroServiceHeader is used to signal that the request has already been forwarded to a microservice.
	XForwardedToMicroServiceHeader = "X-Forwarded-To-MicroService"
)
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]any, 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]any, 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) (*http.Response, 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 GetCallerIDOnHTTP

func GetCallerIDOnHTTP(r *http.Request) string

GetCallerIDOnHTTP returns caller ID from the request header.

func GetIPPortFromHTTPRequest

func GetIPPortFromHTTPRequest(r *http.Request) (ip, port string)

GetIPPortFromHTTPRequest returns http client host IP and port 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 ParseHexKeys

func ParseHexKeys(format string, keys [][]byte) (decodedBytes [][]byte, err error)

ParseHexKeys decodes hexadecimal src into DecodedLen(len(src)) bytes if the format is "hex".

ParseHexKeys expects that each key contains only hexadecimal characters and each key has even length. If existing one key is malformed, ParseHexKeys returns the original bytes.

func ParseKey

func ParseKey(name string, input map[string]any) ([]byte, string, error)

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

func ParseTime

func ParseTime(t string) (time.Time, error)

ParseTime parses a time string with the format "1694580288" If the string is empty, it returns a zero time.

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 any) 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 any) 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 CallerIDRoundTripper

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

CallerIDRoundTripper is used to add caller ID in the HTTP header.

func NewCallerIDRoundTripper

func NewCallerIDRoundTripper(roundTripper http.RoundTripper, callerID string) *CallerIDRoundTripper

NewCallerIDRoundTripper returns a new `CallerIDRoundTripper`.

func (*CallerIDRoundTripper) RoundTrip

func (rt *CallerIDRoundTripper) 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