2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
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-10-24 11:21:03 +02:00
|
|
|
thing := things.Thing{
|
|
|
|
Type: req.Type,
|
|
|
|
Name: req.Name,
|
|
|
|
Metadata: req.Metadata,
|
|
|
|
}
|
|
|
|
saved, err := svc.AddThing(req.key, thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := thingRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: saved.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
created: true,
|
|
|
|
}
|
|
|
|
return res, 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-10-24 11:21:03 +02:00
|
|
|
thing := things.Thing{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: req.id,
|
2018-10-24 11:21:03 +02:00
|
|
|
Type: req.Type,
|
|
|
|
Name: req.Name,
|
|
|
|
Metadata: req.Metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := svc.UpdateThing(req.key, thing); err != nil {
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := thingRes{id: req.id, created: false}
|
|
|
|
return res, 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-12-05 13:09:25 +01:00
|
|
|
thing, err := svc.ViewThing(req.key, req.id)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := viewThingRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: thing.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
Owner: thing.Owner,
|
|
|
|
Type: thing.Type,
|
|
|
|
Name: thing.Name,
|
|
|
|
Key: thing.Key,
|
|
|
|
Metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
return res, 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-10-24 11:21:03 +02:00
|
|
|
res := listThingsRes{}
|
|
|
|
for _, thing := range things {
|
|
|
|
view := viewThingRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: thing.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
Owner: thing.Owner,
|
|
|
|
Type: thing.Type,
|
|
|
|
Name: thing.Name,
|
|
|
|
Key: thing.Key,
|
|
|
|
Metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
res.Things = append(res.Things, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, 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-12-05 13:09:25 +01: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-11-28 15:58:48 +01:00
|
|
|
channel := things.Channel{Name: req.Name, Metadata: req.Metadata}
|
2018-10-24 11:21:03 +02:00
|
|
|
saved, err := svc.CreateChannel(req.key, channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := channelRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: saved.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
created: true,
|
|
|
|
}
|
|
|
|
return res, 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
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
channel := things.Channel{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: req.id,
|
2018-11-28 15:58:48 +01:00
|
|
|
Name: req.Name,
|
|
|
|
Metadata: req.Metadata,
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
|
|
|
if err := svc.UpdateChannel(req.key, channel); err != nil {
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := channelRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: req.id,
|
2018-10-24 11:21:03 +02:00
|
|
|
created: false,
|
|
|
|
}
|
|
|
|
return res, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
channel, err := svc.ViewChannel(req.key, req.id)
|
2017-09-23 01:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
res := viewChannelRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: channel.ID,
|
2018-11-28 15:58:48 +01:00
|
|
|
Owner: channel.Owner,
|
|
|
|
Name: channel.Name,
|
|
|
|
Metadata: channel.Metadata,
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
|
|
|
for _, thing := range channel.Things {
|
|
|
|
view := viewThingRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: thing.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
Owner: thing.Owner,
|
|
|
|
Type: thing.Type,
|
|
|
|
Name: thing.Name,
|
|
|
|
Key: thing.Key,
|
|
|
|
Metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
res.Things = append(res.Things, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-24 11:21:03 +02:00
|
|
|
res := listChannelsRes{}
|
|
|
|
// Cast channels
|
|
|
|
for _, channel := range channels {
|
|
|
|
cView := viewChannelRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: channel.ID,
|
2018-11-28 15:58:48 +01:00
|
|
|
Owner: channel.Owner,
|
|
|
|
Name: channel.Name,
|
|
|
|
Metadata: channel.Metadata,
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cast things
|
|
|
|
for _, thing := range channel.Things {
|
|
|
|
tView := viewThingRes{
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: thing.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
Owner: thing.Owner,
|
|
|
|
Type: thing.Type,
|
|
|
|
Name: thing.Name,
|
|
|
|
Key: thing.Key,
|
|
|
|
Metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
cView.Things = append(cView.Things, tView)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Channels = append(res.Channels, cView)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, 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-12-05 13:09:25 +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-06-16 02:30:46 +02:00
|
|
|
|
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-12-05 13:09:25 +01: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-12-05 13:09:25 +01: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
|
|
|
}
|
|
|
|
}
|