2018-08-26 19:15:48 +08:00
|
|
|
//
|
2019-07-18 21:01:09 +08:00
|
|
|
// Copyright (c) 2019
|
2018-08-26 19:15:48 +08:00
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// 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-07-18 21:01:09 +08:00
|
|
|
func (as *adapterService) Publish(ctx context.Context, token string, msg mainflux.RawMessage) error {
|
|
|
|
ar := &mainflux.AccessReq{
|
|
|
|
Token: token,
|
|
|
|
ChanID: msg.GetChannel(),
|
|
|
|
}
|
|
|
|
thid, err := as.things.CanAccess(ctx, ar)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.Publisher = thid.GetValue()
|
|
|
|
|
|
|
|
return as.pub.Publish(ctx, token, msg)
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|