http

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	StatusContinue           = http.StatusContinue
	StatusSwitchingProtocols = http.StatusSwitchingProtocols
	StatusProcessing         = http.StatusProcessing
	StatusEarlyHints         = http.StatusEarlyHints

	StatusOK                   = http.StatusOK
	StatusCreated              = http.StatusCreated
	StatusAccepted             = http.StatusAccepted
	StatusNonAuthoritativeInfo = http.StatusNonAuthoritativeInfo
	StatusNoContent            = http.StatusNoContent
	StatusResetContent         = http.StatusResetContent
	StatusPartialContent       = http.StatusPartialContent
	StatusMultiStatus          = http.StatusMultiStatus
	StatusAlreadyReported      = http.StatusAlreadyReported
	StatusIMUsed               = http.StatusIMUsed

	StatusMultipleChoices   = http.StatusMultipleChoices
	StatusMovedPermanently  = http.StatusMovedPermanently
	StatusFound             = http.StatusFound
	StatusSeeOther          = http.StatusSeeOther
	StatusNotModified       = http.StatusNotModified
	StatusUseProxy          = http.StatusUseProxy
	StatusTemporaryRedirect = http.StatusTemporaryRedirect
	StatusPermanentRedirect = http.StatusPermanentRedirect

	StatusBadRequest                   = http.StatusBadRequest
	StatusUnauthorized                 = http.StatusUnauthorized
	StatusPaymentRequired              = http.StatusPaymentRequired
	StatusForbidden                    = http.StatusForbidden
	StatusNotFound                     = http.StatusNotFound
	StatusMethodNotAllowed             = http.StatusMethodNotAllowed
	StatusNotAcceptable                = http.StatusNotAcceptable
	StatusProxyAuthRequired            = http.StatusProxyAuthRequired
	StatusRequestTimeout               = http.StatusRequestTimeout
	StatusConflict                     = http.StatusConflict
	StatusGone                         = http.StatusGone
	StatusLengthRequired               = http.StatusLengthRequired
	StatusPreconditionFailed           = http.StatusPreconditionFailed
	StatusRequestEntityTooLarge        = http.StatusRequestEntityTooLarge
	StatusRequestURITooLong            = http.StatusRequestURITooLong
	StatusUnsupportedMediaType         = http.StatusUnsupportedMediaType
	StatusRequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable
	StatusExpectationFailed            = http.StatusExpectationFailed
	StatusTeapot                       = http.StatusTeapot
	StatusMisdirectedRequest           = http.StatusMisdirectedRequest
	StatusUnprocessableEntity          = http.StatusUnprocessableEntity
	StatusLocked                       = http.StatusLocked
	StatusFailedDependency             = http.StatusFailedDependency
	StatusTooEarly                     = http.StatusTooEarly
	StatusUpgradeRequired              = http.StatusUpgradeRequired
	StatusPreconditionRequired         = http.StatusPreconditionRequired
	StatusTooManyRequests              = http.StatusTooManyRequests
	StatusRequestHeaderFieldsTooLarge  = http.StatusRequestHeaderFieldsTooLarge
	StatusUnavailableForLegalReasons   = http.StatusUnavailableForLegalReasons

	StatusInternalServerError           = http.StatusInternalServerError
	StatusNotImplemented                = http.StatusNotImplemented
	StatusBadGateway                    = http.StatusBadGateway
	StatusServiceUnavailable            = http.StatusServiceUnavailable
	StatusGatewayTimeout                = http.StatusGatewayTimeout
	StatusHTTPVersionNotSupported       = http.StatusHTTPVersionNotSupported
	StatusVariantAlsoNegotiates         = http.StatusVariantAlsoNegotiates
	StatusInsufficientStorage           = http.StatusInsufficientStorage
	StatusLoopDetected                  = http.StatusLoopDetected
	StatusNotExtended                   = http.StatusNotExtended
	StatusNetworkAuthenticationRequired = http.StatusNetworkAuthenticationRequired
)

Variables

View Source
var (
	ErrOpenFileFailed        = errorf("open file failed")
	ErrTemplateRequireURL    = errorf("template require url")
	ErrTemplateRequireMethod = errorf("template require method")
)
View Source
var (
	ErrResponseBodyHasRead = errorf("http: response body has read")
	ErrUnknownError        = errorf("http: unknown error")
)

Functions

func EnableDebug

func EnableDebug(b bool)

func GetJSON

func GetJSON(path string, result interface{}) error

func GetStream

func GetStream(path string) (io.ReadCloser, error)

func GetText

func GetText(path string) (string, error)

func Params

