|
| 1 | +// |
| 2 | +// GoogleCloudTranslationAPI.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Andrew Edwards on 5/4/21. |
| 6 | +// |
| 7 | + |
| 8 | +import Vapor |
| 9 | +@_exported import Translation |
| 10 | +@_exported import GoogleCloud |
| 11 | + |
| 12 | +extension Application.GoogleCloudPlatform { |
| 13 | + |
| 14 | + private struct CloudTranslationAPIKey: StorageKey { |
| 15 | + typealias Value = GoogleCloudTranslationAPI |
| 16 | + } |
| 17 | + |
| 18 | + private struct CloudSecretManagerConfigurationKey: StorageKey { |
| 19 | + typealias Value = GoogleCloudTranslationConfiguration |
| 20 | + } |
| 21 | + |
| 22 | + private struct CloudTranslationHTTPClientKey: StorageKey, LockKey { |
| 23 | + typealias Value = HTTPClient |
| 24 | + } |
| 25 | + |
| 26 | + public var translation: GoogleCloudTranslationAPI { |
| 27 | + get { |
| 28 | + if let existing = self.application.storage[CloudTranslationAPIKey.self] { |
| 29 | + return existing |
| 30 | + } else { |
| 31 | + return .init(application: self.application, eventLoop: self.application.eventLoopGroup.next()) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + nonmutating set { |
| 36 | + self.application.storage[CloudTranslationAPIKey.self] = newValue |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public struct GoogleCloudTranslationAPI { |
| 41 | + public let application: Application |
| 42 | + public let eventLoop: EventLoop |
| 43 | + |
| 44 | + /// A client used to interact with the `GoogleCloudTranslation` API. |
| 45 | + public var client: GoogleCloudTranslationClient { |
| 46 | + do { |
| 47 | + let new = try GoogleCloudTranslationClient(credentials: self.application.googleCloud.credentials, |
| 48 | + config: self.configuration, |
| 49 | + httpClient: self.http, |
| 50 | + eventLoop: self.eventLoop) |
| 51 | + return new |
| 52 | + } catch { |
| 53 | + fatalError("\(error.localizedDescription)") |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// The configuration for using `GoogleCloudTranslation` APIs. |
| 58 | + public var configuration: GoogleCloudTranslationConfiguration { |
| 59 | + get { |
| 60 | + if let configuration = application.storage[CloudSecretManagerConfigurationKey.self] { |
| 61 | + return configuration |
| 62 | + } else { |
| 63 | + fatalError("Cloud Translation configuration has not been set. Use app.googleCloud.translation.configuration = ...") |
| 64 | + } |
| 65 | + } |
| 66 | + set { |
| 67 | + if application.storage[CloudSecretManagerConfigurationKey.self] == nil { |
| 68 | + application.storage[CloudSecretManagerConfigurationKey.self] = newValue |
| 69 | + } else { |
| 70 | + fatalError("Attempting to override credentials configuration after being set is not allowed.") |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Custom `HTTPClient` that ignores unclean SSL shutdown. |
| 76 | + public var http: HTTPClient { |
| 77 | + if let existing = application.storage[CloudTranslationHTTPClientKey.self] { |
| 78 | + return existing |
| 79 | + } else { |
| 80 | + let lock = application.locks.lock(for: CloudTranslationHTTPClientKey.self) |
| 81 | + lock.lock() |
| 82 | + defer { lock.unlock() } |
| 83 | + if let existing = application.storage[CloudTranslationHTTPClientKey.self] { |
| 84 | + return existing |
| 85 | + } |
| 86 | + let new = HTTPClient( |
| 87 | + eventLoopGroupProvider: .shared(application.eventLoopGroup), |
| 88 | + configuration: HTTPClient.Configuration(ignoreUncleanSSLShutdown: true) |
| 89 | + ) |
| 90 | + application.storage.set(CloudTranslationHTTPClientKey.self, to: new) { |
| 91 | + try $0.syncShutdown() |
| 92 | + } |
| 93 | + return new |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +extension Request { |
| 100 | + private struct GoogleCloudTranslationKey: StorageKey { |
| 101 | + typealias Value = GoogleCloudTranslationClient |
| 102 | + } |
| 103 | + |
| 104 | + /// A client used to interact with the `GoogleCloudTranslation` API |
| 105 | + public var gcTranslation: GoogleCloudTranslationClient { |
| 106 | + if let existing = application.storage[GoogleCloudTranslationKey.self] { |
| 107 | + return existing.hopped(to: self.eventLoop) |
| 108 | + } else { |
| 109 | + let new = Application.GoogleCloudPlatform.GoogleCloudTranslationAPI(application: self.application, eventLoop: self.eventLoop).client |
| 110 | + application.storage[GoogleCloudTranslationKey.self] = new |
| 111 | + return new |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments