rest

package module
v0.0.0-...-87f56a9 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2012 License: MIT Imports: 9 Imported by: 0

README

Rest2go

A RESTful HTTP client and server.

Installation

Install by running:

go get github.com/Kissaki/rest2go

Usage

See the examples folder for coded examples.

rest.go uses the standard http package to provide resources through specified resource routes. Add a new route and corresponding resource using the Resource method:

rest.Resource("resourcepath", resourcevariable)

The resource URI-path is then /resourcepath/.

A resource is an object that may have any of the following methods which respond to the specified HTTP requests:

GET /resource/       => Index(http.ResponseWriter)
GET /resource/id     => Find(http.ResponseWriter, id string)
POST /resource/      => Create(http.ResponseWriter, *http.Request)
PUT /resource/       => Update(http.ResponseWriter, id string, *http.Request)
PUT /resource/id     => Update(http.ResponseWriter, id string, *http.Request)
DELETE /resource/    => Delete(http.ResponseWriter, id string)
DELETE /resource/id  => Delete(http.ResponseWriter, id string)
OPTIONS /resource/   => Options(http.ResponseWriter, id string)
OPTIONS /resource/id => Options(http.ResponseWriter, id string)

If you are not so familiar with the REST architecture, check these 2 wikipedia links for an overview:

If you want to add a permission/accessibility-check to the resource, implement

HasAccess(*http.Request) (bool, os.Error)

The server will then route HTTP requests to the appropriate method call.

The snips example provides a full example of both a client and server. The weekdays example project implements a simple REST server.

Documentation

Overview

A RESTful HTTP client and server.

rest.go uses the standard http package by adding resource routes. Add a new route by:

rest.Resource("resourcepath", resourcevariable)

and then use http as normal.

A resource is an object that may have any of the following methods which respond to the specified HTTP requests:

GET /resource/ => Index(http.ResponseWriter)
GET /resource/id => Find(http.ResponseWriter, id string)
POST /resource/ => Create(http.ResponseWriter, *http.Request)
PUT /resource/id => Update(http.ResponseWriter, id string, *http.Request)
DELETE /resource/id => Delete(http.ResponseWriter, id string)
OPTIONS /resource/ => Options(http.ResponseWriter, id string)
OPTIONS /resource/id => Options(http.ResponseWriter, id string)

The server will then route HTTP requests to the appropriate method call.

The snips example provides a full example of both a client and server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(c http.ResponseWriter, instructions string)

Emits a bad request with the specified instructions

func Created

func Created(c http.ResponseWriter, location string)

Emits a 201 Created with the URI for the new location

func NoContent

func NoContent(c http.ResponseWriter)

Emits a 204 No Content

func NotFound

func NotFound(c http.ResponseWriter)

Emits a 404 Not Found

func NotImplemented

func NotImplemented(c http.ResponseWriter)

Emits a 501 Not Implemented

func Resource

func Resource(path string, res interface{})

Add a resource route

func Updated

func Updated(c http.ResponseWriter, location string)

Emits a 200 OK with a location. Used when after a PUT

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(resource string) (*Client, error)

Creates a client for the specified resource.

The resource is the url to the base of the resource (i.e., http://127.0.0.1:3000/snips/)

func (*Client) Close

func (client *Client) Close()

Closes the clients connection

func (*Client) Create

func (client *Client) Create(body string) (*http.Response, error)

POST /resource

func (*Client) Delete

func (client *Client) Delete(id string) (*http.Response, error)

DELETE /resource/id

func (*Client) Find

func (client *Client) Find(id string) (*http.Response, error)

GET /resource/id

func (*Client) IdFromURL

func (client *Client) IdFromURL(urlString string) (string, error)

Parse a response-Location-URI to get the ID of the worked-on snip

func (*Client) Index

func (client *Client) Index() (*http.Response, error)

GET /resource/

func (*Client) Request

func (client *Client) Request(request *http.Request) (*http.Response, error)

Send a request

func (*Client) Update

func (client *Client) Update(id string, body string) (*http.Response, error)

PUT /resource/id

Directories

Path Synopsis
examples
snips
Defines SnipsCollection struct-type and NewSnipsCollection() It is used by the example REST server for storing and managing snips It has the methods Add, WithId, All and Remove.
Defines SnipsCollection struct-type and NewSnipsCollection() It is used by the example REST server for storing and managing snips It has the methods Add, WithId, All and Remove.
weekdays
Example REST server When run can be opened in a webbrowser: http://localhost:3000/wd/ for index, showing all entries as logic of the Index function specifies http://localhost:3000/wd/We get full string for index We (other indices from map also possible)
Example REST server When run can be opened in a webbrowser: http://localhost:3000/wd/ for index, showing all entries as logic of the Index function specifies http://localhost:3000/wd/We get full string for index We (other indices from map also possible)

Jump to

Keyboard shortcuts

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