From 8ee97fb76c01fd64a7290c5ea576cd7e3dec2e31 Mon Sep 17 00:00:00 2001 From: Owain Hunt Date: Sat, 11 Apr 2020 16:04:41 +0100 Subject: [PATCH] Make Datastore base URL configurable --- .../GoogleCloudDatastoreAPI.swift | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/CloudDatastore/GoogleCloudDatastoreAPI.swift b/Sources/CloudDatastore/GoogleCloudDatastoreAPI.swift index 2ac1d6e..43888a7 100644 --- a/Sources/CloudDatastore/GoogleCloudDatastoreAPI.swift +++ b/Sources/CloudDatastore/GoogleCloudDatastoreAPI.swift @@ -22,6 +22,10 @@ extension Application.GoogleCloudPlatform { typealias Value = HTTPClient } + private struct CloudDatastoreBaseKey: StorageKey { + typealias Value = String + } + public var datastore: GoogleCloudDatastoreAPI { get { if let existing = self.application.storage[CloudDatastoreAPIKey.self] { @@ -46,7 +50,8 @@ extension Application.GoogleCloudPlatform { let new = try GoogleCloudDatastoreClient(credentials: self.application.googleCloud.credentials, config: self.configuration, httpClient: self.http, - eventLoop: self.eventLoop) + eventLoop: self.eventLoop, + base: self.base) return new } catch { fatalError("\(error.localizedDescription)") @@ -71,6 +76,24 @@ extension Application.GoogleCloudPlatform { } } + public var base: String { + get { + if let base = application.storage[CloudDatastoreBaseKey.self] { + return base + } else { + return "https://datastore.googleapis.com" + } + } + + set { + if application.storage[CloudDatastoreBaseKey.self] == nil { + application.storage[CloudDatastoreBaseKey.self] = newValue + } else { + fatalError("Attempting to override configuration after being set is not allowed.") + } + } + } + /// Custom `HTTPClient` that ignores unclean SSL shutdown. public var http: HTTPClient { if let existing = application.storage[CloudDatastoreHTTPClientKey.self] {