func Params(r *http.Request) map[string]string

func PatchJSON

func PatchJSON(path string, body interface{}, result interface{}) error

func PostJSON

func PostJSON(path string, body interface{}, result interface{}) error

func PutJSON

func PutJSON(path string, body interface{}, result interface{}) error

func SaveFile

func SaveFile(path string, filename string) error

func SetLogger

func SetLogger(l Logger)

func StatusText

func StatusText(code int) string

Types

type Builder

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

func NewBuilder

func NewBuilder(option ...*Template) *Builder

func (*Builder) Build

func (b *Builder) Build() *Template

func (*Builder) CleanBody

func (b *Builder) CleanBody() *Builder

func (*Builder) CleanHeaders

func (b *Builder) CleanHeaders() *Builder

func (*Builder) CleanQueries

func (b *Builder) CleanQueries() *Builder

func (*Builder) Client

func (b *Builder) Client(client *http.Client) *Builder

func (*Builder) Delete

func (b *Builder) Delete(path string) *Builder

func (*Builder) Do

func (b *Builder) Do(client ...*http.Client) *Response

func (*Builder) Get

func (b *Builder) Get(path string) *Builder

func (*Builder) Header

func (b *Builder) Header(k, v string) *Builder

func (*Builder) Headers

func (b *Builder) Headers(m map[string]string) *Builder

func (*Builder) Options

func (b *Builder) Options(path string) *Builder

func (*Builder) Patch

func (b *Builder) Patch(path string) *Builder

func (*Builder) Post

func (b *Builder) Post(path string) *Builder

func (*Builder) Prefix

func (b *Builder) Prefix(p string) *Builder

func (*Builder) Put

func (b *Builder) Put(path string) *Builder

func (*Builder) Queries

func (b *Builder) Queries(m map[string]string) *Builder

func (*Builder) Query

func (b *Builder) Query(k, v string) *Builder

func (*Builder) Write

func (b *Builder) Write(reader io.Reader) *Builder

func (*Builder) WriteFile

func (b *Builder) WriteFile(filename string) *Builder

func (*Builder) WriteFormFile

func (b *Builder) WriteFormFile(formName string, fileName string) *Builder

func (*Builder) WriteJSON

func (b *Builder) WriteJSON(v interface{}) *Builder

func (*Builder) WriteText

func (b *Builder) WriteText(text string) *Builder

func (*Builder) WriteXML

func (b *Builder) WriteXML(v interface{}) *Builder

type EntryView

type EntryView struct {
	Method  string `json:"method"`
	Pattern string `json:"pattern"`
}

type HandlerFunc

type HandlerFunc func(w *ResponseWriter, r *Request)

func (HandlerFunc) Next

func (h HandlerFunc) Next(w *ResponseWriter, r *Request)

func (HandlerFunc) ServeHTTP

func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

type Middleware

type Middleware = func(HandlerFunc) HandlerFunc

type Request

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

func WrapRequest

func WrapRequest(r *http.Request) *Request

func (*Request) AddCooke

func (r *Request) AddCooke(cookie *http.Cookie)

func (*Request) BasicAuth

func (r *Request) BasicAuth() (username string, password string, ok bool)

func (*Request) BindForm

func (r *Request) BindForm(i interface{}) error

func (*Request) BindJSON

func (r *Request) BindJSON(i interface{}) error

func (*Request) BindPostForm

func (r *Request) BindPostForm(i interface{}) error

func (*Request) BindQuery

func (r *Request) BindQuery(i interface{}) error

func (*Request) BindXML

func (r *Request) BindXML(i interface{}) error

func (*Request) Context

func (r *Request) Context() context.Context

func (*Request) Cookie

func (r *Request) Cookie(key string) (string, error)

func (*Request) CookieExists

func (r *Request) CookieExists(key string) bool

func (*Request) CookieOrDefault

func (r *Request) CookieOrDefault(key string, defaultValue ...string) string

func (*Request) Form

func (r *Request) Form(key string) string

func (*Request) FormExists

func (r *Request) FormExists(key string) bool

func (*Request) Header

func (r *Request) Header(key string) string

func (*Request) HeaderExists

func (r *Request) HeaderExists(key string) bool

func (*Request) HeaderOrDefault

func (r *Request) HeaderOrDefault(key string, value string) string

func (*Request) Method

func (r *Request) Method() string

func (*Request) Param

func (r *Request) Param(key string) string

func (*Request) ParamExists

func (r *Request) ParamExists(key string) bool

func (*Request) ParamOrDefault

func (r *Request) ParamOrDefault(key string, value string) string

func (*Request) Path

func (r *Request) Path() string

