restclient

package
v0.0.0-...-a9c371f Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 13 Imported by: 29

Documentation

Index

Examples

Constants

View Source
const Version = "2.8"

Variables

This section is empty.

Functions

func DefaultErrorParser

func DefaultErrorParser(resp *http.Response) error

DefaultErrorParser attempts to parse the response body as a rest.Error. If it cannot do so, return an error containing the entire response body.

Types

type Client

type Client struct {
	// Username for use in HTTP Basic Auth
	ID string
	// Password for use in HTTP Basic Auth
	Token string
	// HTTP Client to use for making requests
	Client *http.Client
	// The base URL for all requests to this API, for example,
	// "https://fax.twilio.com/v1"
	Base string
	// Set UploadType to JSON or FormURLEncoded to control how data is sent to
	// the server. Defaults to FormURLEncoded.
	UploadType UploadType
	// ErrorParser is invoked when the client gets a 400-or-higher status code
	// from the server. Defaults to rest.DefaultErrorParser.
	ErrorParser func(*http.Response) error
	// contains filtered or unexported fields
}

Client is a generic Rest client for making HTTP requests.

Example
package main

import (
	"fmt"

	"github.com/kevinburke/rest/restclient"
)

func main() {
	client := restclient.New("jobs", "secretpassword", "http://ipinfo.io")
	req, _ := client.NewRequest("GET", "/json", nil)
	type resp struct {
		City string `json:"city"`
		Ip   string `json:"ip"`
	}
	var r resp
	client.Do(req, &r)
	fmt.Println(r.Ip)
}
Output:

func New

func New(user, pass, base string) *Client

New returns a new Client with HTTP Basic Auth with the given user and password. Base is the scheme+domain to hit for all requests.

Example
package main

import (
	"fmt"

	"github.com/kevinburke/rest/restclient"
)

func main() {
	client := restclient.New("jobs", "secretpassword", "http://ipinfo.io")
	req, _ := client.NewRequest("GET", "/json", nil)
	type resp struct {
		City string `json:"city"`
		Ip   string `json:"ip"`
	}
	var r resp
	client.Do(req, &r)
	fmt.Println(r.Ip)
}
Output:

func NewBearerClient

func NewBearerClient(token, base string) *Client

NewBearerClient returns a new Client configured to use Bearer authentication.

func (*Client) DialSocket

func (c *Client) DialSocket(socket string, transport *http.Transport)

DialSocket configures c to use the provided socket and http.Transport to dial a Unix socket instead of a TCP port.

If transport is nil, the settings from DefaultTransport are used.

func (*Client) Do

func (c *Client) Do(r *http.Request, v interface{}) error

Do performs the HTTP request. If the HTTP response is in the 2xx range, Unmarshal the response body into v. If the response status code is 400 or above, attempt to Unmarshal the response into an Error. Otherwise return a generic http error.

Example
package main

import (
	"fmt"

	"github.com/kevinburke/rest/restclient"
)

func main() {
	client := restclient.New("jobs", "secretpassword", "http://ipinfo.io")
	req, _ := client.NewRequest("GET", "/json", nil)
	type resp struct {
		City string `json:"city"`
		Ip   string `json:"ip"`
	}
	var r resp
	client.Do(req, &r)
	fmt.Println(r.Ip)
}
Output:

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string, body io.Reader) (*http.Request, error)

NewRequest creates a new Request and sets basic auth based on the client's authentication information.

Example
package main

import (
	"fmt"

	"github.com/kevinburke/rest/restclient"
)

func main() {
	client := restclient.New("jobs", "secretpassword", "http://ipinfo.io")
	req, _ := client.NewRequest("GET", "/json", nil)
	type resp struct {
		City string `json:"city"`
		Ip   string `json:"ip"`
	}
	var r resp
	client.Do(req, &r)
	fmt.Println(r.Ip)
}
Output:

func (*Client) NewRequestWithContext

func (c *Client) NewRequestWithContext(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)

type Transport

type Transport struct {
	// The underlying RoundTripper.
	RoundTripper http.RoundTripper
	// Whether to write the HTTP request and response contents to Output.
	Debug bool
	// If Debug is true, write the HTTP request and response contents here. If
	// Output is nil, os.Stderr will be used.
	Output io.Writer
}

Transport implements HTTP round trips, but adds hooks for debugging the HTTP request.

Example
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/kevinburke/rest/restclient"
)

func main() {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	}))
	defer server.Close()
	b := new(bytes.Buffer)
	client := http.Client{
		Transport: &restclient.Transport{Debug: true, Output: b},
	}
	req, _ := http.NewRequest("GET", server.URL+"/bar", nil)
	client.Do(req)

	// Dump the HTTP request from the buffer, but skip the lines that change.
	scanner := bufio.NewScanner(b)
	for scanner.Scan() {
		text := scanner.Text()
		if strings.HasPrefix(text, "Host:") || strings.HasPrefix(text, "Date:") {
			continue
		}
		fmt.Println(text)
	}
}
Output:

GET /bar HTTP/1.1
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip

HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/plain; charset=utf-8

Hello World
var DefaultTransport *Transport

DefaultTransport is like http.DefaultTransport, but prints the contents of HTTP requests to os.Stderr if the DEBUG_HTTP_TRAFFIC environment variable is set to true.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

type UploadType

type UploadType string
var FormURLEncoded UploadType = "application/x-www-form-urlencoded"

FormURLEncoded specifies you'd like to upload form-urlencoded data.

var JSON UploadType = "application/json"

JSON specifies you'd like to upload JSON data.

Jump to

Keyboard shortcuts

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