mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
a0c40ba462
* chore(license): update copyright notices Add CI check for non go files to check that the files contain a license Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix(ci): log failed files When the CI fails during check for license header, log the failed file to console so that someone can check on the actual file. Also simplify the grep check to make it more human readable and understandable Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
187 lines
5.3 KiB
Go
187 lines
5.3 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/internal/api"
|
|
"github.com/absmach/magistrala/internal/apiutil"
|
|
mfclients "github.com/absmach/magistrala/pkg/clients"
|
|
"github.com/absmach/magistrala/pkg/errors"
|
|
"github.com/go-zoo/bone"
|
|
)
|
|
|
|
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: bone.GetValue(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: bone.GetValue(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: bone.GetValue(r, "domainID"),
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeDisableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := disableDomainReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: bone.GetValue(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: bone.GetValue(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 decodeUnassignUsersRequest(_ 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 := unassignUsersReq{
|
|
token: apiutil.ExtractBearerToken(r),
|
|
domainID: bone.GetValue(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: bone.GetValue(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 := mfclients.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
|
|
}
|