urlbuilder

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Source

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

Source is used to create URLBuilder instances.

The URLBuilder created using Source will include the base URL and query parameters present on the Source instance.

Ideally, Source instance should be created only once for a given base URL.

func NewSource

func NewSource(baseURL string) (Source, error)

NewSource builds a Source instance by parsing the baseURL.

The baseURL is expected to specify the host. If no scheme is specified, it defaults to http scheme.

func (Source) New

func (b Source) New() *URLBuilder

New creates a new instance of URLBuilder with the base URL and query parameters carried over from Source.

type URLBuilder

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

URLBuilder is used to build a URL.

Source should be used to create an instance of URLBuilder.

b, err := httputil.NewSource("https://api.example.com/")
if err != nil {
	// handle error
}

u := b.New().
	Path("/users/{id}/posts").
	PathParam("id", id).
	QueryParam("limit", limit).
	QueryParam("offset", offset).
	URL()

// { id: 123, limit: 10, offset: 120 }
// https://api.example.com/users/123/posts?limit=10&offset=120

r, err := http.NewRequestWithContext(context.Background(), http.MethodGet, u.String(), nil)
if err != nil {
	// handle error
}

// send HTTP request.

func (*URLBuilder) Path

func (u *URLBuilder) Path(p string) *URLBuilder

Path sets the path template for the URL.

Path parameters of the format "{paramName}" are supported and can be substituted using PathParam*.

func (*URLBuilder) PathParam

func (u *URLBuilder) PathParam(name, value string) *URLBuilder

PathParam sets the path parameter name and value which needs to be substituted in the path template. Substitution happens when the URL is built using URLBuilder.URL()

func (*URLBuilder) PathParamInt

func (u *URLBuilder) PathParamInt(name string, value int64) *URLBuilder

PathParamInt sets the path parameter name and value which needs to be substituted in the path template. Substitution happens when the URL is built using URLBuilder.URL()

func (*URLBuilder) PathParams

func (u *URLBuilder) PathParams(params map[string]string) *URLBuilder

PathParams sets the path parameter names and values which need to be substituted in the path template. Substitution happens when the URL is built using URLBuilder.URL()

func (*URLBuilder) QueryParam

func (u *URLBuilder) QueryParam(key string, values ...string) *URLBuilder

QueryParam sets the query parameter with the given values. If a value was previously set, it is replaced.

func (*URLBuilder) QueryParamBool

func (u *URLBuilder) QueryParamBool(key string, value bool) *URLBuilder

QueryParamBool sets the query parameter with the given value. If a value was previously set, it is replaced.

func (*URLBuilder) QueryParamFloat

func (u *URLBuilder) QueryParamFloat(key string, values ...float64) *URLBuilder

QueryParamFloat sets the query parameter with the given values. If a value was previously set, it is replaced.

func (*URLBuilder) QueryParamInt

func (u *URLBuilder) QueryParamInt(key string, values ...int64) *URLBuilder

QueryParamInt sets the query parameter with the given values. If a value was previously set, it is replaced.

func (*URLBuilder) QueryParams

func (u *URLBuilder) QueryParams(params url.Values) *URLBuilder

QueryParams sets the query parameters. If a value was previously set for any of the given parameters, it is replaced.

func (*URLBuilder) URL

func (u *URLBuilder) URL() *url.URL

URL constructs and returns an instance of URL.

The constructed URL has the complete path and query parameters setup. The path parameters are substituted before being joined with the base URL.

Jump to

Keyboard shortcuts

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