1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Aleksandar Novaković a6a8648e4f MF-783 - Allow access checking by a thing ID (#784)
* Add can access by things ID endpoint to things service

Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>

* Add new auth endpoint to the swagger docs

Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>

* Add test for the new endpoint of the things service

Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2019-07-15 18:28:15 +02:00

198 lines
6.2 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
// +build !test
package api
import (
"time"
"github.com/go-kit/kit/metrics"
"github.com/mainflux/mainflux/things"
)
var _ things.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc things.Service
}
// MetricsMiddleware instruments core service by tracking request count and
// latency.
func MetricsMiddleware(svc things.Service, counter metrics.Counter, latency metrics.Histogram) things.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (ms *metricsMiddleware) AddThing(token string, thing things.Thing) (things.Thing, error) {
defer func(begin time.Time) {
ms.counter.With("method", "add_thing").Add(1)
ms.latency.With("method", "add_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.AddThing(token, thing)
}
func (ms *metricsMiddleware) UpdateThing(token string, thing things.Thing) error {
defer func(begin time.Time) {
ms.counter.With("method", "update_thing").Add(1)
ms.latency.With("method", "update_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateThing(token, thing)
}
func (ms *metricsMiddleware) UpdateKey(token, id, key string) error {
defer func(begin time.Time) {
ms.counter.With("method", "update_key").Add(1)
ms.latency.With("method", "update_key").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateKey(token, id, key)
}
func (ms *metricsMiddleware) ViewThing(token, id string) (things.Thing, error) {
defer func(begin time.Time) {
ms.counter.With("method", "view_thing").Add(1)
ms.latency.With("method", "view_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ViewThing(token, id)
}
func (ms *metricsMiddleware) ListThings(token string, offset, limit uint64, name string) (things.ThingsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_things").Add(1)
ms.latency.With("method", "list_things").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListThings(token, offset, limit, name)
}
func (ms *metricsMiddleware) ListThingsByChannel(token, id string, offset, limit uint64) (things.ThingsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_things_by_channel").Add(1)
ms.latency.With("method", "list_things_by_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListThingsByChannel(token, id, offset, limit)
}
func (ms *metricsMiddleware) RemoveThing(token, id string) error {
defer func(begin time.Time) {
ms.counter.With("method", "remove_thing").Add(1)
ms.latency.With("method", "remove_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.RemoveThing(token, id)
}
func (ms *metricsMiddleware) CreateChannel(token string, channel things.Channel) (things.Channel, error) {
defer func(begin time.Time) {
ms.counter.With("method", "create_channel").Add(1)
ms.latency.With("method", "create_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.CreateChannel(token, channel)
}
func (ms *metricsMiddleware) UpdateChannel(token string, channel things.Channel) error {
defer func(begin time.Time) {
ms.counter.With("method", "update_channel").Add(1)
ms.latency.With("method", "update_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateChannel(token, channel)
}
func (ms *metricsMiddleware) ViewChannel(token, id string) (things.Channel, error) {
defer func(begin time.Time) {
ms.counter.With("method", "view_channel").Add(1)
ms.latency.With("method", "view_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ViewChannel(token, id)
}
func (ms *metricsMiddleware) ListChannels(token string, offset, limit uint64, name string) (things.ChannelsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_channels").Add(1)
ms.latency.With("method", "list_channels").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListChannels(token, offset, limit, name)
}
func (ms *metricsMiddleware) ListChannelsByThing(token, id string, offset, limit uint64) (things.ChannelsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_channels_by_thing").Add(1)
ms.latency.With("method", "list_channels_by_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListChannelsByThing(token, id, offset, limit)
}
func (ms *metricsMiddleware) RemoveChannel(token, id string) error {
defer func(begin time.Time) {
ms.counter.With("method", "remove_channel").Add(1)
ms.latency.With("method", "remove_channel").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.RemoveChannel(token, id)
}
func (ms *metricsMiddleware) Connect(token, chanID, thingID string) error {
defer func(begin time.Time) {
ms.counter.With("method", "connect").Add(1)
ms.latency.With("method", "connect").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Connect(token, chanID, thingID)
}
func (ms *metricsMiddleware) Disconnect(token, chanID, thingID string) error {
defer func(begin time.Time) {
ms.counter.With("method", "disconnect").Add(1)
ms.latency.With("method", "disconnect").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Disconnect(token, chanID, thingID)
}
func (ms *metricsMiddleware) CanAccess(id, key string) (string, error) {
defer func(begin time.Time) {
ms.counter.With("method", "can_access").Add(1)
ms.latency.With("method", "can_access").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.CanAccess(id, key)
}
func (ms *metricsMiddleware) CanAccessByID(chanID, thingID string) error {
defer func(begin time.Time) {
ms.counter.With("method", "can_access_by_id").Add(1)
ms.latency.With("method", "can_access_by_id").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.CanAccessByID(chanID, thingID)
}
func (ms *metricsMiddleware) Identify(key string) (string, error) {
defer func(begin time.Time) {
ms.counter.With("method", "identify").Add(1)
ms.latency.With("method", "identify").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Identify(key)
}