Skip to content

Latest commit

 

History

History
352 lines (249 loc) · 8.1 KB

README.md

File metadata and controls

352 lines (249 loc) · 8.1 KB

http

import "github.com/cloudwego/dynamicgo/http"

Index

Constants

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

Variables

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

func AnnoToMethod

func AnnoToMethod(annoKey string) string

AnnoToMethod maps annotation to corresponding http method

type Endpoint

Endpoint a http endpoint.

type Endpoint struct {
    Method, Path string
}

type HTTPRequest

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

type HTTPRequest struct {
    *http.Request

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

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) Body

func (self HTTPRequest) Body() []byte

Body implements RequestGetter.Body.

func (HTTPRequest) Cookie

func (self HTTPRequest) Cookie(key string) string

Cookie implements RequestGetter.Cookie.

func (HTTPRequest) Header

func (self HTTPRequest) Header(key string) string

Header implements RequestGetter.Header.

func (HTTPRequest) Host

func (self HTTPRequest) Host() string

Host implements RequestGetter.Host.

func (*HTTPRequest) MapBody

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

MapBody implements RequestGetter.MapBody.

func (HTTPRequest) Method

func (self HTTPRequest) Method() string

Method implements RequestGetter.Method.

func (HTTPRequest) Param

func (self HTTPRequest) Param(key string) string

Param implements RequestGetter.Param.

func (HTTPRequest) Path

func (self HTTPRequest) Path() string

Path implements RequestGetter.Path.

func (HTTPRequest) PostForm

func (self HTTPRequest) PostForm(key string) string

PostForm implements RequestGetter.PostForm.

func (HTTPRequest) Query

func (self HTTPRequest) Query(key string) string

Query implements RequestGetter.Query.

func (HTTPRequest) Uri

func (self HTTPRequest) Uri() string

Uri implements RequestGetter.Uri.

type HTTPResponse

HTTPResponse is an implementation of ResponseSetter

type HTTPResponse struct {
    *http.Response
}

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

Param in url path

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

type Param struct {
    Key   string
    Value string
}

type Params

Http url-path params

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

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

RequestGetter is a interface for getting request parameters

type RequestGetter interface {
    // Method returns the http method.
    Method() string
    // Host returns the host.
    Host() string
    // Uri returns entire uri.
    Uri() string
    // Header returns the value of the header with the given key.
    Header(string) string
    // Cookie returns the value of the cookie with the given key.
    Cookie(string) string
    // Query returns the value of the query with the given key.
    Query(string) string
    // Param returns the value of the url-path param with the given key.
    Param(string) string
    // PostForm returns the value of the post-form body with the given key.
    PostForm(string) string
    // MapBody returns the value of body with the given key.
    MapBody(string) string
    // Body returns the raw body in bytes.
    Body() []byte
}

type ResponseSetter

ResponseSetter is a interface for setting response parameters

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
}

Generated by gomarkdoc