mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
f0d014eba2
Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
197 lines
5.6 KiB
Go
197 lines
5.6 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
api "github.com/absmach/supermq/api/http"
|
|
apiutil "github.com/absmach/supermq/api/http/util"
|
|
"github.com/absmach/supermq/clients"
|
|
"github.com/absmach/supermq/pkg/errors"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
const clientID = "clientID"
|
|
|
|
func decodeViewClient(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := viewClientReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeListClients(_ context.Context, r *http.Request) (interface{}, error) {
|
|
s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
n, err := apiutil.ReadStringQuery(r, api.NameKey, "")
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
t, err := apiutil.ReadStringQuery(r, api.TagKey, "")
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
id, err := apiutil.ReadStringQuery(r, api.IDOrder, "")
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
p, err := apiutil.ReadStringQuery(r, api.PermissionKey, api.DefPermission)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
lp, err := apiutil.ReadBoolQuery(r, api.ListPerms, api.DefListPerms)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
st, err := clients.ToStatus(s)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
req := listClientsReq{
|
|
status: st,
|
|
offset: o,
|
|
limit: l,
|
|
metadata: m,
|
|
name: n,
|
|
tag: t,
|
|
permission: p,
|
|
listPerms: lp,
|
|
userID: chi.URLParam(r, "userID"),
|
|
id: id,
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeUpdateClient(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
req := updateClientReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeUpdateClientTags(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
req := updateClientTagsReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
req := updateClientCredentialsReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeCreateClientReq(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
var c clients.Client
|
|
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
req := createClientReq{
|
|
client: c,
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeCreateClientsReq(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
c := createClientsReq{}
|
|
if err := json.NewDecoder(r.Body).Decode(&c.Clients); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func decodeChangeClientStatus(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := changeClientStatusReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeSetClientParentGroupStatus(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
|
}
|
|
|
|
req := setClientParentGroupReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeRemoveClientParentGroupStatus(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := removeClientParentGroupReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeDeleteClientReq(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := deleteClientReq{
|
|
id: chi.URLParam(r, clientID),
|
|
}
|
|
|
|
return req, nil
|
|
}
|