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