mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
![]() |
// Copyright (c) Mainflux
|
||
|
// SPDX-License-Identifier: Apache-2.0
|
||
|
|
||
|
package api
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/go-kit/kit/endpoint"
|
||
|
"github.com/mainflux/mainflux/certs"
|
||
|
)
|
||
|
|
||
|
func issueCert(svc certs.Service) endpoint.Endpoint {
|
||
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||
|
req := request.(addCertsReq)
|
||
|
if err := req.validate(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
res, err := svc.IssueCert(ctx, req.token, req.ThingID, req.Valid, req.KeyBits, req.KeyType)
|
||
|
if err != nil {
|
||
|
return certsResponse{Error: err.Error()}, nil
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func listCerts(svc certs.Service) endpoint.Endpoint {
|
||
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||
|
req := request.(listReq)
|
||
|
if err := req.validate(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
page, err := svc.ListCerts(ctx, req.token, req.offset, req.limit)
|
||
|
if err != nil {
|
||
|
return certsPageRes{
|
||
|
Error: err.Error(),
|
||
|
}, err
|
||
|
}
|
||
|
res := certsPageRes{
|
||
|
pageRes: pageRes{
|
||
|
Total: page.Total,
|
||
|
Offset: page.Offset,
|
||
|
Limit: page.Limit,
|
||
|
},
|
||
|
Certs: []certsResponse{},
|
||
|
}
|
||
|
|
||
|
for _, cert := range page.Certs {
|
||
|
view := certsResponse{
|
||
|
Serial: cert.Serial,
|
||
|
ThingID: cert.ThingID,
|
||
|
}
|
||
|
res.Certs = append(res.Certs, view)
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func revokeCert(svc certs.Service) endpoint.Endpoint {
|
||
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||
|
req := request.(revokeReq)
|
||
|
if err := req.validate(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return svc.RevokeCert(ctx, req.token, req.ThingID)
|
||
|
}
|
||
|
}
|