Skip to content

Commit cd0bf1d

Browse files
authored
Merge pull request #18 from vapor-community/vapor-beta
Updates for Vapor 4 beta and GoogleCloudKit.
2 parents 9de1c11 + be63a5c commit cd0bf1d

6 files changed

Lines changed: 174 additions & 38 deletions

File tree

Package.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@ import PackageDescription
55

66
let package = Package(
77
name: "GoogleCloud",
8+
platforms: [
9+
.macOS(.v10_14)
10+
],
811
products: [
9-
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1012
.library(
1113
name: "GoogleCloud",
1214
targets: ["GoogleCloud"]),
15+
.library(
16+
name: "CloudStorage",
17+
targets: ["CloudStorage"]),
1318
],
1419
dependencies: [
15-
// Dependencies declare other packages that this package depends on.
16-
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-alpha"),
20+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta"),
1721
.package(url: "https://github.com/vapor-community/GoogleCloudKit.git", from: "1.0.0-alpha")
1822
],
1923
targets: [
20-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
21-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2224
.target(
2325
name: "GoogleCloud",
2426
dependencies: ["Vapor", "GoogleCloudKit"]),
25-
.testTarget(
26-
name: "GoogleCloudTests",
27-
dependencies: ["GoogleCloud"]),
27+
28+
.target(
29+
name: "CloudStorage",
30+
dependencies: ["Vapor", "GoogleCloudStorage"]),
2831
]
2932
)

README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
In your `Package.swift` file, add the following
1212

1313
```swift
14-
.package(url: "https://github.com/vapor-community/google-cloud-provider.git", from: "1.0.0-alpha.1")
14+
.package(url: "https://github.com/vapor-community/google-cloud-provider.git", from: "1.0.0-beta")
1515
```
1616

1717
Register the credentials configuration (required) and the provider in `Configure.swift`
@@ -20,39 +20,34 @@ Register the credentials configuration (required) and the provider in `Configur
2020
import GoogleCloud
2121

2222
// register the credentials configuration which is used by all APIs
23-
s.register(GoogleCloudCredentialsConfiguration.self) { _ in
23+
app.register(GoogleCloudCredentialsConfiguration.self) { _ in
2424
return GoogleCloudCredentialsConfiguration(project: "myprojectid-12345",
2525
credentialsFile: "~/path/to/service-account.json")
2626
}
2727

2828
// Register an API specific configuration. CloudStorage in this example.
29-
s.register(GoogleCloudStorageConfiguration.self) { _ in
29+
app.register(GoogleCloudStorageConfiguration.self) { _ in
3030
return GoogleCloudStorageConfiguration.defult()
3131
}
3232

33-
s.provider(GoogleCloudProvider())
33+
// Configure more API configurations that you want to use.
34+
35+
// Add the GoogleCloudProvider and choose the APIs you want to include
36+
app.provider(GoogleCloudProvider(apis: [.storage, .pubsub, ...])
3437
```
3538

