Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ To consume messages, you Pull them, process them, and `Ack` (acknowledge) them.
topicID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
publisher := pubsub.NewPublisher(topicID, pubsub.WithHTTPRoundTripper(roundTripper))

messageIDs, err := publisher.PublishStrings(ctx, "Hello, PubSub!")
err := publisher.PublishStrings(ctx, "Hello, PubSub!")

// Pull
subscriptionID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
Expand Down Expand Up @@ -112,7 +112,7 @@ The SDK returns specific error types to help you handle different failure scenar

```go
// Example of detailed error handling
_, err := publisher.PublishStrings(ctx, "Hello, PubSub!")
err := publisher.PublishStrings(ctx, "Hello, PubSub!")
if err != nil {
var apiErr *pubsub.APIError
if errors.As(err, &apiErr) {
Expand Down
23 changes: 2 additions & 21 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# For details see https://spec.openapis.org/oas/v3.0.3.html
openapi: 3.0.3
info:
version: 1alpha.0.0
version: 1alpha.1.0
title: STACKIT PubSub Data Plane API
description: |
This API provides endpoints for managing PubSubs.
Expand All @@ -42,12 +42,8 @@ paths:
schema:
$ref: '#/components/schemas/publishMessageToTopicRequest'
responses:
'200':
'204':
description: Messages have been published.
content:
application/json:
schema:
$ref: '#/components/schemas/publishMessageResponse'
'400':
$ref: '#/components/responses/badRequest'
'401':
Expand Down Expand Up @@ -245,16 +241,6 @@ components:
items:
$ref: '#/components/schemas/publishMessage'
description: The messages to publish.
publishMessageResponse:
required:
- messageIds
properties:
messageIds:
type: array
items:
type: integer
format: uint64
description: A list of unique, increasing sequence numbers identifying the messages.
messages:
required:
- messages
Expand All @@ -265,16 +251,11 @@ components:
$ref: '#/components/schemas/message'
message:
required:
- id
- ackId
- data
- createTime
- deliveryAttempts
properties:
id:
type: integer
format: uint64
description: A unique, increasing sequence number identifying the message within the topic.
ackId:
type: string
description: An auto generated unique id for acknowledging the message.
Expand Down
2 changes: 1 addition & 1 deletion example/example_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func publish() {
)

// Publish the messages to the topic using the publisher client
_, err = publisher.PublishStrings(
err = publisher.PublishStrings(
context.Background(),
"Hello PubSub from example", "This is another message",
)
Expand Down
19 changes: 0 additions & 19 deletions pkg/pubsub/api/sdk.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/pubsub/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

type PullMessage struct {
subscription *Subscriber
ID uint64
AckID string
Data []byte
CreateTime time.Time
Expand Down
13 changes: 6 additions & 7 deletions pkg/pubsub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewPublisher(topicID uuid.UUID, opts ...Option) *Publisher {

// PublishStrings acts as a lightweight adapter converting string slice to byte slices,
// deferring all encoding safely to the core Publish method.
func (p *Publisher) PublishStrings(ctx context.Context, messages ...string) ([]uint64, error) {
func (p *Publisher) PublishStrings(ctx context.Context, messages ...string) error {
byteMessages := make([][]byte, len(messages))
for i, msg := range messages {
byteMessages[i] = []byte(msg)
Expand All @@ -78,7 +78,7 @@ func (p *Publisher) PublishStrings(ctx context.Context, messages ...string) ([]u

// Publish processes raw bytes, encodes them transparently using bytesToBase64,
// and transmits them out via the API client.
func (p *Publisher) Publish(ctx context.Context, messages [][]byte) ([]uint64, error) {
func (p *Publisher) Publish(ctx context.Context, messages [][]byte) error {
encodedMessages := bytesToBase64(messages...)

messagesToPublish := make([]api.PublishMessage, len(encodedMessages))
Expand All @@ -95,20 +95,19 @@ func (p *Publisher) Publish(ctx context.Context, messages [][]byte) ([]uint64, e
p.logger.V(4).Info("publishing messages", "count", len(messages))
resp, err := p.dataplane.PublishMessagesWithResponse(ctx, reqBody)
if err != nil {
return nil, NewNetworkError("failed to execute publish messages request", err)
return NewNetworkError("failed to execute publish messages request", err)
}

if resp.StatusCode() != http.StatusOK {
return nil, NewAPIError(resp.StatusCode(), resp.Body)
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNoContent { // StatusOK is still there for backward compatibility
return NewAPIError(resp.StatusCode(), resp.Body)
}

p.logger.V(4).Info(
"published messages",
"count", len(messages),
"message_ids", resp.JSON200.MessageIds,
"status_code", resp.StatusCode(),
)
return resp.JSON200.MessageIds, nil
return nil
}

// Purge removes all messages currently stored in the topic.
Expand Down
6 changes: 2 additions & 4 deletions pkg/pubsub/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ var _ = Describe("publish a message", func() {
It("should publish the message and return a message ID", func(ctx context.Context) {
publisher := pubsub.NewPublisher(topicId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))

messageIDs, err := publisher.PublishStrings(ctx, "Hello, Stackit!")
err := publisher.PublishStrings(ctx, "Hello, Stackit!")

Expect(err).ToNot(HaveOccurred())
Expect(messageIDs).ToNot(BeNil())
Expect(messageIDs).To(HaveLen(1))
})
})

Expand All @@ -42,7 +40,7 @@ var _ = Describe("publish a message", func() {
publisher := pubsub.NewPublisher(topicId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))
subscriber := pubsub.NewSubscriber(topicId, subscriptionId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))

_, err := publisher.PublishStrings(ctx, "message-to-be-purged-1", "message-to-be-purged-2")
err := publisher.PublishStrings(ctx, "message-to-be-purged-1", "message-to-be-purged-2")
Expect(err).ToNot(HaveOccurred())

Eventually(func(g Gomega) {
Expand Down
1 change: 0 additions & 1 deletion pkg/pubsub/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func toSDKMessages(m []api.Message, subscription *Subscriber) PullMessages {
CreateTime: msg.CreateTime,
Data: msg.Data,
DeliveryAttempts: msg.DeliveryAttempts,
ID: msg.Id,
}
}
return sdkMessages
Expand Down
6 changes: 3 additions & 3 deletions pkg/pubsub/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("Acknowledge messages", func() {
BeforeEach(func(ctx context.Context) {
publisher := pubsub.NewPublisher(topicId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))

_, err := publisher.PublishStrings(ctx, "test1")
err := publisher.PublishStrings(ctx, "test1")
Expect(err).ToNot(HaveOccurred())

subscriber := pubsub.NewSubscriber(topicId, subscriptionId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))
Expand Down Expand Up @@ -110,7 +110,7 @@ var _ = Describe("Pull messages", func() {
BeforeEach(func(ctx context.Context) {
publisher := pubsub.NewPublisher(topicId, pubsub.WithHTTPRoundTripper(rt), pubsub.WithHost(environment))

_, err := publisher.PublishStrings(ctx, "test1", "test2", "test3")
err := publisher.PublishStrings(ctx, "test1", "test2", "test3")
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -148,7 +148,7 @@ var _ = Describe("PullJob", func() {
time.Sleep(1 * time.Second) // wait for topic to be purged

// publishing test messages
_, err = publisher.PublishStrings(ctx, "testMessage")
err = publisher.PublishStrings(ctx, "testMessage")
Expect(err).ToNot(HaveOccurred())
})

Expand Down
Loading