Index
- Variables
- type Path
- func PathFromTokens(tokens []string) Path
- func (p Path) Get(index int) string
- func (p Path) GetAfter(token string) string
- func (p Path) GetAfterWithIndex(token string) (string, int)
- func (p Path) GetBefore(token string) string
- func (p Path) GetBeforeWithIndex(token string) (string, int)
- func (p Path) Tokens() []string
- type Query
- type RequestBuilder
- func Request(method, href string) *RequestBuilder
- func (r *RequestBuilder) Async() *async.Future[*ResponseWrapper]
- func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder
- func (r *RequestBuilder) BodyForm(body any) *RequestBuilder
- func (r *RequestBuilder) BodyJson(body any) *RequestBuilder
- func (r *RequestBuilder) BodyText(body string) *RequestBuilder
- func (r *RequestBuilder) Build() *http.Request
- func (r *RequestBuilder) Client(client *http.Client) *RequestBuilder
- func (r *RequestBuilder) Do() *ResponseWrapper
- func (r *RequestBuilder) Form(body any) *RequestBuilder
- func (r *RequestBuilder) Header(key, val string) *RequestBuilder
- func (r *RequestBuilder) HeaderMap(headers map[string]string) *RequestBuilder
- func (r *RequestBuilder) HeaderMapFmt(headers map[string]any) *RequestBuilder
- func (r *RequestBuilder) HeaderValues(headers map[string][]string) *RequestBuilder
- func (r *RequestBuilder) JSON(body any) *RequestBuilder
- func (r *RequestBuilder) Query(key, val string) *RequestBuilder
- func (r *RequestBuilder) QueryMap(values map[string]string) *RequestBuilder
- func (r *RequestBuilder) QueryMapFmt(values map[string]any) *RequestBuilder
- func (r *RequestBuilder) QueryStruct(values any) *RequestBuilder
- func (r *RequestBuilder) QueryValues(values url.Values) *RequestBuilder
- func (r *RequestBuilder) Text(body string) *RequestBuilder
- type ResponseWrapper
- func Response(resp *http.Response, err ...error) *ResponseWrapper
- func (r *ResponseWrapper) Clear() *ResponseWrapper
- func (r *ResponseWrapper) Debug() *ResponseWrapper
- func (r *ResponseWrapper) Error() error
- func (r *ResponseWrapper) Must() *ResponseWrapper
- func (r *ResponseWrapper) Success() *ResponseWrapper
- func (r *ResponseWrapper) Text() string
- func (r *ResponseWrapper) Unmarshal(target any, mime ...string) *ResponseWrapper
Variables
var ErrUnmarshalTarget = errors.New("failed to encode form values to struct, non struct type is given")
type Path
Path type is a wrapper around path string. It provides a few useful extra methods to operate with path.
type Path string
func PathFromTokens
func PathFromTokens(tokens []string) Path
PathFromTokens joins path tokens into a path string and wraps it into Path type.
Usage:
path := httpx.PathFromTokens([]string{"foo", "bar", "baz"})
func (Path) Get
func (p Path) Get(index int) string
Get returns path token at the given index.
Usage:
path := httpx.Path("/foo/bar/baz")
path.Get(1) // string{"bar"}
func (Path) GetAfter
func (p Path) GetAfter(token string) string
GetAfter returns path token located after provided token.
Usage:
path := httpx.Path("/foo/bar/baz")
path.GetAfter("bar") // string{"baz"}
func (Path) GetAfterWithIndex
func (p Path) GetAfterWithIndex(token string) (string, int)
GetAfterWithIndex returns path token and it's index located after provided token.
Usage:
path := httpx.Path("/foo/bar/baz")
path.GetAfterWithIndex("bar") // string{"baz"}, 2
func (Path) GetBefore
func (p Path) GetBefore(token string) string
GetBefore returns path token located before provided token.
Usage:
path := httpx.Path("/foo/bar/baz")
path.GetBefore("baz") // string{"bar"}
func (Path) GetBeforeWithIndex
func (p Path) GetBeforeWithIndex(token string) (string, int)
GetBeforeWithIndex returns path token and it's index located before provided token.
Usage:
path := httpx.Path("/foo/bar/baz")
path.GetBeforeWithIndex("baz") // string{"bar"}, 1
func (Path) Tokens
func (p Path) Tokens() []string
Tokens returns path tokens.
Usage:
path := httpx.Path("/foo/bar/baz")
path.Tokens() // []string{"foo", "bar", "baz"}
type Query
Query type is a wrapper for url.Values. It provides a few useful extra methods to operate with query.
type Query url.Values
func (Query) Unmarshal
func (q Query) Unmarshal(target any) error
Unmarshal helps to parse url.Values into a struct. Slightly modified version of github.com/knadh/querytostruct
Example:
var target struct {
Foo string `query:"foo"`
Bar int `query:"bar"`
}
q, _ := url.ParseQuery("foo=asdqwe&bar=123")
kyoto.Query(q).Unmarshal(&target)
type RequestBuilder
RequestBuilder provides set of chainable functions to build a request and execute it.
type RequestBuilder struct {
// contains filtered or unexported fields
}
func Request
func Request(method, href string) *RequestBuilder
Request initializes a *RequestBuilder with a given required parameters. See RequestBuilder for details.
func (*RequestBuilder) Async
func (r *RequestBuilder) Async() *async.Future[*ResponseWrapper]
Async wraps a request execution (Do) with an async.Future.
func (*RequestBuilder) Body
func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder
Body sets a body as-is.
func (*RequestBuilder) BodyForm
func (r *RequestBuilder) BodyForm(body any) *RequestBuilder
BodyForm transforms given struct into url encoded string, wraps it with an io.Reader and sets as a request body. Also, it sets a "Content-Type: application/x-www-form-urlencoded" header. If body is not serializable with json, it panics.
func (*RequestBuilder) BodyJson
func (r *RequestBuilder) BodyJson(body any) *RequestBuilder
BodyJson transforms given object into json, wraps it with an io.Reader and sets as a request body. Also, it sets a "Content-Type: application/json" header. If body is not serializable with json, it panics.
func (*RequestBuilder) BodyText
func (r *RequestBuilder) BodyText(body string) *RequestBuilder
BodyText wraps a given string body parameter with an io.Reader and sets as a request body. Also, it sets a "Content-Type: text/plain" header.
func (*RequestBuilder) Build
func (r *RequestBuilder) Build() *http.Request
Build composes provided parameters into *http.Request.
func (*RequestBuilder) Client
func (r *RequestBuilder) Client(client *http.Client) *RequestBuilder
Client sets a client, which will be used on request execution (with Do or Async methods).
func (*RequestBuilder) Do
func (r *RequestBuilder) Do() *ResponseWrapper
Do builds an *http.Request and executes it with a provided client. If client wasn't provided, uses http.DefaultClient.
func (*RequestBuilder) Form
func (r *RequestBuilder) Form(body any) *RequestBuilder
Deprecated: use BodyForm instead. Backward compatibility alias.
func (*RequestBuilder) Header
func (r *RequestBuilder) Header(key, val string) *RequestBuilder
Header sets a header with a given parameters.
func (*RequestBuilder) HeaderMap
func (r *RequestBuilder) HeaderMap(headers map[string]string) *RequestBuilder
HeaderMap sets a header values with a given parameters, stored in map.
func (*RequestBuilder) HeaderMapFmt
func (r *RequestBuilder) HeaderMapFmt(headers map[string]any) *RequestBuilder
HeaderMapFmt formats and sets header values with a given parameters, stored in map.
func (*RequestBuilder) HeaderValues
func (r *RequestBuilder) HeaderValues(headers map[string][]string) *RequestBuilder
HeaderValues sets a headers as-is.
func (*RequestBuilder) JSON
func (r *RequestBuilder) JSON(body any) *RequestBuilder
Deprecated: use BodyJson instead. Backward compatibility alias.
func (*RequestBuilder) Query
func (r *RequestBuilder) Query(key, val string) *RequestBuilder
Query sets a query value with a given parameters.
func (*RequestBuilder) QueryMap
func (r *RequestBuilder) QueryMap(values map[string]string) *RequestBuilder
QueryMap sets a query values with a given parameters, stored in map.
func (*RequestBuilder) QueryMapFmt
func (r *RequestBuilder) QueryMapFmt(values map[string]any) *RequestBuilder
QueryMapFmt formats and sets query values with a given parameters, stored in map.
func (*RequestBuilder) QueryStruct
func (r *RequestBuilder) QueryStruct(values any) *RequestBuilder
QueryStruct sets a query values with a given object. It uses conv.Map to extract values, then acts in the same way as QueryMapFmt. If something goes wrong with marshalling, it panics.
func (*RequestBuilder) QueryValues
func (r *RequestBuilder) QueryValues(values url.Values) *RequestBuilder
QueryValues sets a query as-is.
func (*RequestBuilder) Text
func (r *RequestBuilder) Text(body string) *RequestBuilder
Deprecated: use BodyText instead. Backward compatibility alias.
type ResponseWrapper
ResponseWrapper is a wrapper around http.Response. It provides a set of functions for a chained response processing.
type ResponseWrapper struct {
*http.Response
// contains filtered or unexported fields
}
func Response
func Response(resp *http.Response, err ...error) *ResponseWrapper
Response wraps *http.Response with own wrapper, providing extra functions. See httpx.ResponseWrapper for details.
func (*ResponseWrapper) Clear
func (r *ResponseWrapper) Clear() *ResponseWrapper
Clear removes an error from response. Needed for cases when we are acknowledged about it, processed it, and want to proceed with response results.
func (*ResponseWrapper) Debug
func (r *ResponseWrapper) Debug() *ResponseWrapper
Debug prints the response to stdout. If something goes wrong during dump, chain execution will be stopped. Returns wrapper for chaining.
func (*ResponseWrapper) Error
func (r *ResponseWrapper) Error() error
Error is a chain closer. Ensures that there was no errors in processing chain. If not, error is not nil.
func (*ResponseWrapper) Must
func (r *ResponseWrapper) Must() *ResponseWrapper
Must ensures that there was no errors in processing chain. If not, it panics.
func (*ResponseWrapper) Success
func (r *ResponseWrapper) Success() *ResponseWrapper
Success ensures that response code is between 200 and 299. If not, chain execution will be stopped. Returns wrapper for chaining.
func (*ResponseWrapper) Text
func (r *ResponseWrapper) Text() string
Text reads response body as a text.
func (*ResponseWrapper) Unmarshal
func (r *ResponseWrapper) Unmarshal(target any, mime ...string) *ResponseWrapper
Unmarshal detects response type and decodes it into target. Have an optional mime parameter to force response type. If response type is not supported, or there is an error during decoding, chain execution will be stopped. Returns wrapper for chaining.
Generated by gomarkdoc