2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-11 07:00:10 +08:00
|
|
|
// Package http contains the domain concept definitions needed to support
|
|
|
|
// Mainflux http adapter service functionality.
|
2017-09-23 07:55:29 +08:00
|
|
|
package http
|
|
|
|
|
2019-07-18 21:01:09 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
)
|
2017-09-23 07:55:29 +08:00
|
|
|
|
2018-03-12 01:06:01 +08:00
|
|
|
var _ mainflux.MessagePublisher = (*adapterService)(nil)
|
2017-09-23 07:55:29 +08:00
|
|
|
|
|
|
|
type adapterService struct {
|
2019-07-18 21:01:09 +08:00
|
|
|
pub mainflux.MessagePublisher
|
|
|
|
things mainflux.ThingsServiceClient
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 07:00:10 +08:00
|
|
|
// New instantiates the HTTP adapter implementation.
|
2019-07-18 21:01:09 +08:00
|
|
|
func New(pub mainflux.MessagePublisher, things mainflux.ThingsServiceClient) mainflux.MessagePublisher {
|
|
|
|
return &adapterService{
|
|
|
|
pub: pub,
|
|
|
|
things: things,
|
|
|
|
}
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|
|
|
|
|
2019-11-05 18:57:16 +08:00
|
|
|
func (as *adapterService) Publish(ctx context.Context, token string, msg mainflux.Message) error {
|
2019-10-22 05:24:45 +08:00
|
|
|
ar := &mainflux.AccessByKeyReq{
|
2019-07-18 21:01:09 +08:00
|
|
|
Token: token,
|
|
|
|
ChanID: msg.GetChannel(),
|
|
|
|
}
|
2019-10-22 05:24:45 +08:00
|
|
|
thid, err := as.things.CanAccessByKey(ctx, ar)
|
2019-07-18 21:01:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.Publisher = thid.GetValue()
|
|
|
|
|
|
|
|
return as.pub.Publish(ctx, token, msg)
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|