restclient

package module
v1.1.1-0...-11ff1ce Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2018 License: Apache-2.0 Imports: 10 Imported by: 9

README

restclient

Example Usage

package main

import (
    "encoding/base64"
    "fmt"
    "bitbucket.org/atlassianlabs/restclient"
)

func main() {
    type ExampleRequest struct {
        Version uint64 `json:"number"`
        Hash    string `json:"hash"`
    }

    requestData := &ExampleRequest{
        Version: 1,
        Hash:    "1c76f1f33f12c14a63026f71c8d17ab2",
    }

    rm := restclient.NewRequestMutator(
        restclient.BaseURL("https://scottgreenup.com/"),
    )

    BasicAuthMutator := func(username, password string) restclient.RequestMutation {
        return func(req *http.Request) error {
            code := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
            req.Header.Add("Authorization", fmt.Sprintf("Basic %s", code))
            return nil
        }
    }

    req, _ := rm.NewRequest(
        restclient.ResolvePath("/api/example"),
        restclient.Method(http.MethodPost),
        restclient.BasicAuthMutator("MyUsername", "4RuwRmDkLm990qkXMK6obWK88S7pW3K3"),
        restclient.BodyFromJSON(requestData),
    )

    fmt.Println("URL:", req.URL.String())
    fmt.Println("Content Length:", req.ContentLength)
    fmt.Println("Authorization Header:", req.Header.Get("Authorization"))
}

Contributing

Pull requests, issues and comments welcome. For pull requests:

  • Add tests for new features and bug fixes
  • Follow the existing style
  • Separate unrelated changes into multiple pull requests

See the existing issues for things to start contributing.

For bigger changes, make sure you start a discussion first by creating an issue and explaining the intended change.

Atlassian requires contributors to sign a Contributor License Agreement, known as a CLA. This serves as a record stating that the contributor is entitled to contribute the code/documentation/translation to the project and is willing to have it used in distributions and derivative works (or is willing to transfer ownership).

Prior to accepting your contributions we ask that you please follow the appropriate link below to digitally sign the CLA. The Corporate CLA is for those who are contributing as a member of an organization and the individual CLA is for those contributing as an individual.

License

Copyright (c) 2018 Atlassian and others. Apache 2.0 licensed, see LICENSE file.

Documentation

Index

Examples

Constants

View Source
const (
	// ContentTypeJSON is the default for JSON strings
	ContentTypeJSON = "application/json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestMutation

type RequestMutation func(req *http.Request) (*http.Request, error)

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 Header(key, value string) RequestMutation

Header adds a header to the request

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)

Jump to

Keyboard shortcuts

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