2019-01-09 15:42:23 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import "github.com/mainflux/mainflux/bootstrap"
|
|
|
|
|
|
|
|
type apiReq interface {
|
|
|
|
validate() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type addReq struct {
|
|
|
|
key string
|
2019-02-22 14:54:09 +01:00
|
|
|
ThingID string `json:"thing_id"`
|
2019-01-09 15:42:23 +01:00
|
|
|
ExternalID string `json:"external_id"`
|
|
|
|
ExternalKey string `json:"external_key"`
|
|
|
|
Channels []string `json:"channels"`
|
2019-02-06 10:28:54 +01:00
|
|
|
Name string `json:"name"`
|
2019-01-09 15:42:23 +01:00
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req addReq) validate() error {
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:42:23 +01:00
|
|
|
if req.ExternalID == "" || req.ExternalKey == "" {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type entityReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req entityReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.id == "" {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:42:23 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateReq struct {
|
2019-03-04 17:41:38 +01:00
|
|
|
key string
|
|
|
|
id string
|
|
|
|
Name string `json:"name"`
|
|
|
|
Content string `json:"content"`
|
2019-01-09 15:42:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.id == "" {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:41:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateConnReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
Channels []string `json:"channels"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateConnReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.id == "" {
|
2019-01-09 15:42:23 +01:00
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type listReq struct {
|
|
|
|
key string
|
|
|
|
filter bootstrap.Filter
|
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req listReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.limit == 0 || req.limit > maxLimit {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:42:23 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type bootstrapReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req bootstrapReq) validate() error {
|
|
|
|
if req.key == "" {
|
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.id == "" {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type changeStateReq struct {
|
|
|
|
key string
|
|
|
|
id string
|
|
|
|
State bootstrap.State `json:"state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req changeStateReq) validate() error {
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.key == "" {
|
2019-01-09 15:42:23 +01:00
|
|
|
return bootstrap.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:54:09 +01:00
|
|
|
if req.id == "" {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:42:23 +01:00
|
|
|
if req.State != bootstrap.Inactive &&
|
|
|
|
req.State != bootstrap.Active {
|
|
|
|
return bootstrap.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|