http

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// HeaderContentType is the key of Content-Type header
	HeaderContentType = "Content-Type"
	// HeaderSetCookie is the key of Set-Cookie header
	HeaderSetCookie = "Set-Cookie"
)

Variables

View Source
var (
	// DefaultJsonPairSize is the default size of json.Pair slice.
	DefaultJsonPairSize = 16
)

Functions

func AnnoToMethod

func AnnoToMethod(annoKey string) string

AnnoToMethod maps annotation to corresponding http method

Types

type Endpoint

type Endpoint struct {
	Method, Path string
}

Endpoint a http endpoint.

type HTTPRequest

type HTTPRequest struct {
	*http.Request

	Params  Params
	BodyMap interface{}
	// contains filtered or unexported fields
}

Request is a implementation of RequestGetter. It wraps http.Request.

func NewHTTPRequest

func NewHTTPRequest() *HTTPRequest

NewHTTPRequest creates a new HTTPRequest.

func NewHTTPRequestFromStdReq

func NewHTTPRequestFromStdReq(req *http.Request, params ...Param) (ret *HTTPRequest, err error)

NewHTTPRequestFromStdReq creates a new HTTPRequest from http.Request. It will check the content-type of the request and parse the body if the type one of following:

  • application/json
  • application/x-www-form-urlencoded

func NewHTTPRequestFromUrl

func NewHTTPRequestFromUrl(method, url string, body io.Reader, params ...Param) (*HTTPRequest, error)

NewHTTPRequestFromUrl creates a new HTTPRequest from url, body and url-path param.

func (HTTPRequest) GetBody

func (self HTTPRequest) GetBody() []byte

Body implements RequestGetter.Body.

func (HTTPRequest) GetCookie

func (self HTTPRequest) GetCookie(key string) string

Cookie implements RequestGetter.Cookie.

func (HTTPRequest) GetHeader

func (self HTTPRequest) GetHeader(key string) string

Header implements RequestGetter.Header.

func (HTTPRequest) GetHost

func (self HTTPRequest) GetHost() string

Host implements RequestGetter.Host.

func (*HTTPRequest) GetMapBody

func (self *HTTPRequest) GetMapBody(key string) string

MapBody implements RequestGetter.MapBody.

func (HTTPRequest) GetMethod

func (self HTTPRequest) GetMethod() string

Method implements RequestGetter.Method.

func (HTTPRequest) GetParam

func (self HTTPRequest) GetParam(key string) string

Param implements RequestGetter.Param.

func (HTTPRequest) GetPath

func (self HTTPRequest) GetPath() string

Path implements RequestGetter.Path.

func (HTTPRequest) GetPostForm

func (self HTTPRequest) GetPostForm(key string) string

PostForm implements RequestGetter.PostForm.

func (HTTPRequest) GetQuery

func (self HTTPRequest) GetQuery(key string) string

Query implements RequestGetter.Query.

func (HTTPRequest) GetUri

func (self HTTPRequest) GetUri() string

Uri implements RequestGetter.Uri.

func (*HTTPRequest) String added in v0.3.0

func (h *HTTPRequest) String() string

type HTTPResponse

type HTTPResponse struct {
	*http.Response
}

HTTPResponse is an implementation of ResponseSetter

func NewHTTPResponse

func NewHTTPResponse() *HTTPResponse

NewHTTPResponse creates a new HTTPResponse

func (HTTPResponse) SetCookie

func (self HTTPResponse) SetCookie(key string, val string) error

SetCookie implements ResponseSetter.SetCookie

func (HTTPResponse) SetHeader

func (self HTTPResponse) SetHeader(key string, val string) error

SetHeader implements ResponseSetter.SetHeader

func (HTTPResponse) SetRawBody

func (self HTTPResponse) SetRawBody(body []byte) error

func (HTTPResponse) SetStatusCode

func (self HTTPResponse) SetStatusCode(code int) error

SetStatusCode implements ResponseSetter.SetStatusCode

type Param

type Param struct {
	Key   string
	Value string
}

Param in url path

e.g. /user/:id + /user/123 => Param{Key: "id", Value: "123"}

type Params

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

Http url-path params

func (*Params) ByName

func (ps *Params) ByName(name string) string

ByName search Param by given name

func (*Params) Recycle

func (ps *Params) Recycle()

Recycle the Params

func (*Params) Set

func (ps *Params) Set(name string, val string) bool

Set set Param by given name and value, return true if Param exists

type RequestGetter

type RequestGetter interface {
	// GetMethod returns the http method.
	GetMethod() string
	// GetHost returns the host.
	GetHost() string
	// GetUri returns entire uri.
	GetUri() string
	// Header returns the value of the header with the given key.
	GetHeader(string) string
	// Cookie returns the value of the cookie with the given key.
	GetCookie(string) string
	// Query returns the value of the query with the given key.
	GetQuery(string) string
	// Param returns the value of the url-path param with the given key.
	GetParam(string) string
	// PostForm returns the value of the post-form body with the given key.
	GetPostForm(string) string
	// MapBody returns the value of body with the given key.
	GetMapBody(string) string
	// Body returns the raw body in bytes.
	GetBody() []byte
}

RequestGetter is a interface for getting request parameters

type ResponseSetter

type ResponseSetter interface {
	// SetStatusCode sets the status code of the response
	SetStatusCode(int) error
	// SetHeader sets the header of the response
	SetHeader(string, string) error
	// SetCookie sets the cookie of the response
	SetCookie(string, string) error
	// SetRawBody sets the raw body of the response
	SetRawBody([]byte) error
}

ResponseSetter is a interface for setting response parameters

Jump to

Keyboard shortcuts

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