Skip to content

Commit 74571d6

Browse files
authored
Add Swift Result type APIs to Storage (#5225)
1 parent 1054d6a commit 74571d6

File tree

4 files changed

+706
-1
lines changed

4 files changed

+706
-1
lines changed

FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Google
1+
// Copyright 2020 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

FirebaseStorageSwift.podspec

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Pod::Spec.new do |s|
2+
s.name = 'FirebaseStorageSwift'
3+
s.version = '0.1.0'
4+
s.summary = 'Swift Extensions for Google Cloud Storage'
5+
6+
s.description = <<-DESC
7+
Firebase Storage provides robust, secure file uploads and downloads from Firebase SDKs, powered by Google Cloud Storage.
8+
DESC
9+
10+
11+
s.homepage = 'https://developers.google.com/'
12+
s.license = { :type => 'Apache', :file => 'LICENSE' }
13+
s.authors = 'Google, Inc.'
14+
15+
s.source = {
16+
:git => 'https://github.com/Firebase/firebase-ios-sdk.git',
17+
:tag => 'StorageSwift-' + s.version.to_s
18+
}
19+
20+
s.swift_version = '5.0'
21+
s.ios.deployment_target = '8.0'
22+
s.osx.deployment_target = '10.11'
23+
s.tvos.deployment_target = '10.0'
24+
s.watchos.deployment_target = '6.0'
25+
26+
s.cocoapods_version = '>= 1.4.0'
27+
s.static_framework = true
28+
s.prefix_header_file = false
29+
30+
s.source_files = [
31+
'FirebaseStorageSwift/Sources/*.swift',
32+
]
33+
34+
s.dependency 'FirebaseStorage', '~> 3.6'
35+
36+
s.test_spec 'integration' do |int_tests|
37+
int_tests.platforms = {:ios => '8.0', :osx => '10.11', :tvos => '10.0'}
38+
int_tests.source_files = 'FirebaseStorageSwift/Tests/Integration/*.swift'
39+
int_tests.requires_app_host = true
40+
# Resources are shared with FirebaseStorage's integration tests.
41+
int_tests.resources = 'FirebaseStorage/Tests/Integration/Resources/1mb.dat',
42+
'FirebaseStorage/Tests/Integration/Resources/GoogleService-Info.plist'
43+
end
44+
end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import FirebaseStorage
16+
17+
/// getResultCallback generates a closure that returns a Result type from a closure that returns an
18+
/// optional type and Error.
19+
private func getResultCallback<T>(
20+
completion: @escaping (Result<T, Error>) -> Void
21+
) -> (_: T?, _: Error?) -> Void {
22+
return { (value: T?, error: Error?) -> Void in
23+
if let value = value {
24+
completion(.success(value))
25+
} else if let error = error {
26+
completion(.failure(error))
27+
} else {
28+
completion(.failure(NSError(domain: "FirebaseStorageSwift",
29+
code: -1,
30+
userInfo: [NSLocalizedDescriptionKey:
31+
"InternalError - Return type and Error code both nil in " +
32+
"Storage Result generator"])))
33+
}
34+
}
35+
}
36+
37+
public extension StorageReference {
38+
func downloadURL(completion: @escaping (Result<URL, Error>) -> Void) {
39+
downloadURL(completion: getResultCallback(completion: completion))
40+
}
41+
42+
func getData(maxSize: Int64, completion: @escaping (Result<Data, Error>) -> Void)
43+
-> StorageDownloadTask {
44+
return getData(maxSize: maxSize, completion: getResultCallback(completion: completion))
45+
}
46+
47+
func getMetadata(completion: @escaping (Result<StorageMetadata, Error>) -> Void) {
48+
getMetadata(completion: getResultCallback(completion: completion))
49+
}
50+
51+
func list(withMaxResults maxResults: Int64,
52+
pageToken: String,
53+
completion: @escaping (Result<StorageListResult, Error>) -> Void) {
54+
list(withMaxResults: maxResults,
55+
pageToken: pageToken,
56+
completion: getResultCallback(completion: completion))
57+
}
58+
59+
func list(withMaxResults maxResults: Int64,
60+
completion: @escaping (Result<StorageListResult, Error>) -> Void) {
61+
list(withMaxResults: maxResults,
62+
completion: getResultCallback(completion: completion))
63+
}
64+
65+
func listAll(completion: @escaping (Result<StorageListResult, Error>) -> Void) {
66+
listAll(completion: getResultCallback(completion: completion))
67+
}
68+
69+
func putData(_ uploadData: Data,
70+
metadata: StorageMetadata? = nil,
71+
completion: @escaping (Result<StorageMetadata, Error>) -> Void)
72+
-> StorageUploadTask {
73+
return putData(uploadData,
74+
metadata: metadata,
75+
completion: getResultCallback(completion: completion))
76+
}
77+
78+
func putFile(from: URL,
79+
metadata: StorageMetadata? = nil,
80+
completion: @escaping (Result<StorageMetadata, Error>) -> Void)
81+
-> StorageUploadTask {
82+
return putFile(from: from,
83+
metadata: metadata,
84+
completion: getResultCallback(completion: completion))
85+
}
86+
87+
func updateMetadata(_ metadata: StorageMetadata,
88+
completion: @escaping (Result<StorageMetadata, Error>) -> Void) {
89+
return updateMetadata(metadata, completion: getResultCallback(completion: completion))
90+
}
91+
92+
func write(toFile: URL, completion: @escaping (Result<URL, Error>)
93+
-> Void) -> StorageDownloadTask {
94+
return write(toFile: toFile, completion: getResultCallback(completion: completion))
95+
}
96+
}

0 commit comments

Comments
 (0)