api

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: GPL-3.0 Imports: 8 Imported by: 4

README

api

Automatically implements your REST API.

Usage

Create an API instance
myAPI := api.New("/api/", DB)

Parameters:

  • The root of all your API routes
  • A database handle that fulfills the Database interface
Install on an Aero app
myAPI.Install(app)

This will register all API routes in the app.

Routes

For the following examples we'll assume you have the type Movie registered in the database and that your API root is /api/. Type names are automatically lowercased for all routes.

GET /api/movie/:id

Action: get

Fetches the object with the given ID from the database and shows the JSON representation.

Example response:

{
	"id": 1,
	"title": "The Last Samurai"
}

If you need to filter out sensitive or private data you can implement the Filter interface on the data type.

POST /api/movie/:id

Action: edit

Writes new data to the object with the given ID. The data needs to be a JSON-formatted map[string]interface{} where each key stands for a path to a field of this object. The data type needs to implement the Editable interface. Editable fields must be whitelisted with the tag editable using the value true.

Example request:

{
	"Title": "The First Samurai"
}

Alternate example using advanced key paths:

{
	"Title": "The First Samurai",
	"Staff.Director.Name": "Edward Zwick",
	"Actors[0].Name": "Tom Cruise",
	"Actors[0].BirthYear": 1962
}
POST /api/new/movie

Action: create

Create a new object of that data type. The data type needs to implement the Creatable interface to register that route. Usually the post body contains a JSON-formatted key/value map which is used as the initial data for the new object.

Example request:

{
	"title": "The First Samurai",
	"directorName": "Edward Zwick",
}

It is up to the developer how the data is interpreted. This API library doesn't make any assumptions about the POST body in create requests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetObjectProperties

func SetObjectProperties(obj interface{}, edits map[string]interface{}, ctx *aero.Context) error

SetObjectProperties ...

Types

type API

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

API ...

func New

func New(root string, db Database) *API

New creates a new API.

func (*API) ActionHandler

func (api *API) ActionHandler(action *Action) (string, aero.Handle)

ActionHandler ...

func (*API) ArrayAppend

func (api *API) ArrayAppend(table string) (string, aero.Handle)

ArrayAppend ...

func (*API) ArrayRemove

func (api *API) ArrayRemove(table string) (string, aero.Handle)

ArrayRemove ...

func (*API) Create

func (api *API) Create(table string) (string, aero.Handle)

Create ...

func (*API) Delete

func (api *API) Delete(table string) (string, aero.Handle)

Delete ...

func (*API) Edit

func (api *API) Edit(table string) (string, aero.Handle)

Edit ...

func (*API) EditField

func (api *API) EditField(table string) (string, aero.Handle)

EditField ...

func (*API) Get

func (api *API) Get(table string) (string, aero.Handle)

Get ...

func (*API) GetField

func (api *API) GetField(table string) (string, aero.Handle)

GetField ...

func (*API) Install

func (api *API) Install(app *aero.Application)

Install ...

func (*API) RegisterAction

func (api *API) RegisterAction(action *Action)

RegisterAction registers an action for a table.

func (*API) RegisterActions

func (api *API) RegisterActions(table string, actions []*Action)

RegisterActions registers actions for a table.

func (*API) RegisterTable

func (api *API) RegisterTable(app *aero.Application, table string, objType reflect.Type)

RegisterTable registers a single table.

func (*API) Type

func (api *API) Type(table string) reflect.Type

Type ...

type Action

type Action struct {
	Table string
	Name  string
	Route string
	Run   func(obj interface{}, ctx *aero.Context) error
}

Action defines a single action on a given datatype.

type Actionable

type Actionable interface {
	Authorizable
}

Actionable means the data type can execute actions.

type AfterEditable

type AfterEditable interface {
	AfterEdit(ctx *aero.Context) error
}

An AfterEditable is called after the editing process happens and before the object is saved.

type ArrayEventListener

type ArrayEventListener interface {
	OnAppend(ctx *aero.Context, field string, index int, obj interface{})
	OnRemove(ctx *aero.Context, field string, index int, obj interface{})
}

ArrayEventListener means the data type can authorize changes.

type Authorizable

type Authorizable interface {
	// Authorize returns an error if the given API request is not authorized.
	Authorize(ctx *aero.Context, action string) error
}

Authorizable means the data type can authorize changes.

type Creatable

type Creatable interface {
	Create(*aero.Context) error
}

Creatable defines an object that can be created with some initial data.

type CustomEditable

type CustomEditable interface {
	Edit(ctx *aero.Context, key string, value reflect.Value, newValue reflect.Value) (consumed bool, err error)
}

A CustomEditable has its own implementation on how to edit certain object fields.

type Database

type Database interface {
	Get(table string, id string) (interface{}, error)
	Set(table string, id string, obj interface{})
	Delete(table string, id string) bool
	Types() map[string]reflect.Type
}

Database ...

type Deletable

type Deletable interface {
	Authorizable
	DeleteInContext(*aero.Context) error
	Delete() error
}

Deletable defines an object type that can be deleted from the database.

type Editable

type Editable interface {
	Authorizable
	Savable
}

An Editable can authorize changes, be changed and be saved in the database.

type Filter

type Filter interface {
	ShouldFilter(*aero.Context) bool
	Filter()
}

Filter describes an object with private data that needs to be filtered in the public API.

type Newable

type Newable interface {
	Savable
	Authorizable
	Creatable
}

Newable defines an object type where new instances can be created by users and saved in the database.

type Savable

type Savable interface {
	// Save saves the object in the database.
	Save()
}

Savable objects can be saved in the database.

type VirtualEditable

type VirtualEditable interface {
	VirtualEdit(ctx *aero.Context, key string, newValue reflect.Value) (bool, error)
}

A VirtualEditable has virtual properties that do not really exist but can be set.

Jump to

Keyboard shortcuts

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