|
| 1 | +// |
| 2 | +// SubscriptionsAPI.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Susheel Athmakuri on 6/21/21. |
| 6 | +// |
| 7 | + |
| 8 | +import Core |
| 9 | +import NIO |
| 10 | +import AsyncHTTPClient |
| 11 | +import Foundation |
| 12 | + |
| 13 | +public protocol SubscriptionsAPI { |
| 14 | + /// Gets the configuration details of a subscription. |
| 15 | + /// |
| 16 | + /// - parameter `subscriptionId`: The name of the subscription to get |
| 17 | + /// - returns: An instance of the `Subscription` |
| 18 | + func get(subscriptionId: String) -> EventLoopFuture<GoogleCloudPubSubSubscription> |
| 19 | + |
| 20 | + /// Acknowledges the messages associated with the ackIds in the AcknowledgeRequest. |
| 21 | + /// |
| 22 | + /// - parameters `subscriptionId`: ID of the subscription whose message is being acknowledged |
| 23 | + /// `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> |
| 25 | + |
| 26 | + /// Creates a subscription to a given topic. |
| 27 | + /// - parameter `subscriptionId`: The name of the subscription to be created. |
| 28 | + /// `topicId`: The name of the topic from which this subscription is receiving messages. |
| 29 | + /// `pushEndpoint`: A URL locating the endpoint to which messages should be pushed. |
| 30 | + /// `pushConfigAttributes`: Endpoint configuration attributes that can be used to control different aspects of the message delivery. |
| 31 | + /// `pushConfigOidcTokenServiceAccountEmail`:Service account email to be used for generating the OIDC token. |
| 32 | + /// `pushConfigOidcTokenAudience`: Audience to be used when generating OIDC token. |
| 33 | + /// `ackDeadlineSeconds`: The approximate amount of time (on a best-effort basis) Pub/Sub waits for the subscriber to acknowledge receipt before resending the message |
| 34 | + /// `retainAckedMessages`: Indicates whether to retain acknowledged messages. |
| 35 | + /// `messageRetentionDuration`: How long to retain unacknowledged messages in the subscription's backlog, from the moment a message is published |
| 36 | + /// `labels`: An object containing a list of "key": value pairs. |
| 37 | + /// `enableMessageOrdering`: If true, messages published with the same orderingKey in PubsubMessage will be delivered to the subscribers in the order in which they are received by the Pub/Sub system. Otherwise, they may be delivered in any order. |
| 38 | + /// `expirationPolicyTTL`: A policy that specifies the conditions for this subscription's expiration. |
| 39 | + /// `filter`: An expression written in the Pub/Sub filter language. |
| 40 | + /// `deadLetterPolicyTopic`: The name of the topic to which dead letter messages should be published. |
| 41 | + /// `deadLetterPolicyMaxDeliveryAttempts`: The maximum number of delivery attempts for any message. |
| 42 | + /// `retryPolicyMinimumBackoff`: The minimum delay between consecutive deliveries of a given message. |
| 43 | + /// `retryPolicyMaximumBackoff`: The maximum delay between consecutive deliveries of a given message. |
| 44 | + /// `detached`: Indicates whether the subscription is detached from its topic. |
| 45 | + /// |
| 46 | + /// - returns: If successful, the response body contains a newly created instance of Subscription. |
| 47 | + /// If the subscription already exists, returns ALREADY_EXISTS. If the corresponding topic doesn't exist, returns NOT_FOUND. |
| 48 | + func create(subscriptionId: String, |
| 49 | + topicId: String, |
| 50 | + pushEndpoint: String?, |
| 51 | + pushConfigAttributes: [String: String]?, |
| 52 | + pushConfigOidcTokenServiceAccountEmail: String?, |
| 53 | + pushConfigOidcTokenAudience: String?, |
| 54 | + ackDeadlineSeconds: Int?, |
| 55 | + retainAckedMessages: Bool?, |
| 56 | + messageRetentionDuration: String?, |
| 57 | + labels: [String: String]?, |
| 58 | + enableMessageOrdering: Bool?, |
| 59 | + expirationPolicyTTL: String?, |
| 60 | + filter: String?, |
| 61 | + deadLetterPolicyTopic: String?, |
| 62 | + deadLetterPolicyMaxDeliveryAttempts: Int?, |
| 63 | + retryPolicyMinimumBackoff: String?, |
| 64 | + retryPolicyMaximumBackoff: String?, |
| 65 | + detached: Bool?) -> EventLoopFuture<GoogleCloudPubSubSubscription> |
| 66 | +} |
| 67 | + |
| 68 | +public final class GoogleCloudPubSubSubscriptionsAPI: SubscriptionsAPI { |
| 69 | + let endpoint: String |
| 70 | + let request: GoogleCloudPubSubRequest |
| 71 | + let encoder = JSONEncoder() |
| 72 | + |
| 73 | + init(request: GoogleCloudPubSubRequest, endpoint: String) { |
| 74 | + self.request = request |
| 75 | + self.endpoint = endpoint |
| 76 | + } |
| 77 | + |
| 78 | + public func get(subscriptionId: String) -> EventLoopFuture<GoogleCloudPubSubSubscription> { |
| 79 | + return request.send(method: .GET, path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId)") |
| 80 | + } |
| 81 | + |
| 82 | + public func acknowledge(subscriptionId: String, ackIds: [String]) -> EventLoopFuture<EmptyResponse> { |
| 83 | + do { |
| 84 | + let acks = AcknowledgeRequest(ackIds: ackIds) |
| 85 | + let body = try HTTPClient.Body.data(encoder.encode(acks)) |
| 86 | + return request.send(method: .POST, |
| 87 | + path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId):acknowledge", |
| 88 | + body: body) |
| 89 | + } catch { |
| 90 | + return request.eventLoop.makeFailedFuture(error) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public func create(subscriptionId: String, |
| 95 | + topicId: String, |
| 96 | + pushEndpoint: String?, |
| 97 | + pushConfigAttributes: [String: String]?, |
| 98 | + pushConfigOidcTokenServiceAccountEmail: String?, |
| 99 | + pushConfigOidcTokenAudience: String?, |
| 100 | + ackDeadlineSeconds: Int?, |
| 101 | + retainAckedMessages: Bool?, |
| 102 | + messageRetentionDuration: String?, |
| 103 | + labels: [String: String]?, |
| 104 | + enableMessageOrdering: Bool?, |
| 105 | + expirationPolicyTTL: String?, |
| 106 | + filter: String?, |
| 107 | + deadLetterPolicyTopic: String?, |
| 108 | + deadLetterPolicyMaxDeliveryAttempts: Int?, |
| 109 | + retryPolicyMinimumBackoff: String?, |
| 110 | + retryPolicyMaximumBackoff: String?, |
| 111 | + detached: Bool?) -> EventLoopFuture<GoogleCloudPubSubSubscription> { |
| 112 | + do { |
| 113 | + var pushConfig: PushConfig? = nil |
| 114 | + if let pushEndpoint = pushEndpoint { |
| 115 | + var oidcToken: OidcToken? = nil |
| 116 | + if let serviceAccountEmail = pushConfigOidcTokenServiceAccountEmail, let audience = pushConfigOidcTokenAudience { |
| 117 | + oidcToken = OidcToken(serviceAccountEmail: serviceAccountEmail, audience: audience) |
| 118 | + } |
| 119 | + |
| 120 | + pushConfig = PushConfig(pushEndpoint: pushEndpoint, |
| 121 | + attributes: pushConfigAttributes, |
| 122 | + oidcToken: oidcToken) |
| 123 | + } |
| 124 | + |
| 125 | + var expirationPolicy: ExpirationPolicy? = nil |
| 126 | + if let ttl = expirationPolicyTTL { |
| 127 | + expirationPolicy = ExpirationPolicy(ttl: ttl) |
| 128 | + } |
| 129 | + |
| 130 | + var deadLetterPolicy: DeadLetterPolicy? = nil |
| 131 | + if let deadLetterPolicyTopic = deadLetterPolicyTopic { |
| 132 | + deadLetterPolicy = DeadLetterPolicy(deadLetterTopic: deadLetterPolicyTopic, |
| 133 | + maxDeliveryAttempts: deadLetterPolicyMaxDeliveryAttempts) |
| 134 | + } |
| 135 | + |
| 136 | + var retryPolicy: RetryPolicy? = nil |
| 137 | + if let min = retryPolicyMinimumBackoff, let max = retryPolicyMaximumBackoff { |
| 138 | + retryPolicy = RetryPolicy(minimumBackoff: min, |
| 139 | + maximumBackoff: max) |
| 140 | + } |
| 141 | + |
| 142 | + let subscription = GoogleCloudPubSubSubscription(name: subscriptionId, |
| 143 | + topic: "projects/\(request.project)/topics/\(topicId)", |
| 144 | + pushConfig: pushConfig, |
| 145 | + ackDeadlineSeconds: ackDeadlineSeconds, |
| 146 | + retainAckedMessages: retainAckedMessages, |
| 147 | + messageRetentionDuration: messageRetentionDuration, |
| 148 | + labels: labels, |
| 149 | + enableMessageOrdering: enableMessageOrdering, |
| 150 | + expirationPolicy: expirationPolicy, |
| 151 | + filter: filter, |
| 152 | + deadLetterPolicy: deadLetterPolicy, |
| 153 | + retryPolicy: retryPolicy, |
| 154 | + detached: detached) |
| 155 | + let body = try HTTPClient.Body.data(encoder.encode(subscription)) |
| 156 | + return request.send(method: .PUT, |
| 157 | + path: "\(endpoint)/v1/projects/\(request.project)/subscriptions/\(subscriptionId)", |
| 158 | + body: body) |
| 159 | + } catch { |
| 160 | + return request.eventLoop.makeFailedFuture(error) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments