Skip to content

broothie/qst

Repository files navigation

qst

Go Reference Go Report Card codecov gosec

qst is an *http.Request builder. "qst" is short for "quest", which is part of the word "request".

Installation

go get github.com/broothie/qst

Documentation

Detailed documentation can be found at pkg.go.dev.

A list of all available options can be found here.

Usage

qst uses an options pattern to build *http.Request objects:

request, err := qst.NewPatch("https://breakfast.com/api", // New PATCH request
    qst.BearerAuth("c0rNfl@k3s"),                         // Authorization header
    qst.Path("/cereals", cerealID),                       // Query param
    qst.BodyJSON(map[string]string{"name": "Life"}),      // JSON body
)

It can also be used to fire requests:

response, err := qst.Patch("https://breakfast.com/api", // Send PATCH request
    qst.BearerAuth("c0rNfl@k3s"),                       // Authorization header
    qst.Path("/cereals", cerealID),                     // Query param
    qst.BodyJSON(map[string]string{"name": "Life"}),    // JSON body
)

The options pattern makes it easy to define custom options:

func createdSinceYesterday() qst.Option {
    yesterday := time.Now().Add(-24 * time.Hour)
    return qst.Query("created_at", fmt.Sprintf(">%s", yesterday.Format(time.RFC3339)))
}

func main() {
    response, err := qst.Get("https://breakfast.com/api",
        qst.BearerAuth("c0rNfl@k3s"),
        qst.Path("/cereals"),
        createdSinceYesterday(),
    )
}

qst.Client

This package also includes a Client, which can be outfitted with a set of default options:

client := qst.NewClient(http.DefaultClient,
    qst.URL("https://breakfast.com/api"),
    qst.BearerAuth("c0rNfl@k3s"), 
)

response, err := client.Patch(
    // qst.URL("https://breakfast.com/api"), // Not necessary, included via client
    // qst.BearerAuth("c0rNfl@k3s"),         // Not necessary, included via client
    qst.Path("/cereals", cerealID),
    qst.BodyJSON(map[string]interface{}{
        "name": "Golden Grahams",
    }),
)

qst.OptionFunc

OptionFunc can be used to add a custom function which is run during request creation:

client := qst.NewClient(http.DefaultClient,
    qst.OptionFunc(func(request *http.Request) (*http.Request, error) {
      token := dynamicallyGetBearerTokenSomehow()
      return qst.BearerAuth(token).Apply(request)
    }),
)