restkit

package
v0.0.0-...-c55ebbb Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2016 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 0

README

rest-kit

GoDoc

Simple REST client helper written in Go.

Usage

import (
    "net/url"
    "github.com/pulcy/rest-kit"
)

c := restkit.NewRestClient(baseURL)
var user UserType
q := url.Values{}
q.Set("id", "some-user-id")
if err := c.Request("GET", "/user", q, nil, &user); err != nil {
    panic("GET /user failed")
}
// Modify user ...
if err := c.Request("POST", "/user", nil, user, nil); err != nil {
    panic("POST /user failed")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ForbiddenError          = newErrorResponseWithStatusCodeFunc(http.StatusForbidden)
	InternalServerError     = newErrorResponseWithStatusCodeFunc(http.StatusInternalServerError)
	BadRequestError         = newErrorResponseWithStatusCodeFunc(http.StatusBadRequest)
	NotFoundError           = newErrorResponseWithStatusCodeFunc(http.StatusNotFound)
	PreconditionFailedError = newErrorResponseWithStatusCodeFunc(http.StatusPreconditionFailed)
	UnauthorizedError       = newErrorResponseWithStatusCodeFunc(http.StatusUnauthorized)
)

Functions

func Error

func Error(resp http.ResponseWriter, err error) error

Error sends an error message back to the given response writer.

func Html

func Html(resp http.ResponseWriter, content string, code int) error

Html creates a text/html content-type header, sets the given HTTP status code and writes the given content to the response writer.

func IsErrorResponseWithCode

func IsErrorResponseWithCode(err error, code int) bool

func IsErrorResponseWithCodeFunc

func IsErrorResponseWithCodeFunc(code int) func(error) bool

func IsStatusBadRequest

func IsStatusBadRequest(err error) bool

func IsStatusForbidden

func IsStatusForbidden(err error) bool

func IsStatusInternalServer

func IsStatusInternalServer(err error) bool

func IsStatusNotFound

func IsStatusNotFound(err error) bool

func IsStatusPreconditionFailed

func IsStatusPreconditionFailed(err error) bool

func IsStatusUnauthorizedError

func IsStatusUnauthorizedError(err error) bool

func JSON

func JSON(resp http.ResponseWriter, result interface{}, code int) error

JSON creates a application/json content-type header, sets the given HTTP status code and encodes the given result object to the response writer.

func NewErrorResponse

func NewErrorResponse(message string, code int) error

func Text

func Text(resp http.ResponseWriter, content string, code int) error

Text creates a text/plain content-type header, sets the given HTTP status code and writes the given content to the response writer.

Types

type ErrorResponse

type ErrorResponse struct {
	TheError struct {
		Message string `json:"message,omitempty"`
		Code    int    `json:"code,omitempty"`
	} `json:"error"`
	// contains filtered or unexported fields
}

func NewErrorResponseFromError

func NewErrorResponseFromError(err error) ErrorResponse

NewErrorResponseFromError creates an ErrorResponse from the given error. This ErrorResponse can be sent directly to an HttpResponseWriter.

func (*ErrorResponse) Error

func (er *ErrorResponse) Error() string

func (*ErrorResponse) HTTPStatusCode

func (er *ErrorResponse) HTTPStatusCode() int

HTTPStatusCode returns the status code of the given ErrorResponse if such a status code was set. Otherwise it returns http.StatusBadRequest.

type RestClient

type RestClient struct {
	RequestBuilder func(method, path string, query url.Values, reqBody interface{}) (*http.Request, error)
	ResultParser   func(resp *http.Response, body []byte, result interface{}) error
	ErrorParser    func(resp *http.Response, body []byte) error
	ResponseParser func(resp *http.Response, result interface{}) error
	// contains filtered or unexported fields
}

func NewRestClient

func NewRestClient(baseURL *url.URL) *RestClient

func (*RestClient) DefaultErrorParser

func (c *RestClient) DefaultErrorParser(resp *http.Response, body []byte) error

DefaultErrorParser tries to parse the given response body into an ErrorResponse.

func (*RestClient) DefaultRequestBuilder

func (c *RestClient) DefaultRequestBuilder(method, path string, query url.Values, reqBody interface{}) (*http.Request, error)

DefaultRequestBuilder implements the default RequestBuilder behavior.

func (*RestClient) DefaultResponseParser

func (c *RestClient) DefaultResponseParser(resp *http.Response, result interface{}) error

DefaultResponseParser implements the default ResponseParser behavior. It reads the response body. Then if the status == OK, it tries to parse it into the result. Otherwise if the body is not empty, it tries to parse it into an ErrorResponse.

func (*RestClient) DefaultResultParser

func (c *RestClient) DefaultResultParser(resp *http.Response, body []byte, result interface{}) error

DefaultErrorParser tries to parse the given response body into a the given result object.

func (*RestClient) Request

func (c *RestClient) Request(method, path string, query url.Values, reqBody interface{}, result interface{}) error

Request executes a client request. method: GET|POST|PUT|DELETE|HEAD path: Path relative to the path of the baseURL query: Query string (can be nil) reqBody: Object to marshal into the request body result: Reference to object to unmarshal the response into

Jump to

Keyboard shortcuts

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