func (*Request) PostForm

func (r *Request) PostForm(key string) string

func (*Request) PostFormExists

func (r *Request) PostFormExists(key string) bool

func (*Request) Query

func (r *Request) Query(key string) string

func (*Request) QueryExists

func (r *Request) QueryExists(key string) bool

func (*Request) QueryOrDefault

func (r *Request) QueryOrDefault(key string, value string) string

func (*Request) Raw

func (r *Request) Raw() *http.Request

func (*Request) SetBasicAuth

func (r *Request) SetBasicAuth(username string, password string)

func (*Request) SetContext

func (r *Request) SetContext(ctx context.Context)

func (*Request) SetHeader

func (r *Request) SetHeader(key string, value string)

type Response

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

func Get

func Get(path string) *Response
Example
fmt.Println(Get("https://www.baidu.com").raw.StatusCode)
Output:

200

func Patch

func Patch(path string, reader io.Reader, contentType string) *Response

func Post

func Post(path string, reader io.Reader, contentType string) *Response

func PostFile

func PostFile(path string, fileName string) *Response

func PostFormFile

func PostFormFile(path string, formName string, fileName string) *Response

func Put

func Put(path string, reader io.Reader, contentType string) *Response

func WrapResponse

func WrapResponse(r *http.Response) *Response

func (*Response) Bytes

func (r *Response) Bytes() ([]byte, error)

func (*Response) Dump

func (r *Response) Dump(body bool) ([]byte, error)

func (*Response) JSON

func (r *Response) JSON(value interface{}) error

func (*Response) Pipe

func (r *Response) Pipe(writer io.Writer) error

func (*Response) SaveFile

func (r *Response) SaveFile(filename string) error

func (*Response) StatusCode

func (r *Response) StatusCode() int

func (*Response) Stream

func (r *Response) Stream() (io.ReadCloser, error)

func (*Response) Text

func (r *Response) Text() (string, error)

func (*Response) XML

func (r *Response) XML(value interface{}) error

type ResponseWriter

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

func WrapResponseWriter

func WrapResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) HTML

func (w *ResponseWriter) HTML(statusCode int, content string)

func (*ResponseWriter) Header

func (w *ResponseWriter) Header(key string, value string) *ResponseWriter

func (*ResponseWriter) JSON

func (w *ResponseWriter) JSON(statusCode int, value interface{})

func (*ResponseWriter) Raw

func (*ResponseWriter) StatusCode

func (w *ResponseWriter) StatusCode(statusCode int)

func (*ResponseWriter) Text

func (w *ResponseWriter) Text(statusCode int, text string)

func (*ResponseWriter) XML

func (w *ResponseWriter) XML(statusCode int, value interface{})

type Router

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

func NewRouter

func NewRouter(middlewares ...Middleware) *Router

func (*Router) Any

func (router *Router) Any(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Delete

func (router *Router) Delete(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Get

func (router *Router) Get(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Group

func (router *Router) Group(prefix string, middlewares ...Middleware) *group

func (*Router) Handle

func (router *Router) Handle(method string, path string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) HandleNotFound

func (router *Router) HandleNotFound(h HandlerFunc) *Router

func (*Router) Items

func (router *Router) Items() []EntryView

func (*Router) Options

func (router *Router) Options(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Patch

func (router *Router) Patch(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Post

func (router *Router) Post(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Prefix

func (router *Router) Prefix(p string) *Router

func (*Router) Put

func (router *Router) Put(pattern string, handler HandlerFunc, middlewares ...Middleware) *Router

func (*Router) Run

func (router *Router) Run(addr string) error

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) String

func (router *Router) String() string

func (*Router) Use

func (router *Router) Use(middlewares ...Middleware) *Router

type Template

type Template struct {
	Client  *http.Client
	Prefix  string
	Method  string
	Path    string
	Queries map[string]string
	Headers map[string]string
	Body    io.Reader
}

func (*Template) Do

func (t *Template) Do(client ...*http.Client) *Response

func (*Template) FullURL

func (t *Template) FullURL() string

func (*Template) New

func (t *Template) New() *Builder
Example
t := &Template{
	Prefix: "https://example.com/",
	Queries: map[string]string{
		"foo": "bar",
	},
}
fmt.Println(t.FullURL())
fmt.Println(t.New().Get("/user").Query("id", "1").Queries(map[string]string{"name": "medivh"}).Build().FullURL())
Output:

https://example.com/?foo=bar
https://example.com/user?foo=bar&id=1&name=medivh

Directories

Path Synopsis
internal
middlewares

Jump to

Keyboard shortcuts

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