Skip to content

Index

Variables

go
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.

go
type Path string

func PathFromTokens

go
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

go
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

go
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

go
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

go
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

go
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

go
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.

go
type Query url.Values

func (Query) Unmarshal

go
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.

go
type RequestBuilder struct {
    // contains filtered or unexported fields
}

func Request

go
func Request(method, href string) *RequestBuilder

Request initializes a *RequestBuilder with a given required parameters. See RequestBuilder for details.

func (*RequestBuilder) Async

go
func (r *RequestBuilder) Async() *async.Future[*ResponseWrapper]

Async wraps a request execution (Do) with an async.Future.

func (*RequestBuilder) Body

go
func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder

Body sets a body as-is.

func (*RequestBuilder) BodyForm

go
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

go
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

go
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

go
func (r *RequestBuilder) Build() *http.Request

Build composes provided parameters into *http.Request.

func (*RequestBuilder) Client

go
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

go
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

go
func (r *RequestBuilder) Form(body any) *RequestBuilder

Deprecated: use BodyForm instead. Backward compatibility alias.

func (*RequestBuilder) Header

go
func (r *RequestBuilder) Header(key, val string) *RequestBuilder

Header sets a header with a given parameters.

func (*RequestBuilder) HeaderMap

go
func (r *RequestBuilder) HeaderMap(headers map[string]string) *RequestBuilder

HeaderMap sets a header values with a given parameters, stored in map.

func (*RequestBuilder) HeaderMapFmt

go
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

go
func (r *RequestBuilder) HeaderValues(headers map[string][]string) *RequestBuilder

HeaderValues sets a headers as-is.

func (*RequestBuilder) JSON

go
func (r *RequestBuilder) JSON(body any) *RequestBuilder

Deprecated: use BodyJson instead. Backward compatibility alias.

func (*RequestBuilder) Query

go
func (r *RequestBuilder) Query(key, val string) *RequestBuilder

Query sets a query value with a given parameters.

func (*RequestBuilder) QueryMap

go
func (r *RequestBuilder) QueryMap(values map[string]string) *RequestBuilder

QueryMap sets a query values with a given parameters, stored in map.

func (*RequestBuilder) QueryMapFmt

go
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

go
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

go
func (r *RequestBuilder) QueryValues(values url.Values) *RequestBuilder

QueryValues sets a query as-is.

func (*RequestBuilder) Text

go
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.

go
type ResponseWrapper struct {
    *http.Response
    // contains filtered or unexported fields
}

func Response

go
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

go
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

go
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

go
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

go
func (r *ResponseWrapper) Must() *ResponseWrapper

Must ensures that there was no errors in processing chain. If not, it panics.

func (*ResponseWrapper) Success

go
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

go
func (r *ResponseWrapper) Text() string

Text reads response body as a text.

func (*ResponseWrapper) Unmarshal

go
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