Documentation ¶
Overview ¶
Package api2go enables building REST servers for the JSONAPI.org standard.
See https://github.com/univedo/api2go for usage instructions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API is a REST JSONAPI.
func NewAPI ¶
NewAPI returns an initialized API instance `prefix` is added in front of all endpoints.
func NewAPIWithBaseURL ¶
NewAPIWithBaseURL does the same as NewAPI with the addition of a baseURL which get's added in front of all generated URLs. For example http://localhost/v1/myResource/abc instead of /v1/myResource/abc
func (*API) AddResource ¶
AddResource registers a data source for the given resource At least the CRUD interface must be implemented, all the other interfaces are optional. `resource` should by an empty struct instance such as `Post{}`. The same type will be used for constructing new elements.
func (*API) SetRedirectTrailingSlash ¶
SetRedirectTrailingSlash enables 307 redirects on urls ending with / when disabled, an URL ending with / will 404
type CRUD ¶
type CRUD interface { // FindOne returns an object by its ID FindOne(ID string, req Request) (interface{}, error) // Create a new object and return its ID Create(obj interface{}, req Request) (string, error) // Delete an object Delete(id string, req Request) error // Update an object Update(obj interface{}, req Request) error }
The CRUD interface MUST be implemented in order to use the api2go api.
type Error ¶
type Error struct { ID string `json:"id,omitempty"` Href string `json:"href,omitempty"` Status string `json:"status,omitempty"` Code string `json:"code,omitempty"` Title string `json:"title,omitempty"` Detail string `json:"detail,omitempty"` Path string `json:"path,omitempty"` }
Error can be used for all kind of application errors e.g. you would use it to define form errors or any other semantical application problems for more information see http://jsonapi.org/format/#errors
type FindMultiple ¶
type FindMultiple interface { // FindMultiple returns all objects for the specified IDs FindMultiple(IDs []string, req Request) (interface{}, error) }
The FindMultiple interface can be optionally implemented to fetch multiple records by their ID at once.
type HTTPError ¶
type HTTPError struct { Errors []Error `json:"errors,omitempty"` // contains filtered or unexported fields }
HTTPError is used for errors
func NewHTTPError ¶
NewHTTPError creates a new error with message and status code. `err` will be logged (but never sent to a client), `msg` will be sent and `status` is the http status code. `err` can be nil.
type PaginatedFindAll ¶
type PaginatedFindAll interface {
PaginatedFindAll(req Request) (obj interface{}, totalCount uint, err error)
}
The PaginatedFindAll interface can be optionally implemented to fetch a subset of all records. Pagination query parameters must be used to limit the result. Pagination URLs will automatically be generated by the api. You can use a combination of the following 2 query parameters: page[number] AND page[size] OR page[offset] AND page[limit]
Directories ¶
Path | Synopsis |
---|---|
Create a new user: `curl -X POST http://localhost:31415/v0/users -d '{"data" : [{"type" : "users" , "username" : "marvin"}]}'` List users: `curl -X GET http://localhost:31415/v0/users` List paginated users: `curl -X GET http://localhost:31415/v0/users?page[offset]=0&page[limit]=2` OR `curl -X GET http://localhost:31415/v0/users?page[number]=1&page[size]=2` Update: `curl -vX PUT http://localhost:31415/v0/users/1 -d '{ "data" : {"type" : "users", "username" : "better marvin", "id" : "1"}}'` Delete: `curl -vX DELETE http://localhost:31415/v0/users/2` FindMultiple (this only works if you've called create a bunch of times :) `curl -X GET http://localhost:31415/v0/users/3,4` Create a chocolate with the name sweet `curl -X POST http://localhost:31415/v0/chocolates -d '{"data" : [{"type" : "chocolates" , "name" : "Ritter Sport", "taste": "Very Good"}]}'` Link the sweet `curl -X POST http://localhost:31415/v0/users -d '{"data" : [{"type" : "users" , "username" : "marvin", "links": {"sweets": {"linkage": {"type": "chocolates", "id": "1"}}}}]}'`
|
Create a new user: `curl -X POST http://localhost:31415/v0/users -d '{"data" : [{"type" : "users" , "username" : "marvin"}]}'` List users: `curl -X GET http://localhost:31415/v0/users` List paginated users: `curl -X GET http://localhost:31415/v0/users?page[offset]=0&page[limit]=2` OR `curl -X GET http://localhost:31415/v0/users?page[number]=1&page[size]=2` Update: `curl -vX PUT http://localhost:31415/v0/users/1 -d '{ "data" : {"type" : "users", "username" : "better marvin", "id" : "1"}}'` Delete: `curl -vX DELETE http://localhost:31415/v0/users/2` FindMultiple (this only works if you've called create a bunch of times :) `curl -X GET http://localhost:31415/v0/users/3,4` Create a chocolate with the name sweet `curl -X POST http://localhost:31415/v0/chocolates -d '{"data" : [{"type" : "chocolates" , "name" : "Ritter Sport", "taste": "Very Good"}]}'` Link the sweet `curl -X POST http://localhost:31415/v0/users -d '{"data" : [{"type" : "users" , "username" : "marvin", "links": {"sweets": {"linkage": {"type": "chocolates", "id": "1"}}}}]}'` |