mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Update WS tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require in all writer tests Refactor code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Ignore Mainflux generated pb.go files Ignore *.pb.go files generated by Mainflux, but don't ignore vendored generated code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Return an exported ErrNotFound instead of the unexported one Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks to match the actual behaviour Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks error message Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add auth service unavailable error test Since this error is caused by gRPC server returning codes.Internal, this behaviour is simulated using specific token. When that token is passed as an auth header, the mock gRPC client returns aforementioned error. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require package for postgres tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove redundant error checks in tests Refactor tests. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename error flag token Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
219 lines
4.8 KiB
Go
219 lines
4.8 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/mainflux/mainflux/things"
|
|
)
|
|
|
|
func addThingEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(addThingReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
saved, err := svc.AddThing(req.key, req.thing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return thingRes{id: saved.ID, created: true}, nil
|
|
}
|
|
}
|
|
|
|
func updateThingEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateThingReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.thing.ID = req.id
|
|
|
|
if err := svc.UpdateThing(req.key, req.thing); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return thingRes{id: req.id, created: false}, nil
|
|
}
|
|
}
|
|
|
|
func viewThingEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
thing, err := svc.ViewThing(req.key, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return viewThingRes{thing}, nil
|
|
}
|
|
}
|
|
|
|
func listThingsEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listResourcesReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
things, err := svc.ListThings(req.key, req.offset, req.limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return listThingsRes{Things: things}, nil
|
|
}
|
|
}
|
|
|
|
func removeThingEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
err := req.validate()
|
|
if err == things.ErrNotFound {
|
|
return removeRes{}, nil
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = svc.RemoveThing(req.key, req.id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return removeRes{}, nil
|
|
}
|
|
}
|
|
|
|
func createChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(createChannelReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
saved, err := svc.CreateChannel(req.key, req.channel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return channelRes{id: saved.ID, created: true}, nil
|
|
}
|
|
}
|
|
|
|
func updateChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateChannelReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
func viewChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err := svc.ViewChannel(req.key, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return viewChannelRes{channel}, nil
|
|
}
|
|
}
|
|
|
|
func listChannelsEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listResourcesReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channels, err := svc.ListChannels(req.key, req.offset, req.limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return listChannelsRes{Channels: channels}, nil
|
|
}
|
|
}
|
|
|
|
func removeChannelEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
if err == things.ErrNotFound {
|
|
return removeRes{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.RemoveChannel(req.key, req.id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return removeRes{}, nil
|
|
}
|
|
}
|
|
|
|
func connectEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
cr := request.(connectionReq)
|
|
|
|
if err := cr.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.Connect(cr.key, cr.chanID, cr.thingID); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return connectionRes{}, nil
|
|
}
|
|
}
|
|
|
|
func disconnectEndpoint(svc things.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
cr := request.(connectionReq)
|
|
|
|
if err := cr.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.Disconnect(cr.key, cr.chanID, cr.thingID); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return disconnectionRes{}, nil
|
|
}
|
|
}
|