reqbuilder

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: MIT Imports: 10 Imported by: 2

README

go-reqbuilder

Test Status codecov

The package provides a flexible and customizable way to build HTTP requests in Go, allowing users to easily construct requests with various methods, headers, bodies, and parameters.

Installation

To use reqbuilder in your Go project, you can install it using go get:

go get github.com/vcraescu/go-reqbuilder

Example

type User struct {
    ID        string `json:"id,omitempty`
    FirstName string `json:"firstName,omitempty"`
    LastName  string `json:"lastName,omitempty"`
}

func main() {
    req, err := reqbuilder.NewBuilder("https://api.example.com").
        WithMethod(http.MethodPost).
        WithPath("/users").
        WithBody(User{
            ID:        "1",
            FirstName: "John",
            LastName:  "Doe",
        }).
        WithHeaders(reqbuilder.JSONAcceptHeader, reqbuilder.JSONContentHeader).
        Build(context.Background())
    if err != nil {
        log.Fatalf(err)
    }

    // ...
}

License

This package is distributed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JSONContentHeader       = ContentHeader("application/json")
	URLEncodedContentHeader = ContentHeader("application/x-www-form-urlencoded")
	JSONAcceptHeader        = Header("Accept", "application/json")
)

Functions

func AuthBearerHeader

func AuthBearerHeader(token string) http.Header

func AuthHeader

func AuthHeader(v string) http.Header

func ContentHeader

func ContentHeader(v string) http.Header
func Header(k, v string) http.Header

Types

type BodyMarshaler

type BodyMarshaler interface {
	Marshal(v any) ([]byte, error)
}

BodyMarshaler is an interface for marshaling request bodies.

type BodyMarshalerFunc

type BodyMarshalerFunc func(v any) ([]byte, error)

BodyMarshalerFunc is a function type that implements the BodyMarshaler interface.

func (BodyMarshalerFunc) Marshal

func (fn BodyMarshalerFunc) Marshal(v any) ([]byte, error)

BodyMarshalerFunc is a function type that implements the BodyMarshaler interface.

type Builder

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

Builder is a struct for configuring and building HTTP requests.

func NewBuilder

func NewBuilder(baseURL string, opts ...BuilderOption) Builder

NewBuilder creates a new instance of Builder with the provided base URL and options.

func (Builder) Build

func (b Builder) Build(ctx context.Context) (*http.Request, error)

Build constructs the HTTP request based on the configured parameters.

func (Builder) WithBody

func (b Builder) WithBody(body any) Builder

WithBody sets the request body.

func (Builder) WithBodyMarshaler

func (b Builder) WithBodyMarshaler(bodyMarshaler BodyMarshaler) Builder

WithBodyMarshaler sets the marshaler for the request body.

func (Builder) WithHeaders

func (b Builder) WithHeaders(headers ...http.Header) Builder

WithHeaders sets the request headers.

func (Builder) WithMethod

func (b Builder) WithMethod(method string) Builder

WithMethod sets the HTTP method for the request.

func (Builder) WithParams

func (b Builder) WithParams(params any) Builder

WithParams sets the URL parameters.

func (Builder) WithPath

func (b Builder) WithPath(path string) Builder

WithPath sets the URL path for the request.

type BuilderOption

type BuilderOption interface {
	// contains filtered or unexported methods
}

func WithBodyMarshaler

func WithBodyMarshaler(marshaler BodyMarshaler) BuilderOption

func WithURLValuesMarshaler

func WithURLValuesMarshaler(marshaler URLValuesMarshaler) BuilderOption

type URLValuesMarshaler

type URLValuesMarshaler interface {
	Marshal(v any) (url.Values, error)
}

URLValuesMarshaler is an interface for marshaling URL parameters.

type URLValuesMarshalerFunc

type URLValuesMarshalerFunc func(v any) (url.Values, error)

URLValuesMarshalerFunc is a function type that implements the URLValuesMarshaler interface.

func (URLValuesMarshalerFunc) Marshal

func (fn URLValuesMarshalerFunc) Marshal(v any) (url.Values, error)

Marshal marshals the given interface into URL values.

Jump to

Keyboard shortcuts

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