36-
Example usage
37-
```swift
39+
Now we can access the `GoogleCloudClient` which has access to all the APIs we've configured.
3840

39-
struct UploadRequest: Content {
40-
var data: Data
41-
var filename: String
42-
}
43-
44-
func uploadImage(_ req: Request) throws {
45-
let upload = try req.content.decode(UploadRequest.self)
46-
47-
let storageClient = try container.make(GoogleCloudStorageClient.self)
48-
storageClient.object.createSimpleUpload(bucket: "vapor-cloud-storage-demo",
49-
data: upload.data,
50-
name: upload.filename,
51-
contentType: "image/jpeg").flatMap { uploadedObject in
52-
print(uploadedObject.mediaLink) // prints the download link for the image.
41+
```swift
42+
let cloudClient = app.make(GoogleCloudClient.self)
43+
// Use the Storage api to list the buckets.
44+
cloudClient.storage.buckets.list().flatMap { buckets in
45+
print(buckets.items.last.name)
5346
}
54-
}
5547
```
5648

49+
### Supported APIs
50+
[x] [CloudStorage](/Sources/CloudStorage/README.md)
51+
5752
### A More detailed guide can be found [here](https://github.com/vapor-community/GoogleCloudKit).
5853

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// CloudStorageProvider.swift
3+
//
4+
//
5+
// Created by Andrew Edwards on 11/2/19.
6+
//
7+
8+
import Vapor
9+
@_exported import Core
10+
@_exported import Storage
11+
12+
public final class CloudStorageProvider: Provider {
13+
public init() {}
14+
public func register(_ app: Application) {
15+
app.register(GoogleCloudStorageClient.self) { app in
16+
let credentialsConfig = app.make(GoogleCloudCredentialsConfiguration.self)
17+
let storageConfig = app.make(GoogleCloudStorageConfiguration.self)
18+
return try GoogleCloudStorageClient(configuration: credentialsConfig,
19+
storageConfig: storageConfig,
20+
eventLoop: app.make())
21+
}
22+
}
23+
}

Sources/CloudStorage/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# CloudStorageProvider
2+
3+
## Getting Started
4+
If you only need to use the [Google Cloud Storage API](https://cloud.google.com/storage/), then this guide will help you get started.
5+
6+
In your `Package.swift` file, make sure you have the following dependencies and targets
7+
8+
```swift
9+
dependencies: [
10+
//...
11+
.package(url: "https://github.com/vapor-community/google-cloud-provider.git", from: "1.0.0-beta"),
12+
],
13+
targets: [
14+
.target(name: "MyAppName", dependencies: [// Other dependencies
15+
"CloudStorage",
16+
]),
17+
]
18+
```
19+
20+
Now you can register the `GoogleCloudCredentialsConfiguraton` and the `GoogleCloudStorageConfiguration`.
21+
22+
In `Configure.swift`
23+
24+
```swift
25+
import CloudStorage
26+
27+
// register the credentials configuration which is used by all APIs
28+
app.register(GoogleCloudCredentialsConfiguration.self) { _ in
29+
return GoogleCloudCredentialsConfiguration(project: "myprojectid-12345",
30+
credentialsFile: "~/path/to/service-account.json")
31+
}
32+
33+
// Register the CloudStorage Configuraton.
34+
app.register(GoogleCloudStorageConfiguration.self) { _ in
35+
return GoogleCloudStorageConfiguration.defult()
36+
}
37+
38+
// Add the CloudStorageProvder
39+
app.provider(CloudStorageProvider())
40+
```
41+
42+
Now we can start using the CloudStorage API.
43+
We do that by making a `GoogleCloudStorageClient` object that's already configured
44+
from the `CloudStorageProvider` we setup above.
45+
46+
```swift
47+
struct UploadRequest: Content {
48+
var data: Data
49+
var filename: String
50+
}
51+
52+
func uploadImage(_ req: Request) throws {
53+
let upload = try req.content.decode(UploadRequest.self)
54+
55+
let storageClient = app.make(GoogleCloudStorageClient.self)
56+
storageClient.object.createSimpleUpload(bucket: "vapor-cloud-storage-demo",
57+
data: upload.data,
58+
name: upload.filename,
59+
contentType: "image/jpeg").flatMap { uploadedObject in
60+
print(uploadedObject.mediaLink) // prints the download link for the image.
61+
}
62+
}
63+
```

Sources/GoogleCloud/APIs.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// APIs.swift
3+
//
4+
//
5+
// Created by Andrew Edwards on 11/2/19.
6+
//
7+
8+
import Foundation
9+
10+
/// An enum that describes all the currently suported APIs.
11+
/// New APIs that are supported should be added as a new case named after the API.
12+
public enum GoogleCloudAPI: CaseIterable {
13+
/// The Cloud Storage API.
14+
case storage
15+
}

Sources/GoogleCloud/GoogleCloudProvider.swift

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,57 @@
44
//
55
// Created by Andrew Edwards on 4/17/18.
66
//
7+
78
import Vapor
8-
@_exported import GoogleCloudKit
9+
@_exported import Core
10+
@_exported import Storage
11+
// New APIs added to GoogleCloudKit should be exported here.
912

1013
public final class GoogleCloudProvider: Provider {
14+
private let apis: [GoogleCloudAPI]
15+
16+
/// - Parameter apis: An array containing `GoogleCloudAPI`s that you want to use and that have also been configured to use.
17+
public init(apis: [GoogleCloudAPI]) {
18+
self.apis = apis
19+
}
20+
21+
public func register(_ app: Application) {
22+
app.register(GoogleCloudClient.self) { app in
23+
return try GoogleCloudClient(apis: self.apis, app: app)
24+
}
25+
}
26+
}
27+
28+
/** The GoogleCloudClient acts as a single point of access for interacting with one or more GCP APIs that have been configured.
29+
30+
```
31+
let client: GoogleCloudClient = GoogleCloudClient(...)
32+
client.storage.buckets // etc.
33+
client.pubsub // etc.
34+
client.gcpAPI // etc.
35+
```
36+
*/
37+
public class GoogleCloudClient {
38+
private var _storage: GoogleCloudStorageClient?
1139

12-
public init() {}
40+
/// An instance of the CloudStorageClient used to access the various CloudStorage APIs.
41+
public var storage: GoogleCloudStorageClient {
42+
guard let storageClient = _storage else {
43+
fatalError("Failed to create CloudStorageClient. Make sure you specify '.storage' when creating the 'GoogleCloudProvider'.")
44+
}
45+
return storageClient
46+
}
1347

14-
public func register(_ s: inout Services) {
15-
s.register(GoogleCloudStorageClient.self) { container in
16-
let credentialsConfig = try container.make(GoogleCloudCredentialsConfiguration.self)
17-
let storageConfig = try container.make(GoogleCloudStorageConfiguration.self)
18-
return try GoogleCloudStorageClient(configuration: credentialsConfig,
19-
storageConfig: storageConfig,
20-
eventLoop: container.eventLoop)
48+
init(apis: [GoogleCloudAPI], app: Application) throws {
49+
let credentialsConfig = app.make(GoogleCloudCredentialsConfiguration.self)
50+
for api in apis {
51+
switch api {
52+
case .storage:
53+
let storageConfig = app.make(GoogleCloudStorageConfiguration.self)
54+
_storage = try GoogleCloudStorageClient(configuration: credentialsConfig,
55+
storageConfig: storageConfig,
56+
eventLoop: app.make())
57+
}
2158
}
2259
}
2360
}

0 commit comments

Comments
 (0)