Skip to content

Commit

Permalink
as: Set pagination values in PubSub and Webhooks List RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
halimi committed Feb 26, 2025
1 parent 5853e43 commit 7593884
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 14 additions & 0 deletions pkg/applicationserver/io/pubsub/grpc_pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ package pubsub

import (
"context"
"strconv"

"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/events"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
)

func setTotalHeader(ctx context.Context, total int64) {
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatInt(total, 10))) // nolint: errcheck
}

// appendImplicitPubSubGetPaths appends implicit ttnpb.ApplicationPubSub get paths to paths.
func appendImplicitPubSubGetPaths(paths ...string) []string {
return append(append(make([]string, 0, 3+len(paths)),
Expand Down Expand Up @@ -66,13 +73,20 @@ func (ps *PubSub) List(ctx context.Context, req *ttnpb.ListApplicationPubSubsReq
if err := rights.RequireApplication(ctx, req.ApplicationIds, ttnpb.Right_RIGHT_APPLICATION_TRAFFIC_READ); err != nil {
return nil, err
}
var total int64
ctx = ps.registry.WithPagination(ctx, req.Limit, req.Page, &total)
pubsubs, err := ps.registry.List(ctx, req.ApplicationIds, appendImplicitPubSubGetPaths(req.FieldMask.GetPaths()...))
if err != nil {
return nil, err
}
for _, pubsub := range pubsubs {
_ = ps.providerStatuses.Enabled(ctx, pubsub.Provider)
}
defer func() {
if err == nil {
setTotalHeader(ctx, total)
}
}()
return &ttnpb.ApplicationPubSubs{
Pubsubs: pubsubs,
}, nil
Expand Down
8 changes: 5 additions & 3 deletions pkg/applicationserver/io/web/grpc_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)

func setTotalHeader(ctx context.Context, total uint64) {
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatUint(total, 10)))
func setTotalHeader(ctx context.Context, total int64) {
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatInt(total, 10))) // nolint: errcheck
}

// appendImplicitWebhookGetPaths appends implicit ttnpb.ApplicationWebhook get paths to paths.
Expand Down Expand Up @@ -81,13 +81,15 @@ func (s webhookRegistryRPC) List(ctx context.Context, req *ttnpb.ListApplication
if err := rights.RequireApplication(ctx, req.ApplicationIds, ttnpb.Right_RIGHT_APPLICATION_TRAFFIC_READ); err != nil {
return nil, err
}
var total int64
ctx = s.webhooks.WithPagination(ctx, req.Limit, req.Page, &total)
webhooks, err := s.webhooks.List(ctx, req.ApplicationIds, appendImplicitWebhookGetPaths(req.FieldMask.GetPaths()...))
if err != nil {
return nil, err
}
defer func() {
if err == nil {
setTotalHeader(ctx, uint64(len(webhooks)))
setTotalHeader(ctx, total)
}
}()
return &ttnpb.ApplicationWebhooks{
Expand Down

0 comments on commit 7593884

Please sign in to comment.