Skip to content

Commit

Permalink
Merge pull request #7521 from TheThingsNetwork/feature/pubsub-webhook…
Browse files Browse the repository at this point in the history
…-list-pagination

Add limit and page fields to PubSub and Webhooks List RPC
  • Loading branch information
halimi authored Mar 3, 2025
2 parents 7c8f1fe + 7593884 commit 22b921c
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 176 deletions.
6 changes: 6 additions & 0 deletions api/ttn/lorawan/v3/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2030,12 +2030,15 @@ The NATS provider settings.
| ----- | ---- | ----- | ----------- |
| `application_ids` | [`ApplicationIdentifiers`](#ttn.lorawan.v3.ApplicationIdentifiers) | | |
| `field_mask` | [`google.protobuf.FieldMask`](#google.protobuf.FieldMask) | | |
| `limit` | [`uint32`](#uint32) | | |
| `page` | [`uint32`](#uint32) | | |

#### Field Rules

| Field | Validations |
| ----- | ----------- |
| `application_ids` | <p>`message.required`: `true`</p> |
| `limit` | <p>`uint32.lte`: `1000`</p> |

### <a name="ttn.lorawan.v3.SetApplicationPubSubRequest">Message `SetApplicationPubSubRequest`</a>

Expand Down Expand Up @@ -2342,12 +2345,15 @@ The fields are meant to be replaced inside the URLs and headers when the webhook
| ----- | ---- | ----- | ----------- |
| `application_ids` | [`ApplicationIdentifiers`](#ttn.lorawan.v3.ApplicationIdentifiers) | | |
| `field_mask` | [`google.protobuf.FieldMask`](#google.protobuf.FieldMask) | | |
| `limit` | [`uint32`](#uint32) | | |
| `page` | [`uint32`](#uint32) | | |

#### Field Rules

| Field | Validations |
| ----- | ----------- |
| `application_ids` | <p>`message.required`: `true`</p> |
| `limit` | <p>`uint32.lte`: `1000`</p> |

### <a name="ttn.lorawan.v3.SetApplicationWebhookRequest">Message `SetApplicationWebhookRequest`</a>

Expand Down
28 changes: 28 additions & 0 deletions api/ttn/lorawan/v3/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3420,6 +3420,20 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "limit",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "page",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
}
],
"tags": [
Expand Down Expand Up @@ -3705,6 +3719,20 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "limit",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "page",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
}
],
"tags": [
Expand Down
2 changes: 2 additions & 0 deletions api/ttn/lorawan/v3/applicationserver_pubsub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ message ListApplicationPubSubsRequest {
};
ApplicationIdentifiers application_ids = 1 [(validate.rules).message.required = true];
google.protobuf.FieldMask field_mask = 2;
uint32 limit = 3 [(validate.rules).uint32.lte = 1000];
uint32 page = 4;
}

message SetApplicationPubSubRequest {
Expand Down
2 changes: 2 additions & 0 deletions api/ttn/lorawan/v3/applicationserver_web.proto
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ message GetApplicationWebhookRequest {
message ListApplicationWebhooksRequest {
ApplicationIdentifiers application_ids = 1 [(validate.rules).message.required = true];
google.protobuf.FieldMask field_mask = 2;
uint32 limit = 3 [(validate.rules).uint32.lte = 1000];
uint32 page = 4;
}

message SetApplicationWebhookRequest {
Expand Down
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
Loading

0 comments on commit 22b921c

Please sign in to comment.