http

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

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 (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

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.

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