Files
supermq/re/api/endpoints.go
T
Dušan Borovčanin 57c3ecb175 MG-13 - Magistrala Rules engine (#16)
* Add initial Rules Engine model

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* WIP - Add API layer

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Add async consumer

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Use Named queries and single topics

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Update rules listing

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Enable consumers with no transformer

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Move RE to addons

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Fix HTTP server host

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Remove unused code

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

* Remove cache for the time being

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>

---------

Signed-off-by: Dusan Borovcanin <borovcanindusan1@gmail.com>
2024-12-27 20:00:50 +01:00

98 lines
2.5 KiB
Go

// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package api
import (
"context"
"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/pkg/authn"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/re"
"github.com/go-kit/kit/endpoint"
)
func addRuleEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}
req := request.(addRuleReq)
rule, err := s.AddRule(ctx, session, req.Rule)
if err != nil {
return addRuleRes{}, err
}
return addRuleRes{Rule: rule, created: true}, nil
}
}
func viewRuleEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}
req := request.(viewRuleReq)
rule, err := s.ViewRule(ctx, session, req.id)
if err != nil {
return viewRuleRes{}, err
}
return viewRuleRes{Rule: rule}, nil
}
}
func updateRuleEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}
req := request.(updateRuleReq)
rule, err := s.UpdateRule(ctx, session, req.Rule)
if err != nil {
return updateRuleRes{}, err
}
return updateRuleRes{Rule: rule}, nil
}
}
func listRulesEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}
req := request.(listRulesReq)
page, err := s.ListRules(ctx, session, req.PageMeta)
if err != nil {
return rulesPageRes{}, nil
}
ret := rulesPageRes{
Rules: page.Rules,
}
return ret, nil
}
}
func upadateRuleStatusEndpoint(s re.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
session, ok := ctx.Value(api.SessionKey).(authn.Session)
if !ok {
return nil, svcerr.ErrAuthorization
}
req := request.(changeRuleStatusReq)
err := s.RemoveRule(ctx, session, req.id)
if err != nil {
return changeRoleStatusRes{false}, err
}
return changeRoleStatusRes{true}, nil
}
}