Documentation ¶
Index ¶
- Constants
- type RequestMutation
- func BaseURL(base string) RequestMutation
- func Body(body io.Reader) RequestMutation
- func BodyFromJSON(v interface{}) RequestMutation
- func BodyFromJSONString(s string) RequestMutation
- func Context(ctx context.Context) RequestMutation
- func Header(key, value string) RequestMutation
- func JoinPath(pathString string) RequestMutation
- func Method(method string) RequestMutation
- func Query(key string, values ...string) RequestMutation
- func ResolvePath(path string) RequestMutation
- type RequestMutator
Examples ¶
Constants ¶
const (
// ContentTypeJSON is the default for JSON strings
ContentTypeJSON = "application/json"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RequestMutation ¶
func BaseURL ¶
func BaseURL(base string) RequestMutation
BaseURL sets the URL of the req from a URL string
func Body ¶
func Body(body io.Reader) RequestMutation
Body will correctly take the contents of a Reader and embed it as the body of the request.
func BodyFromJSON ¶
func BodyFromJSON(v interface{}) RequestMutation
BodyFromJSON marshals the interface `v` into JSON for the req
func BodyFromJSONString ¶
func BodyFromJSONString(s string) RequestMutation
BodyFromJSONString uses the string as JSON for the req
func Context ¶
func Context(ctx context.Context) RequestMutation
Context adds a context.Context to the request that can be retrieved in handlers receiving the context.
func JoinPath ¶
func JoinPath(pathString string) RequestMutation
JoinPath takes a relative pathstring and joins it to the end of the current url, does not allow .., preserves tailing /
func Method ¶
func Method(method string) RequestMutation
Method sets the HTTP method of the request, e.g. http.MethodGet ("GET")
func Query ¶
func Query(key string, values ...string) RequestMutation
Query adds a key and potentially a list of value query param pair without applying query escaping
func ResolvePath ¶
func ResolvePath(path string) RequestMutation
ResolvePath attempts to resolve path against the base url, use this when you need to allow merging paths with .. relative joins must not start with / see https://golang.org/pkg/net/url/#URL.ResolveReference for semantics
type RequestMutator ¶
type RequestMutator struct {
// contains filtered or unexported fields
}
Example ¶
type ExampleRequest struct { Version uint64 `json:"number"` Hash string `json:"hash"` } requestData := &ExampleRequest{ Version: 1, Hash: "1c76f1f33f12c14a63026f71c8d17ab2", } rm := NewRequestMutator( BaseURL("https://scottgreenup.com/"), ) BasicAuthMutator := func(username, password string) RequestMutation { return func(req *http.Request) (*http.Request, error) { code := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) req.Header.Add("Authorization", fmt.Sprintf("Basic %s", code)) return req, nil } } req, _ := rm.NewRequest( ResolvePath("/api/example"), Method(http.MethodPost), BasicAuthMutator("MyUsername", "4RuwRmDkLm990qkXMK6obWK88S7pW3K3"), BodyFromJSON(requestData), ) fmt.Println("URL:", req.URL.String()) fmt.Println("Content Length:", req.ContentLength) fmt.Println("Authorization Header:", req.Header.Get("Authorization"))
Output: URL: https://scottgreenup.com/api/example Content Length: 55 Authorization Header: Basic TXlVc2VybmFtZTo0UnV3Um1Ea0xtOTkwcWtYTUs2b2JXSzg4UzdwVzNLMw==
func NewRequestMutator ¶
func NewRequestMutator(commonMutations ...RequestMutation) *RequestMutator
NewRequestMutator manages RequestMutations. It's main purpose is to store a base set of mutations and then apply them to a http.Request (or new http.Request) with extra mutations, always mutating in the order given.
func (*RequestMutator) Mutate ¶
func (rm *RequestMutator) Mutate(req *http.Request, mutations ...RequestMutation) (*http.Request, error)
Mutate will mutate the request with the common mutations and the given mutations, in the order provided.
func (*RequestMutator) NewRequest ¶
func (rm *RequestMutator) NewRequest(mutations ...RequestMutation) (*http.Request, error)
NewRequest will create a blank http.Request and then call Mutate(newrequest, mutations)