Documentation ¶
Overview ¶
Package jshapi is a http.Handler compatible wrapper that makes building JSON API resource handlers easy.
Index ¶
- Variables
- type API
- type MockStorage
- func (m *MockStorage) Delete(ctx context.Context, id string) jsh.ErrorType
- func (m *MockStorage) Get(ctx context.Context, id string) (*jsh.Object, jsh.ErrorType)
- func (m *MockStorage) List(ctx context.Context) (jsh.List, jsh.ErrorType)
- func (m *MockStorage) SampleList(length int) jsh.List
- func (m *MockStorage) SampleObject(id string) *jsh.Object
- func (m *MockStorage) Save(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType)
- func (m *MockStorage) Update(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType)
- type MockToManyStorage
- func (m *MockToManyStorage) Delete(ctx context.Context, id string, list jsh.IDList) (jsh.IDList, jsh.ErrorType)
- func (m *MockToManyStorage) List(ctx context.Context, id string) (jsh.IDList, jsh.ErrorType)
- func (m *MockToManyStorage) ListResources(ctx context.Context, id string) (jsh.List, jsh.ErrorType)
- func (m *MockToManyStorage) SampleIDList(id string) jsh.IDList
- func (m *MockToManyStorage) SampleList(id string) jsh.List
- func (m *MockToManyStorage) Save(ctx context.Context, id string, list jsh.IDList) (jsh.IDList, jsh.ErrorType)
- func (m *MockToManyStorage) Update(ctx context.Context, id string, list jsh.IDList) (jsh.IDList, jsh.ErrorType)
- type MockToOneStorage
- func (m *MockToOneStorage) Get(ctx context.Context, id string) (*jsh.IDObject, jsh.ErrorType)
- func (m *MockToOneStorage) GetResource(ctx context.Context, id string) (*jsh.Object, jsh.ErrorType)
- func (m *MockToOneStorage) SampleIDObject(id string) *jsh.IDObject
- func (m *MockToOneStorage) SampleObject(id string) *jsh.Object
- func (m *MockToOneStorage) Update(ctx context.Context, id string, relationship *jsh.IDObject) (*jsh.IDObject, jsh.ErrorType)
- type Relationship
- type Resource
- func (res *Resource) Action(action string, storage store.Action, allow bool)
- func (res *Resource) CRUD(storage store.CRUD)
- func (res *Resource) Delete(storage store.Delete, allow bool)
- func (res *Resource) DeleteMany(storage store.ToManyUpdate, matcher string, allow bool)
- func (res *Resource) Get(storage store.Get, allow bool)
- func (res *Resource) GetRelated(storage store.Get, matcher string, allow bool)
- func (res *Resource) GetRelationship(storage store.ToOneGet, matcher string, allow bool)
- func (res *Resource) List(storage store.List, allow bool)
- func (res *Resource) ListRelated(storage store.ToManyListResources, matcher string, allow bool)
- func (res *Resource) ListRelationships(storage store.ToManyList, matcher string, allow bool)
- func (res *Resource) Options(pattern string)
- func (res *Resource) PartialCRUD(storage store.CRUD, disallow string)
- func (res *Resource) PartialToMany(relationship string, storage store.ToMany, disallow string)
- func (res *Resource) PartialToOne(relationship string, storage store.ToOne, disallow string)
- func (res *Resource) Patch(storage store.Update, allow bool)
- func (res *Resource) PatchMany(storage store.ToManyUpdate, matcher string, allow bool)
- func (res *Resource) PatchOne(storage store.ToOneUpdate, matcher string, allow bool)
- func (res *Resource) Post(storage store.Save, allow bool)
- func (res *Resource) PostMany(storage store.ToManyUpdate, matcher string, allow bool)
- func (res *Resource) RouteTree() string
- func (res *Resource) ToMany(relationship string, storage store.ToMany)
- func (res *Resource) ToOne(relationship string, storage store.ToOne)
- type Route
- type Sender
Constants ¶
This section is empty.
Variables ¶
var EnableClientGeneratedIDs bool
EnableClientGeneratedIDs is an option that allows consumers to allow for client generated IDs.
var SendHandler = DefaultSender(log.New(os.Stderr, "jshapi: ", log.LstdFlags))
SendHandler allows the customization of how API responses are sent and logged. This is used by all jshapi.Resource objects.
Functions ¶
This section is empty.
Types ¶
type API ¶
type API struct { *goji.Mux Resources map[string]*Resource Debug bool // contains filtered or unexported fields }
API is used to direct HTTP requests to resources
func Default ¶
Default builds a new top-level API with a few out of the box additions to get people started without needing to add a lot of extra functionality.
The most basic implementation is:
// create a logger, the std log package works, as do most other loggers // std.Logger interface defined here: // https://github.com/derekdowling/go-stdlogger/blob/master/logger.go logger := log.New(os.Stderr, "jshapi: ", log.LstdFlags) // create the API. Specify a http://yourapi/<prefix>/ if required api := jshapi.Default("<prefix>", false, logger) api.Add(yourResource)
type MockStorage ¶
type MockStorage struct { // ResourceType is the name of the resource you are mocking i.e. "user", "comment" ResourceType string // ResourceAttributes a sample set of attributes a resource object should have // used by GET /resources and GET /resources/:id ResourceAttributes interface{} // ListCount is the number of sample objects to return in a GET /resources request ListCount int }
MockStorage allows you to mock out APIs really easily. It is also used internally for testing the API layer.
func (*MockStorage) Delete ¶
func (m *MockStorage) Delete(ctx context.Context, id string) jsh.ErrorType
Delete does nothing
func (*MockStorage) Get ¶
func (m *MockStorage) Get(ctx context.Context, id string) (*jsh.Object, jsh.ErrorType)
Get returns a resource with ID as specified by the request
func (*MockStorage) List ¶
func (m *MockStorage) List(ctx context.Context) (jsh.List, jsh.ErrorType)
List returns a sample list
func (*MockStorage) SampleList ¶
func (m *MockStorage) SampleList(length int) jsh.List
SampleList generates a sample list of resources that can be used for/against the mock API
func (*MockStorage) SampleObject ¶
func (m *MockStorage) SampleObject(id string) *jsh.Object
SampleObject builds an object based on provided resource specifications
func (*MockStorage) Save ¶
func (m *MockStorage) Save(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType)
Save assigns a URL of 1 to the object
func (*MockStorage) Update ¶
func (m *MockStorage) Update(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType)
Update does nothing
type MockToManyStorage ¶
type MockToManyStorage MockStorage
MockToManyStorage allows you to mock out APIs to-many relationships really easily. \ It is also used internally for testing the API layer.
func (*MockToManyStorage) Delete ¶
func (m *MockToManyStorage) Delete(ctx context.Context, id string, list jsh.IDList) (jsh.IDList, jsh.ErrorType)
Delete does nothing
func (*MockToManyStorage) List ¶
func (m *MockToManyStorage) List(ctx context.Context, id string) (jsh.IDList, jsh.ErrorType)
ListRelationships returns the to-many relationship ID objects as specified by the request
func (*MockToManyStorage) ListResources ¶
func (m *MockToManyStorage) ListResources(ctx context.Context, id string) (jsh.List, jsh.ErrorType)
List returns the to-many relationship resources with ID as specified by the request
func (*MockToManyStorage) SampleIDList ¶
func (m *MockToManyStorage) SampleIDList(id string) jsh.IDList
SampleIDObject builds an ID object based on provided resource specifications
func (*MockToManyStorage) SampleList ¶
func (m *MockToManyStorage) SampleList(id string) jsh.List
SampleObject builds an object based on provided resource specifications
type MockToOneStorage ¶
type MockToOneStorage MockStorage
MockToOneStorage allows you to mock out APIs to-one relationships really easily. \ It is also used internally for testing the API layer.
func (*MockToOneStorage) Get ¶
func (m *MockToOneStorage) Get(ctx context.Context, id string) (*jsh.IDObject, jsh.ErrorType)
GetRelationship returns the to-one relationship ID object as specified by the request
func (*MockToOneStorage) GetResource ¶
func (m *MockToOneStorage) GetResource(ctx context.Context, id string) (*jsh.Object, jsh.ErrorType)
Get returns the to-one relationship resource with ID as specified by the request
func (*MockToOneStorage) SampleIDObject ¶
func (m *MockToOneStorage) SampleIDObject(id string) *jsh.IDObject
SampleIDObject builds an ID object based on provided resource specifications
func (*MockToOneStorage) SampleObject ¶
func (m *MockToOneStorage) SampleObject(id string) *jsh.Object
SampleObject builds an object based on provided resource specifications
type Relationship ¶
type Relationship string
Relationship helps define the relationship between two resources
const ( // ToOne signifies a one to one relationship ToOne Relationship = "One-To-One" // ToMany signifies a one to many relationship ToMany Relationship = "One-To-Many" )
type Resource ¶
type Resource struct { *goji.Mux // The singular name of the resource type("user", "post", etc) Type string // Routes is a list of routes registered to the resource Routes []Route // Map of relationships Relationships map[string]Relationship }
Resource holds the necessary state for creating a REST API endpoint for a given resource type. Will be accessible via `/<type>`.
Using NewCRUDResource you can generate a generic CRUD handler for a JSON Specification Resource end point. If you wish to only implement a subset of these endpoints that is also available through NewResource() and manually registering storage handlers via .Post(), .Get(), .List(), .Patch(), and .Delete():
Besides the built in registration helpers, it isn't recommended, but you can add your own routes using the goji.Mux API:
func searchHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { name := pat.Param(ctx, "name") fmt.Fprintf(w, "Hello, %s!", name) } resource := jshapi.NewCRUDResource("users", userStorage) // creates /users/search/:name resource.HandleC(pat.New("search/:name"), searchHandler)
func NewCRUDResource ¶
NewCRUDResource generates a resource
func NewMockResource ¶
NewMockResource builds a mock API endpoint that can perform basic CRUD actions:
GET /types POST /types GET /types/:id DELETE /types/:id PATCH /types/:id
Will return objects and lists based upon the sampleObject that is specified here in the constructor.
func NewResource ¶
NewResource is a resource constructor that makes no assumptions about routes that you'd like to implement, but still provides some basic utilities for managing routes and handling API calls.
The prefix parameter causes all routes created within the resource to be prefixed.
func (*Resource) Action ¶
Action adds to the resource a custom action of the form: POST /resources/:id/<action>
func (*Resource) CRUD ¶
CRUD is syntactic sugar and a shortcut for registering all JSON API CRUD routes for a compatible storage implementation:
Registers handlers for:
GET /resource POST /resource GET /resource/:id DELETE /resource/:id PATCH /resource/:id
func (*Resource) DeleteMany ¶
func (res *Resource) DeleteMany(storage store.ToManyUpdate, matcher string, allow bool)
DeleteMany registers a `DELETE /resources/:id/relationships/<relationship>` handler for the resource relationships.
func (*Resource) GetRelated ¶
GetRelated registers a `GET /resources/:id/<relationship>` handler for the resource relationship.
func (*Resource) GetRelationship ¶
GetRelationship registers a `GET /resources/:id/relationships/<relationship>` handler for the resource relationship.
func (*Resource) ListRelated ¶
func (res *Resource) ListRelated(storage store.ToManyListResources, matcher string, allow bool)
ListRelated registers a `GET /resources/:id/<relationship>` handler for the resource relationships.
func (*Resource) ListRelationships ¶
func (res *Resource) ListRelationships(storage store.ToManyList, matcher string, allow bool)
ListRelationships registers a `GET /resources/:id/relationships/<relationship>` handler for the resource relationships.
func (*Resource) PartialCRUD ¶
PartialCRUD registers all CRUD routes with OPTIONS and HEAD support. It provides a handler that sends a 405 response for methods contained in the disallow parameter. Since GET is always allowed, the supported parameters are POST,PATCH,DELETE.
func (*Resource) PartialToMany ¶
PartialToMany registers to-many relationships routes with OPTIONS and HEAD support. It provides a handler that sends a 405 response for methods contained in the disallow parameter. Since GET is always allowed, the supported parameters are POST,PATCH,DELETE.
func (*Resource) PartialToOne ¶
PartialToOne registers to-one relationships routes with OPTIONS and HEAD support. It provides a handler that sends a 405 response for methods contained in the disallow parameter. Since GET is always allowed, the only supported parameter is PATCH.
func (*Resource) PatchMany ¶
func (res *Resource) PatchMany(storage store.ToManyUpdate, matcher string, allow bool)
PatchMany registers a `PATCH /resources/:id/relationships/<relationship>` handler for the resource relationships.
func (*Resource) PatchOne ¶
func (res *Resource) PatchOne(storage store.ToOneUpdate, matcher string, allow bool)
PatchOne registers a `PATCH /resources/:id/relationships/<relationship>` handler for the resource relationship.
func (*Resource) PostMany ¶
func (res *Resource) PostMany(storage store.ToManyUpdate, matcher string, allow bool)
PostMany registers a `POST /resources/:id/relationships/<relationship>` handler for the resource relationships.
func (*Resource) RouteTree ¶
RouteTree prints a recursive route tree based on what the resource, and all subresources have registered
func (*Resource) ToMany ¶
ToMany is syntactic sugar for registering all JSON API routes for a to-many relationship:
Registers handlers for:
GET /resource/:id/relationship GET /resource/:id/relationships/relationship PATCH /resource/:id/relationships/relationship POST /resource/:id/relationships/relationship DELETE /resource/:id/relationships/relationship
CRUD actions on a specific relationship "resourceType" object should be performed via it's own top level /<resourceType> jsh-api handler as per JSONAPI specification.
func (*Resource) ToOne ¶
ToOne is syntactic sugar for registering all JSON API routes for a to-one relationship:
Registers handlers for:
GET /resource/:id/relationship GET /resource/:id/relationships/relationship PATCH /resource/:id/relationships/relationship
CRUD actions on a specific relationship "resourceType" object should be performed via it's own top level /<resourceType> jsh-api handler as per JSONAPI specification.
type Sender ¶
Sender is a function type definition that allows consumers to customize how they send and log API responses.
func DefaultSender ¶
func DefaultSender(logger std.Logger) Sender
DefaultSender is the default sender that will log 5XX errors that it encounters in the process of sending a response.