mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
61d0427898
Signed-off-by: dusan <borovcanindusan1@gmail.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
|
|
apiutil "github.com/absmach/magistrala/api/http/util"
|
|
"github.com/absmach/magistrala/journal"
|
|
"github.com/absmach/magistrala/pkg/authn"
|
|
"github.com/absmach/magistrala/pkg/errors"
|
|
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
|
"github.com/go-kit/kit/endpoint"
|
|
)
|
|
|
|
func retrieveJournalsEndpoint(svc journal.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request any) (any, error) {
|
|
req := request.(retrieveJournalsReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
session, ok := ctx.Value(authn.SessionKey).(authn.Session)
|
|
if !ok {
|
|
return nil, svcerr.ErrAuthorization
|
|
}
|
|
|
|
page, err := svc.RetrieveAll(ctx, session, req.page)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pageRes{
|
|
JournalsPage: page,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func retrieveClientTelemetryEndpoint(svc journal.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request any) (any, error) {
|
|
req := request.(retrieveClientTelemetryReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
|
}
|
|
|
|
session, ok := ctx.Value(authn.SessionKey).(authn.Session)
|
|
if !ok {
|
|
return nil, svcerr.ErrAuthorization
|
|
}
|
|
|
|
telemetry, err := svc.RetrieveClientTelemetry(ctx, session, req.clientID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return clientTelemetryRes{
|
|
ClientTelemetry: telemetry,
|
|
}, nil
|
|
}
|
|
}
|