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"
|
2020-06-03 21:16:19 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
2019-07-18 21:01:09 +08:00
|
|
|
)
|
2017-09-23 07:55:29 +08:00
|
|
|
|
2020-04-02 03:22:13 +08:00
|
|
|
// Service specifies coap service API.
|
|
|
|
type Service interface {
|
|
|
|
// Publish Messssage
|
2020-04-28 17:02:35 +08:00
|
|
|
Publish(ctx context.Context, token string, msg messaging.Message) error
|
2020-04-02 03:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Service = (*adapterService)(nil)
|
2017-09-23 07:55:29 +08:00
|
|
|
|
|
|
|
type adapterService struct {
|
2020-04-28 17:02:35 +08:00
|
|
|
publisher messaging.Publisher
|
|
|
|
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.
|
2020-04-28 17:02:35 +08:00
|
|
|
func New(publisher messaging.Publisher, things mainflux.ThingsServiceClient) Service {
|
2019-07-18 21:01:09 +08:00
|
|
|
return &adapterService{
|
2020-04-28 17:02:35 +08:00
|
|
|
publisher: publisher,
|
|
|
|
things: things,
|
2019-07-18 21:01:09 +08:00
|
|
|
}
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:02:35 +08:00
|
|
|
func (as *adapterService) Publish(ctx context.Context, token string, msg messaging.Message) error {
|
2019-10-22 05:24:45 +08:00
|
|
|
ar := &mainflux.AccessByKeyReq{
|
2019-07-18 21:01:09 +08:00
|
|
|
Token: token,
|
2020-04-28 17:02:35 +08:00
|
|
|
ChanID: msg.Channel,
|
2019-07-18 21:01:09 +08:00
|
|
|
}
|
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()
|
|
|
|
|
2020-04-28 17:02:35 +08:00
|
|
|
return as.publisher.Publish(msg.Channel, msg)
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|