Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add support for publishing binary data #41

Merged
merged 1 commit into from
Nov 15, 2023
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
34 changes: 33 additions & 1 deletion Sources/momento/TopicClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,39 @@ public class TopicClient: TopicClientProtocol {
)
}
}


public func publish(
cacheName: String,
topicName: String,
value: Data
) async -> TopicPublishResponse {
if cacheName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a cache name")
)
}
if topicName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a topic name")
)
}
do {
let result = try await self.pubsubClient.publish(
cacheName: cacheName,
topicName: topicName,
value: value
)
return result
} catch {
return TopicPublishError(
error: UnknownError(
message: "Unknown error from publish",
innerException: error
)
)
}
}

public func subscribe(cacheName: String, topicName: String) async -> TopicSubscribeResponse {
if cacheName.count < 1 {
return TopicSubscribeError(
Expand Down
25 changes: 24 additions & 1 deletion Sources/momento/internal/PubsubClient.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import GRPC
import NIO
import NIOHPACK
Expand Down Expand Up @@ -50,7 +51,13 @@ protocol PubsubClientProtocol {
topicName: String,
value: String
) async throws -> TopicPublishResponse


func publish(
cacheName: String,
topicName: String,
value: Data
) async throws -> TopicPublishResponse

func subscribe(
cacheName: String,
topicName: String
Expand Down Expand Up @@ -117,6 +124,22 @@ class PubsubClient: PubsubClientProtocol {
request.cacheName = cacheName
request.topic = topicName
request.value.text = value
return await self.doPublish(request: request)
}

func publish(
cacheName: String,
topicName: String,
value: Data
) async -> TopicPublishResponse {
var request = CacheClient_Pubsub__PublishRequest()
request.cacheName = cacheName
request.topic = topicName
request.value.binary = value
return await doPublish(request: request)
}

private func doPublish(request: CacheClient_Pubsub__PublishRequest) async -> TopicPublishResponse {
do {
let result = try await self.client.publish(request)
// Successful publish returns client_sdk_swift.CacheClient_Pubsub__Empty
Expand Down
48 changes: 46 additions & 2 deletions Tests/client-sdk-swiftTests/client_sdk_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ final class client_sdk_swiftTests: XCTestCase {
pubResp is TopicPublishSuccess,
"Unexpected response: \((pubResp as! TopicPublishError).description)"
)
try await Task.sleep(nanoseconds: 1000)

let subscription = (subResp as! TopicSubscribeSuccess).subscription
for try await item in subscription {
Expand All @@ -141,7 +140,52 @@ final class client_sdk_swiftTests: XCTestCase {
XCTAssertEqual(value, "publishing and subscribing!", "unexpected topic subscription item value: \(value)")
break
}

}

func testTopicClientPublishesAndSubscribesBinary() async throws {
let creds = try CredentialProvider.fromEnvironmentVariable(envVariableName: "MOMENTO_API_KEY")
let client = TopicClient(configuration: TopicConfigurations.Default.latest(), credentialProvider: creds)
XCTAssertNotNil(client)

let subResp = await client.subscribe(
cacheName: "test-cache",
topicName: "test-topic"
)
XCTAssertTrue(
subResp is TopicSubscribeSuccess,
"Unexpected response: \((subResp as! TopicSubscribeError).description)"
)

try await Task.sleep(nanoseconds: 1000)
let binaryValue = "publishing and subscribing!".data(using: .utf8)!
let pubResp = await client.publish(
cacheName: "test-cache",
topicName: "test-topic",
value: binaryValue
)
XCTAssertTrue(
pubResp is TopicPublishSuccess,
"Unexpected response: \((pubResp as! TopicPublishError).description)"
)

let subscription = (subResp as! TopicSubscribeSuccess).subscription
for try await item in subscription {
print("Received item: \(String(describing: item))")
XCTAssertTrue(
item is TopicSubscriptionItemBinary,
"received subscription item that was not binary: \(String(describing: item))"
)

let value = (item as! TopicSubscriptionItemBinary).value
print("Received value: \(String(decoding: value, as: UTF8.self))")
XCTAssertEqual(
value,
binaryValue,
"unexpected topic subscription item value: \(value)"
)
break
}

client.close()
print("closed subscription")
}
Expand Down