2017-10-01 01:07:37 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
|
|
|
)
|
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
const maxLimitSize = 100
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
type apiReq interface {
|
|
|
|
validate() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type userReq struct {
|
|
|
|
user manager.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req userReq) validate() error {
|
|
|
|
return req.user.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
type identityReq struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req identityReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type addClientReq struct {
|
|
|
|
key string
|
|
|
|
client manager.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req addClientReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.client.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateClientReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
client manager.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateClientReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
|
|
return manager.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.client.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
type createChannelReq struct {
|
|
|
|
key string
|
|
|
|
channel manager.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req createChannelReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateChannelReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
channel manager.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateChannelReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
|
|
return manager.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type viewResourceReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req viewResourceReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
|
|
return manager.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type listResourcesReq struct {
|
|
|
|
key string
|
|
|
|
offset int
|
2018-04-18 22:36:24 +02:00
|
|
|
limit int
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
func (req *listResourcesReq) validate() error {
|
2017-10-01 01:07:37 +02:00
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2018-04-18 23:06:11 +02:00
|
|
|
if req.offset >= 0 && req.limit > 0 && req.limit <= maxLimitSize {
|
2017-10-01 01:07:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager.ErrMalformedEntity
|
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
type connectionReq struct {
|
|
|
|
key string
|
|
|
|
chanId string
|
|
|
|
clientId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req connectionReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return manager.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2018-04-08 22:57:56 +02:00
|
|
|
if !govalidator.IsUUID(req.chanId) || !govalidator.IsUUID(req.clientId) {
|
2018-03-11 18:06:01 +01:00
|
|
|
return manager.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|