wrecker

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2017 License: MIT Imports: 9 Imported by: 17 modules, 42 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Wrecker

Wrecker is a request builder for JSON based APIs, written in Go.

Please note that the API is being actively developed, so please expect breaking changes until we post a 1.0 release. If you would like to use wrecker now, we suggest creating a fork and using that.

Build Status

Fluent Builder

Without digging too far into anything, here is what your requests will look like with Wrecker:

wreckerClient.Get("/users").
    URLParam("id", "1").
    Into(&user).            // Loads the response into the user struct
    Execute()

Usage

Wrecker Instance

To get started with Wrecker, you first have to prepare an instance for yourself:

wreckerClient = &wrecker.Wrecker{
    BaseURL: "http://localhost:5000",
    HttpClient: &http.Client{
        Timeout: 10 * time.Second,
    },
    DefaultContentType: "application/x-www-form-urlencoded",
    RequestInterceptor: func(req *wrecker.Request) error {
        req.URLParam("id", "1")
        return nil
    },
}

There are three values you'll have to set

  • BaseURL: This is the base URL of the API you will be using
  • HttpClient: This is an instance of an http.Client. Feel free to tweak this as much as you would like.
  • DefaultContentType: This is the value of the Content-Type header that will be added to every request. This can be overridden at request level, but for convenience you can set a default.
  • RequestInterceptor: This is an interceptor that can update every request before it gets sent out. This can safely be set to nil if you do not have a need for a request interceptor.

You're only going to have to do this once, but if you still find it's too verbose or you don't need the flexibility you can do this:

wreckerClient = wrecker.New("http://localhost:" + os.Getenv("PORT"))

This way will default the HttpClient + DefaultContentType to the values set in the first example, and RequestInterceptor will be set to nil.

Creating Requests

Let's say that you wanted to hit an endpoint with a response that looked something like this:

{
    "id": 1,
    "user_name": "BrandonRomano",
    "location": "Brooklyn, NY"
}

The first thing you would do is create a model that would represent the response:

type User struct {
    Id       int    `json:"id"`
    UserName string `json:"user_name"`
    Location string `json:"location"`
}

Now that we have our struct and our instance of Wrecker, we're ready to actually create the response.

user := User{}

response, err := wreckerClient.Get("/users").
    URLParam("id", "1").
    Into(&user).
    Execute()

fmt.Println(strconv.Itoa(user.Id)) // Prints 1
fmt.Println(user.UserName) // Prints "BrandonRomano"
fmt.Println(user.Location) // Prints "Brooklyn, NY"

License

Wrecker is licensed under MIT

Documentation

Index

Constants

View Source
const (
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	PATCH  = "PATCH"
	DELETE = "DELETE"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicAuthInfo added in v0.1.0

type BasicAuthInfo struct {
	Username string
	Password string
}

type Request

type Request struct {
	HttpVerb      string
	Endpoint      string
	Response      interface{}
	URLParams     url.Values
	FormParams    url.Values
	HttpBody      interface{}
	Headers       map[string]string
	Cookies       []*http.Cookie
	BasicAuthInfo *BasicAuthInfo
	WreckerClient *Wrecker
}

func (*Request) Body

func (r *Request) Body(body interface{}) *Request

func (*Request) Cookie added in v0.1.5

func (r *Request) Cookie(cookie *http.Cookie) *Request

func (*Request) Execute

func (r *Request) Execute() (*http.Response, error)

func (*Request) FormParam

func (r *Request) FormParam(key, value string) *Request

func (*Request) Header

func (r *Request) Header(key, value string) *Request

func (*Request) Into

func (r *Request) Into(response interface{}) *Request

func (*Request) SetBasicAuth added in v0.1.0

func (r *Request) SetBasicAuth(username, password string) *Request

func (*Request) URL

func (r *Request) URL() string

func (*Request) URLParam

func (r *Request) URLParam(key, value string) *Request

type ResponseError added in v0.1.3

type ResponseError struct {
	StatusCode int
	StatusText string
}

func (ResponseError) Error added in v0.1.3

func (r ResponseError) Error() string

type Wrecker

type Wrecker struct {
	BaseURL            string
	HttpClient         *http.Client
	DefaultContentType string
	RequestInterceptor func(*Request) error
}

func New

func New(baseUrl string) *Wrecker

func (*Wrecker) Delete

func (w *Wrecker) Delete(endpoint string) *Request

func (*Wrecker) Get

func (w *Wrecker) Get(endpoint string) *Request

func (*Wrecker) Patch

func (w *Wrecker) Patch(endpoint string) *Request

func (*Wrecker) Post

func (w *Wrecker) Post(endpoint string) *Request

func (*Wrecker) Put

func (w *Wrecker) Put(endpoint string) *Request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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