2018-05-10 23:53:25 +02:00
|
|
|
package http
|
2017-09-23 01:03:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2017-09-23 01:03:27 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func addThingEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
2018-05-15 17:13:09 +02:00
|
|
|
req := request.(addThingReq)
|
2017-10-01 01:07:37 +02:00
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, err := svc.AddThing(req.key, req.thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
return thingRes{id: saved.ID, created: true}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func updateThingEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
2018-05-15 17:13:09 +02:00
|
|
|
req := request.(updateThingReq)
|
2017-10-01 01:07:37 +02:00
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
req.thing.ID = req.id
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
if err := svc.UpdateThing(req.key, req.thing); err != nil {
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return thingRes{id: req.id, created: false}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func viewThingEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(viewResourceReq)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
thing, err := svc.ViewThing(req.key, req.id)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return viewThingRes{thing}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func listThingsEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listResourcesReq)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
things, err := svc.ListThings(req.key, req.offset, req.limit)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return listThingsRes{Things: things}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func removeThingEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(viewResourceReq)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
err := req.validate()
|
2018-05-15 17:13:09 +02:00
|
|
|
if err == things.ErrNotFound {
|
2017-10-01 01:07:37 +02:00
|
|
|
return removeRes{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
if err = svc.RemoveThing(req.key, req.id); err != nil {
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return removeRes{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func createChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := request.(createChannelReq)
|
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, err := svc.CreateChannel(req.key, req.channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
return channelRes{id: saved.ID, created: true}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func updateChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := request.(updateChannelReq)
|
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
req.channel.ID = req.id
|
|
|
|
|
|
|
|
if err := svc.UpdateChannel(req.key, req.channel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channelRes{id: req.id, created: false}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func viewChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(viewResourceReq)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
channel, err := svc.ViewChannel(req.key, req.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return viewChannelRes{channel}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func listChannelsEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listResourcesReq)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
channels, err := svc.ListChannels(req.key, req.offset, req.limit)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return listChannelsRes{Channels: channels}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func removeChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
2017-09-23 01:03:27 +02:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(viewResourceReq)
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
if err := req.validate(); err != nil {
|
2018-05-15 17:13:09 +02:00
|
|
|
if err == things.ErrNotFound {
|
2018-03-11 18:06:01 +01:00
|
|
|
return removeRes{}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
if err := svc.RemoveChannel(req.key, req.id); err != nil {
|
2017-10-01 01:07:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
return removeRes{}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 17:13:09 +02:00
|
|
|
func connectEndpoint(svc things.Service) endpoint.Endpoint {
|
2018-03-11 18:06:01 +01:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
cr := request.(connectionReq)
|
|
|
|
|
|
|
|
if err := cr.validate(); err != nil {
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
if err := svc.Connect(cr.key, cr.chanID, cr.thingID); err != nil {
|
2018-03-11 18:06:01 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return connectionRes{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func disconnectEndpoint(svc things.Service) endpoint.Endpoint {
|
2018-03-11 18:06:01 +01:00
|
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
|
|
cr := request.(connectionReq)
|
|
|
|
|
|
|
|
if err := cr.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
if err := svc.Disconnect(cr.key, cr.chanID, cr.thingID); err != nil {
|
2018-03-11 18:06:01 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return disconnectionRes{}, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|