mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
243ccade0b
Signed-off-by: Felix Gateru <felix.gateru@gmail.com> Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com> Co-authored-by: Felix Gateru <felix.gateru@gmail.com>
230 lines
7.0 KiB
Go
230 lines
7.0 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
mgclients "github.com/absmach/magistrala/clients"
|
|
"github.com/absmach/magistrala/internal/api"
|
|
"github.com/absmach/magistrala/pkg/apiutil"
|
|
"github.com/absmach/magistrala/pkg/errors"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func decodeViewChannel(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := viewChannelReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeCreateChannelReq(_ 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 := createChannelReq{}
|
|
if err := json.NewDecoder(r.Body).Decode(&req.Channel); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeCreateChannelsReq(_ 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 := createChannelsReq{}
|
|
if err := json.NewDecoder(r.Body).Decode(&req.Channels); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeListChannels(_ 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 := mgclients.ToStatus(s)
|
|
if err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
req := listChannelsReq{
|
|
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 decodeUpdateChannel(_ 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 := updateChannelReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
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 decodeUpdateChannelTags(_ 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 := updateChannelTagsReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
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 decodeSetChannelParentGroupStatus(_ 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 := setChannelParentGroupReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
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 decodeRemoveChannelParentGroupStatus(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := removeChannelParentGroupReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChangeChannelStatus(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := changeChannelStatusReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeDeleteChannelReq(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := deleteChannelReq{
|
|
id: chi.URLParam(r, "channelID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeConnectChannelClientRequest(_ 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 := connectChannelClientsRequest{
|
|
channelID: chi.URLParam(r, "channelID"),
|
|
}
|
|
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 decodeDisconnectChannelClientsRequest(_ 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 := disconnectChannelClientsRequest{
|
|
channelID: chi.URLParam(r, "channelID"),
|
|
}
|
|
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 decodeConnectRequest(_ 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 := connectRequest{}
|
|
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 decodeDisconnectRequest(_ 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 := disconnectRequest{}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
|
|
}
|
|
|
|
return req, nil
|
|
}
|