Skip to content

Commit e36240e

Browse files
authored
Merge pull request #43 from owainhunt/pubsub-topic
Optionally pass in project for PubSub calls
2 parents da674ff + b119c63 commit e36240e

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

PubSub/Sources/API/SubscriptionsAPI.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ public protocol SubscriptionsAPI {
1414
/// Gets the configuration details of a subscription.
1515
///
1616
/// - parameter `subscriptionId`: The name of the subscription to get
17+
/// `subscriptionProject`: Name of the project that owns the subscription. If not provided, the default project will be used.
1718
/// - returns: An instance of the `Subscription`
18-
func get(subscriptionId: String) -> EventLoopFuture<GoogleCloudPubSubSubscription>
19+
func get(subscriptionId: String, subscriptionProject: String?) -> EventLoopFuture<GoogleCloudPubSubSubscription>
1920

2021
/// Acknowledges the messages associated with the ackIds in the AcknowledgeRequest.
2122
///
2223
/// - parameters `subscriptionId`: ID of the subscription whose message is being acknowledged
24+
/// `subscriptionProject`: Name of the project that owns the subscription. If not provided, the default project will be used.
2325
/// `ackIds`: The acknowledgment ID for the messages being acknowledged that was returned by the Pub/Sub system in the subscriptions.pull response. Must not be empty.
24-
func acknowledge(subscriptionId: String, ackIds: [String]) -> EventLoopFuture<EmptyResponse>
26+
func acknowledge(subscriptionId: String, subscriptionProject: String?, ackIds: [String]) -> EventLoopFuture<EmptyResponse>
2527

2628
/// Creates a subscription to a given topic.
2729
/// - parameter `subscriptionId`: The name of the subscription to be created.
30+
/// `subscriptionProject`: Name of the project that owns the subscription. If not provided, the default project will be used.
2831
/// `topicId`: The name of the topic from which this subscription is receiving messages.
32+
/// `topicProject`: Name of the project that owns the topic. If not provided, the default project will be used.
2933
/// `pushEndpoint`: A URL locating the endpoint to which messages should be pushed.
3034
/// `pushConfigAttributes`: Endpoint configuration attributes that can be used to control different aspects of the message delivery.
3135
/// `pushConfigOidcTokenServiceAccountEmail`:Service account email to be used for generating the OIDC token.
@@ -46,7 +50,9 @@ public protocol SubscriptionsAPI {
4650
/// - returns: If successful, the response body contains a newly created instance of Subscription.
4751
/// If the subscription already exists, returns ALREADY_EXISTS. If the corresponding topic doesn't exist, returns NOT_FOUND.
4852
func create(subscriptionId: String,
53+
subscriptionProject: String?,
4954
topicId: String,
55+
topicProject: String?,
5056
pushEndpoint: String?,
5157
pushConfigAttributes: [String: String]?,
5258
pushConfigOidcTokenServiceAccountEmail: String?,
@@ -75,24 +81,26 @@ public final class GoogleCloudPubSubSubscriptionsAPI: SubscriptionsAPI {
7581
self.endpoint = endpoint
7682
}
7783

78-
public func get(subscriptionId: String) -> EventLoopFuture<GoogleCloudPubSubSubscription> {
79-
return request.send(method: .GET, path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId)")
84+
public func get(subscriptionId: String, subscriptionProject: String? = nil) -> EventLoopFuture<GoogleCloudPubSubSubscription> {
85+
return request.send(method: .GET, path: "\(endpoint)/v1/projects/\(subscriptionProject ?? request.project)/subscriptions/\(subscriptionId)")
8086
}
8187

82-
public func acknowledge(subscriptionId: String, ackIds: [String]) -> EventLoopFuture<EmptyResponse> {
88+
public func acknowledge(subscriptionId: String, subscriptionProject: String? = nil, ackIds: [String]) -> EventLoopFuture<EmptyResponse> {
8389
do {
8490
let acks = AcknowledgeRequest(ackIds: ackIds)
8591
let body = try HTTPClient.Body.data(encoder.encode(acks))
8692
return request.send(method: .POST,
87-
path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId):acknowledge",
93+
path: "\(endpoint)/v1/projects/\(subscriptionProject ?? request.project)/subscriptions/\(subscriptionId):acknowledge",
8894
body: body)
8995
} catch {
9096
return request.eventLoop.makeFailedFuture(error)
9197
}
9298
}
9399

94100
public func create(subscriptionId: String,
101+
subscriptionProject: String? = nil,
95102
topicId: String,
103+
topicProject: String? = nil,
96104
pushEndpoint: String?,
97105
pushConfigAttributes: [String: String]?,
98106
pushConfigOidcTokenServiceAccountEmail: String?,
@@ -140,7 +148,7 @@ public final class GoogleCloudPubSubSubscriptionsAPI: SubscriptionsAPI {
140148
}
141149

142150
let subscription = GoogleCloudPubSubSubscription(name: subscriptionId,
143-
topic: "projects/\(request.project)/topics/\(topicId)",
151+
topic: "projects/\(topicProject ?? request.project)/topics/\(topicId)",
144152
pushConfig: pushConfig,
145153
ackDeadlineSeconds: ackDeadlineSeconds,
146154
retainAckedMessages: retainAckedMessages,
@@ -154,7 +162,7 @@ public final class GoogleCloudPubSubSubscriptionsAPI: SubscriptionsAPI {
154162
detached: detached)
155163
let body = try HTTPClient.Body.data(encoder.encode(subscription))
156164
return request.send(method: .PUT,
157-
path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId)",
165+
path: "\(endpoint)/v1/projects/\(subscriptionProject ?? request.project)/subscriptions/\(subscriptionId)",
158166
body: body)
159167
} catch {
160168
return request.eventLoop.makeFailedFuture(error)

PubSub/Sources/API/TopicsAPI.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@ public protocol TopicsAPI {
77
/// Gets the configuration of a topic.
88
///
99
/// - parameter `topicId`: Name of the topic
10+
/// `topicProject`: Name of the project that owns the topic. If not provided, the default project will be used.
1011
/// - returns: If successful, the response body contains an instance of `Topic`.
11-
func get(topicId: String) -> EventLoopFuture<GoogleCloudPubSubTopic>
12+
func get(topicId: String, topicProject: String?) -> EventLoopFuture<GoogleCloudPubSubTopic>
1213

1314
/// Lists matching topics.
1415
///
1516
/// - parameter `pageSize`: Maximum number of topics to return.
1617
/// `pageToken`: The value returned by the last ListTopicsResponse; indicates that this is a
1718
/// continuation of a prior topics.list call, and that the system should return the next page of data
19+
/// `topicProject`: Name of the project that owns the topic. If not provided, the default project will be used.
1820
/// - returns: Returns a list of topics and the `nextPageToken`
19-
func list(pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubListTopicResponse>
21+
func list(pageSize: Int?, pageToken: String?, topicProject: String?) -> EventLoopFuture<GooglePubSubListTopicResponse>
2022

2123
/// Adds one or more messages to the topic.
2224
///
23-
/// - parameter `topidId`: Name of the topic
25+
/// - parameter `topicId`: Name of the topic
26+
/// `topicProject`: Name of the project that owns the topic. If not provided, the default project will be used.
2427
/// `data`: Data to be passed in the message
2528
/// `attributes`: Attributes for this message
2629
/// `orderingKey`: Identifies related messages for which publish order should be respected
2730
/// - returns: Returns an array of `messageId`. `MessageId` is the server-assigned ID of each published message, in the same order as the messages in the request. IDs are guaranteed to be unique within the topic.
28-
func publish(topicId: String, data: String, attributes: [String: String]?, orderingKey: String?) -> EventLoopFuture<GoogleCloudPublishResponse>
31+
func publish(topicId: String, topicProject: String?, data: String, attributes: [String: String]?, orderingKey: String?) -> EventLoopFuture<GoogleCloudPublishResponse>
2932

3033
/// Lists the names of the attached subscriptions on this topic.
31-
func getSubscriptionsList(topicId: String, pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubTopicSubscriptionListResponse>
34+
func getSubscriptionsList(topicId: String, topicProject: String?, pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubTopicSubscriptionListResponse>
3235
}
3336

3437
public final class GoogleCloudPubSubTopicsAPI: TopicsAPI {
@@ -42,27 +45,27 @@ public final class GoogleCloudPubSubTopicsAPI: TopicsAPI {
4245
self.endpoint = endpoint
4346
}
4447

45-
public func get(topicId: String) -> EventLoopFuture<GoogleCloudPubSubTopic> {
46-
return request.send(method: .GET, path: "\(endpoint)/v1/projects/\(request.project)/topics/\(topicId)")
48+
public func get(topicId: String, topicProject: String? = nil) -> EventLoopFuture<GoogleCloudPubSubTopic> {
49+
return request.send(method: .GET, path: "\(endpoint)/v1/projects/\(topicProject ?? request.project)/topics/\(topicId)")
4750
}
4851

49-
public func list(pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubListTopicResponse> {
52+
public func list(pageSize: Int?, pageToken: String?, topicProject: String? = nil) -> EventLoopFuture<GooglePubSubListTopicResponse> {
5053
var query = "pageSize=\(pageSize ?? 10)"
5154
if let pageToken = pageToken {
5255
query.append(contentsOf: "&pageToken=\(pageToken)")
5356
}
5457

5558
return request.send(method: .GET,
56-
path: "\(endpoint)/v1/projects/\(request.project)/topics",
59+
path: "\(endpoint)/v1/projects/\(topicProject ?? request.project)/topics",
5760
query: query)
5861
}
5962

60-
public func publish(topicId: String, data: String, attributes: [String: String]?, orderingKey: String?) -> EventLoopFuture<GoogleCloudPublishResponse> {
63+
public func publish(topicId: String, topicProject: String? = nil, data: String, attributes: [String: String]?, orderingKey: String?) -> EventLoopFuture<GoogleCloudPublishResponse> {
6164
do {
6265
let message = GoogleCloudPubSubMessage(data: data, attributes: attributes, orderingKey: orderingKey)
6366
let publishRequest = GoogleCloudPublishRequest(messages: [message])
6467
let body = try HTTPClient.Body.data(encoder.encode(publishRequest))
65-
let path = "\(endpoint)/v1/projects/\(request.project)/topics/\(topicId):publish"
68+
let path = "\(endpoint)/v1/projects/\(topicProject ?? request.project)/topics/\(topicId):publish"
6669

6770
print("<<<--- Publish on: \(path) --->")
6871

@@ -74,14 +77,14 @@ public final class GoogleCloudPubSubTopicsAPI: TopicsAPI {
7477
}
7578
}
7679

77-
public func getSubscriptionsList(topicId: String, pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubTopicSubscriptionListResponse> {
80+
public func getSubscriptionsList(topicId: String, topicProject: String? = nil, pageSize: Int?, pageToken: String?) -> EventLoopFuture<GooglePubSubTopicSubscriptionListResponse> {
7881
var query = "pageSize=\(pageSize ?? 10)"
7982
if let pageToken = pageToken {
8083
query.append(contentsOf: "&pageToken=\(pageToken)")
8184
}
8285

8386
return request.send(method: .GET,
84-
path: "\(endpoint)/v1/projects/\(request.project)/topics/subscriptions",
87+
path: "\(endpoint)/v1/projects/\(topicProject ?? request.project)/topics/subscriptions",
8588
query: query)
8689
}
8790
}

0 commit comments

Comments
 (0)