Skip to content

Commit e0c1358

Browse files
committed
Refactor: ws
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
1 parent ec0120c commit e0c1358

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

ws/adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Service interface {
3535
// Subscribe subscribes message from the broker using the thingKey for authorization,
3636
// and the channelID for subscription. Subtopic is optional.
3737
// If the subscription is successful, nil is returned otherwise error is returned.
38-
Subscribe(ctx context.Context, thingKey, chanID, subtopic string, client *Client) error
38+
Subscribe(ctx context.Context, thingKey, chanID, subtopic string, client *Client) errors.Error
3939
}
4040

4141
var _ Service = (*adapterService)(nil)
@@ -53,7 +53,7 @@ func New(authClient magistrala.AuthzServiceClient, pubsub messaging.PubSub) Serv
5353
}
5454
}
5555

56-
func (svc *adapterService) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *Client) error {
56+
func (svc *adapterService) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *Client) errors.Error {
5757
if chanID == "" || thingKey == "" {
5858
return svcerr.ErrAuthentication
5959
}
@@ -84,7 +84,7 @@ func (svc *adapterService) Subscribe(ctx context.Context, thingKey, chanID, subt
8484

8585
// authorize checks if the thingKey is authorized to access the channel
8686
// and returns the thingID if it is.
87-
func (svc *adapterService) authorize(ctx context.Context, thingKey, chanID, action string) (string, error) {
87+
func (svc *adapterService) authorize(ctx context.Context, thingKey, chanID, action string) (string, errors.Error) {
8888
ar := &magistrala.AuthorizeReq{
8989
SubjectType: auth.ThingType,
9090
Permission: action,

ws/api/logging.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"time"
1010

11+
"github.com/absmach/magistrala/pkg/errors"
1112
"github.com/absmach/magistrala/ws"
1213
)
1314

@@ -25,7 +26,7 @@ func LoggingMiddleware(svc ws.Service, logger *slog.Logger) ws.Service {
2526

2627
// Subscribe logs the subscribe request. It logs the channel and subtopic(if present) and the time it took to complete the request.
2728
// If the request fails, it logs the error.
28-
func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) (err error) {
29+
func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) (err errors.Error) {
2930
defer func(begin time.Time) {
3031
args := []any{
3132
slog.String("duration", time.Since(begin).String()),

ws/api/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"time"
1111

12+
"github.com/absmach/magistrala/pkg/errors"
1213
"github.com/absmach/magistrala/ws"
1314
"github.com/go-kit/kit/metrics"
1415
)
@@ -31,7 +32,7 @@ func MetricsMiddleware(svc ws.Service, counter metrics.Counter, latency metrics.
3132
}
3233

3334
// Subscribe instruments Subscribe method with metrics.
34-
func (mm *metricsMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) error {
35+
func (mm *metricsMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) errors.Error {
3536
defer func(begin time.Time) {
3637
mm.counter.With("method", "subscribe").Add(1)
3738
mm.latency.With("method", "subscribe").Observe(time.Since(begin).Seconds())

ws/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package ws
55

66
import (
7+
"github.com/absmach/magistrala/pkg/errors"
78
"github.com/absmach/magistrala/pkg/messaging"
89
"github.com/gorilla/websocket"
910
)
@@ -23,19 +24,19 @@ func NewClient(c *websocket.Conn) *Client {
2324
}
2425

2526
// Cancel handles the websocket connection after unsubscribing.
26-
func (c *Client) Cancel() error {
27+
func (c *Client) Cancel() errors.Error {
2728
if c.conn == nil {
2829
return nil
2930
}
30-
return c.conn.Close()
31+
return errors.Cast(c.conn.Close())
3132
}
3233

3334
// Handle handles the sending and receiving of messages via the broker.
34-
func (c *Client) Handle(msg *messaging.Message) error {
35+
func (c *Client) Handle(msg *messaging.Message) errors.Error {
3536
// To prevent publisher from receiving its own published message
3637
if msg.GetPublisher() == c.id {
3738
return nil
3839
}
3940

40-
return c.conn.WriteMessage(websocket.TextMessage, msg.GetPayload())
41+
return errors.Cast(c.conn.WriteMessage(websocket.TextMessage, msg.GetPayload()))
4142
}

ws/tracing/tracing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tracing
66
import (
77
"context"
88

9+
"github.com/absmach/magistrala/pkg/errors"
910
"github.com/absmach/magistrala/ws"
1011
"go.opentelemetry.io/otel/trace"
1112
)
@@ -32,7 +33,7 @@ func New(tracer trace.Tracer, svc ws.Service) ws.Service {
3233
}
3334

3435
// Subscribe traces the "Subscribe" operation of the wrapped ws.Service.
35-
func (tm *tracingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, client *ws.Client) error {
36+
func (tm *tracingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, client *ws.Client) errors.Error {
3637
ctx, span := tm.tracer.Start(ctx, subscribeOP)
3738
defer span.End()
3839

0 commit comments

Comments
 (0)