Coder Social home page Coder Social logo

resttransport's Introduction

resttransport

-- import "github.com/paultyng/resttransport"

Package resttransport provides a simple abstraction for REST API's that allows for simpler API implementation.

Build Status

Usage

type Handler

type Handler func(context.Context, RequestResponse) error

Handler represents a func that processes a RequestResponse.

type RequestResponse

type RequestResponse interface {
	RequestHeader() http.Header

	// BindQuery binds a struct to query string variables extracted from the requested URL.
	BindQuery(interface{}) error
	// BindBody binds the HTTP request body using the transports configured marshaling (which should
	// be based on HTTP request content type).
	BindBody(interface{}) error
	// BindPath binds a struct to path variables extracted from the requested URL.
	BindPath(interface{}) error

	// User returns current user state/context (differs based on transport implementations).
	User() interface{}

	// FormFile retrieves a file (by name) from the request
	FormFile(name string) (*multipart.FileHeader, error)

	// Attachment sends `file` with filename `name` and
	// contentType `contentType` for downloading.
	Attachment(file, name, contentType string) error
	// Redirect sends an empty response with the specified status code
	// (e.g. 301 or 302), and the specified "Location" header.
	Redirect(status int, location string) error
	// Body sends a response body with the given status code. The type of marshaling will be decided
	// by the transport.
	Body(status int, body interface{}) error
	// NoBody sends a response with only a status code.
	NoBody(status int) error
}

RequestResponse represents the handlers contract for reading request data and responding. Both BindQuery and BindPath assume a struct (can be anonymous), optionally including struct field tags, to bind to variables extracted from the requested URL. Implementation of the annotations and URL registration is specific to transport implementation. Similar to JSON marshaling, fields must be exported to be bound.

If a handler registered as /foos/{id}/bars has a request URL similar to /foos/1/bars?page=3, this could be bound similar to the following:

func getFooBars(r RequestResponse) error {
	pathParams := struct{ ID string `path:"id"` }{}
	r.BindPath(&pathParams)

	queryParams := struct{ Page string `path:"page"` }{}
	r.BindQuery(&queryParams)

	// use pathParams.ID, queryParams.Page, etc...
}

type Transport

type Transport interface {
	RegisterHandler(httpMethod, path string, consumes []string, h Handler) error
	RegisterAuthenticatedHandler(httpMethod, path string, consumes []string, h Handler) error
}

Transport represents the mapping between an API and the underlying communication infrastructure. Handlers can be registered with or without authentication. URL variable annotations should follow the form /foo/{id} where brackets are used to denote path parameters.

doctransport

-- import "github.com/paultyng/resttransport/doctransport"

Package doctransport observes API registrations and traffic and generates Swagger. Ideally you could use this with unit tests which simulate API traffic.

Usage

type SwaggerTransport

type SwaggerTransport interface {
	resttransport.Transport
	Generate() (*spec.Swagger, error)
}

SwaggerTransport is the interface for a Transport that has Swagger spec generation.

func New

func New(inner resttransport.Transport) SwaggerTransport

New returns an resttransport middleware that records interactions for documenting.

echotransport

-- import "github.com/paultyng/resttransport/echotransport"

Package echotransport wraps Echo for use as a resttransport implementation.

Usage

func New

func New(c *Config) resttransport.Transport

New returns a Transport that wraps an Echo application.

type Config

type Config struct {
	Echo                     EchoOrContext
	AuthenticationMiddleware []echo.MiddlewareFunc
	UserContextKey           string
}

Config holds configuration information for the Echo Transport.

type EchoOrContext

type EchoOrContext interface {
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

EchoOrContext represents an Echo application struct or Context interface.

resttransport's People

Contributors

mforsyth avatar nickhudkins avatar paultyng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

resttransport's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.