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

* Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
125 lines
2.1 KiB
Go
125 lines
2.1 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package http
|
|
|
|
import (
|
|
"github.com/mainflux/mainflux/twins"
|
|
)
|
|
|
|
const (
|
|
maxNameSize = 1024
|
|
maxLimitSize = 100
|
|
)
|
|
|
|
type apiReq interface {
|
|
validate() error
|
|
}
|
|
|
|
type addTwinReq struct {
|
|
token string
|
|
Name string `json:"name,omitempty"`
|
|
Definition twins.Definition `json:"definition,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
}
|
|
|
|
func (req addTwinReq) validate() error {
|
|
if req.token == "" {
|
|
return twins.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if len(req.Name) > maxNameSize {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateTwinReq struct {
|
|
token string
|
|
id string
|
|
Name string `json:"name,omitempty"`
|
|
Definition twins.Definition `json:"definition,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
}
|
|
|
|
func (req updateTwinReq) validate() error {
|
|
if req.token == "" {
|
|
return twins.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if req.id == "" {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
if len(req.Name) > maxNameSize {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type viewTwinReq struct {
|
|
token string
|
|
id string
|
|
}
|
|
|
|
func (req viewTwinReq) validate() error {
|
|
if req.token == "" {
|
|
return twins.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if req.id == "" {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listReq struct {
|
|
token string
|
|
offset uint64
|
|
limit uint64
|
|
name string
|
|
metadata map[string]interface{}
|
|
}
|
|
|
|
func (req *listReq) validate() error {
|
|
if req.token == "" {
|
|
return twins.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if req.limit == 0 || req.limit > maxLimitSize {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
if len(req.name) > maxNameSize {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listStatesReq struct {
|
|
token string
|
|
offset uint64
|
|
limit uint64
|
|
id string
|
|
}
|
|
|
|
func (req *listStatesReq) validate() error {
|
|
if req.token == "" {
|
|
return twins.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if req.id == "" {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
if req.limit == 0 || req.limit > maxLimitSize {
|
|
return twins.ErrMalformedEntity
|
|
}
|
|
|
|
return nil
|
|
}
|