2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
package http
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
import "github.com/mainflux/mainflux/things"
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
const maxLimitSize = 100
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
type apiReq interface {
|
|
|
|
validate() error
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
type addThingReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2019-04-16 14:58:56 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
2019-04-25 14:37:51 +02:00
|
|
|
Key string `json:"key,omitempty"`
|
2019-04-16 14:58:56 +02:00
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (req addThingReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
return nil
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
type updateThingReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2018-10-24 11:21:03 +02:00
|
|
|
id string
|
2019-04-16 14:58:56 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (req updateThingReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 14:09:11 +02:00
|
|
|
if req.id == "" {
|
2018-10-24 11:21:03 +02:00
|
|
|
return things.ErrMalformedEntity
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
return nil
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
type updateKeyReq struct {
|
|
|
|
token string
|
|
|
|
id string
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateKeyReq) validate() error {
|
|
|
|
if req.token == "" {
|
|
|
|
return things.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.id == "" || req.Key == "" {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
type createChannelReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2019-04-16 14:58:56 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req createChannelReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateChannelReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2018-11-28 15:58:48 +01:00
|
|
|
id string
|
2019-04-16 14:58:56 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req updateChannelReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
if req.id == "" {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type viewResourceReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
|
|
|
id string
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req viewResourceReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
if req.id == "" {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type listResourcesReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2018-10-24 11:21:03 +02:00
|
|
|
offset uint64
|
|
|
|
limit uint64
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
func (req *listResourcesReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
if req.limit == 0 || req.limit > maxLimitSize {
|
|
|
|
return things.ErrMalformedEntity
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
return nil
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2019-01-08 11:53:24 +01:00
|
|
|
type listByConnectionReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2019-01-08 11:53:24 +01:00
|
|
|
id string
|
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req listByConnectionReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2019-01-08 11:53:24 +01:00
|
|
|
return things.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.id == "" {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.limit == 0 || req.limit > maxLimitSize {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
type connectionReq struct {
|
2019-04-25 14:37:51 +02:00
|
|
|
token string
|
2018-10-24 11:21:03 +02:00
|
|
|
chanID string
|
|
|
|
thingID string
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req connectionReq) validate() error {
|
2019-04-25 14:37:51 +02:00
|
|
|
if req.token == "" {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrUnauthorizedAccess
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
if req.chanID == "" || req.thingID == "" {
|
|
|
|
return things.ErrMalformedEntity
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
return nil
|
|
|
|
}
|