ducky/devices

Paddy 2015-12-22 Parent:c24a6c5fcd8c Child:ed1b5ba69551

18:b2fdf827758e Go to Latest

ducky/devices/apiv1/endpoints.go

Add endpoint for retrieving devices. Add an endpoint for retrieving devices, either as a list or by ID. Stub endpoints for updating and deleting devices., along with TODOs marking them as things to still be completed. (Right now, accessing those endpoints is an insta-panic.) Simplify our handleCreateDevices by returning StatusUnauthorized if AuthUser fails, so we can reserve StatusForbidden for when auth succeeds but access is still denied. Also, delay the instantiation and allocation of a Response variable until we're actually going to use it. Create a handleGetDevices handler that authenticates the user, and if no ID is set, returns a list of all their Devices. If one or more IDs are set, only those Devices are returned. If ScopeViewPushToken is one of the scopes associated with the request, the push tokens for each Device will be included in the response. Otherwise, they will be omitted.

History
paddy@15 1 package apiv1
paddy@15 2
paddy@15 3 import (
paddy@15 4 "net/http"
paddy@15 5
paddy@15 6 "code.secondbit.org/api.hg"
paddy@15 7 "code.secondbit.org/trout.hg"
paddy@15 8 "golang.org/x/net/context"
paddy@15 9 )
paddy@15 10
paddy@15 11 // GetRouter returns a trout Router that is configured to handle
paddy@15 12 // all the routes necessary to serve a devices API server.
paddy@15 13 func GetRouter(ctx context.Context) http.Handler {
paddy@15 14 var router trout.Router
paddy@15 15 router.Endpoint("/").Methods("POST").Handler(api.ContextWrapper(ctx, api.ContextHandler(handleCreateDevices)))
paddy@18 16 router.Endpoint("/").Methods("GET").Handler(api.ContextWrapper(ctx, api.ContextHandler(handleGetDevices)))
paddy@18 17 router.Endpoint("/{id}").Methods("GET").Handler(api.ContextWrapper(ctx, api.ContextHandler(handleGetDevices)))
paddy@18 18 // TODO(paddy): add the update handler
paddy@18 19 router.Endpoint("/{id}").Methods("PATCH").Handler(api.ContextWrapper(ctx, api.ContextHandler(nil)))
paddy@18 20 // TODO(paddy): add the delete handler
paddy@18 21 router.Endpoint("/").Methods("DELETE").Handler(api.ContextWrapper(ctx, api.ContextHandler(nil)))
paddy@18 22 router.Endpoint("/{id}").Methods("DELETE").Handler(api.ContextWrapper(ctx, api.ContextHandler(nil)))
paddy@15 23
paddy@15 24 return router
paddy@15 25 }