mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 07:20:19 +00:00
4e2ae73fb0
Signed-off-by: 1998-felix <felix.gateru@gmail.com>
202 lines
5.7 KiB
Go
202 lines
5.7 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package domains
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/absmach/magistrala/auth"
|
|
"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 decodeCreateDomainRequest(_ 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 := createDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeRetrieveDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := retrieveDomainRequest{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeRetrieveDomainPermissionsRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := retrieveDomainPermissionsRequest{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeUpdateDomainRequest(_ 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 := updateDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeListDomainRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
page, err := decodePageRequest(ctx, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := listDomainsReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
page: page,
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeEnableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := enableDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeDisableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := disableDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeFreezeDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := freezeDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeAssignUsersRequest(_ 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 := assignUsersReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeUnassignUserRequest(_ 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 := unassignUserReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: chi.URLParam(r, "domainID"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeListUserDomainsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
page, err := decodePageRequest(ctx, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := listUserDomainsReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
userID: chi.URLParam(r, "userID"),
|
|
page: page,
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodePageRequest(_ context.Context, r *http.Request) (page, error) {
|
|
s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
st, err := auth.ToStatus(s)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
or, err := apiutil.ReadStringQuery(r, api.OrderKey, api.DefOrder)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DefDir)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil)
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
n, err := apiutil.ReadStringQuery(r, api.NameKey, "")
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
t, err := apiutil.ReadStringQuery(r, api.TagKey, "")
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
p, err := apiutil.ReadStringQuery(r, api.PermissionKey, "")
|
|
if err != nil {
|
|
return page{}, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
return page{
|
|
offset: o,
|
|
order: or,
|
|
dir: dir,
|
|
limit: l,
|
|
name: n,
|
|
metadata: m,
|
|
tag: t,
|
|
permission: p,
|
|
status: st,
|
|
}, nil
|
|
}
|