-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ⬆️ Update dependencies * ✨ Prepare new async GCPAPIService * ♻️ Make download command async * ♻️ Make upload command async * ♻️ Make AuthAPI async * 🐛 Fix async URLSession extensions * ⬇️ Downgrade ArgumentParser * 🔧 Require macOS 12 * 🔧 Use Xcode 13.2.1 on CI
- Loading branch information
Showing
14 changed files
with
246 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
13.0 | ||
13.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
extension Sequence { | ||
func asyncForEach( | ||
_ operation: (Element) async throws -> Void | ||
) async rethrows { | ||
for element in self { | ||
try await operation(element) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import Foundation | ||
|
||
public protocol GCPAPIServicing { | ||
func downloadObject( | ||
_ object: String, | ||
bucket: String, | ||
token: AccessToken | ||
) async throws -> Data | ||
|
||
func uploadData( | ||
_ data: Data, | ||
object: String, | ||
bucket: String, | ||
token: AccessToken | ||
) async throws | ||
} | ||
|
||
public final class GCPAPIService: GCPAPIServicing { | ||
private enum Action: String { | ||
case download, upload | ||
} | ||
|
||
private let session: URLSession | ||
|
||
// MARK: - Initializers | ||
|
||
public convenience init() { | ||
self.init(session: .torino) | ||
} | ||
|
||
init(session: URLSession) { | ||
self.session = session | ||
} | ||
|
||
// MARK: - Interface | ||
|
||
public func downloadObject( | ||
_ object: String, | ||
bucket: String, | ||
token: AccessToken | ||
) async throws -> Data { | ||
var urlComponents = URLComponents(string: url( | ||
action: .download, | ||
bucket: bucket, | ||
object: object).absoluteString)! | ||
urlComponents.queryItems = [ | ||
.init(name: "alt", value: "media"), | ||
] | ||
|
||
var request = URLRequest(url: urlComponents.url!) | ||
token.addToRequest(&request) | ||
request.httpMethod = "GET" | ||
|
||
return try await session.data(request: request).0 | ||
} | ||
|
||
public func uploadData( | ||
_ data: Data, | ||
object: String, | ||
bucket: String, | ||
token: AccessToken | ||
) async throws { | ||
var urlComponents = URLComponents(string: url( | ||
action: .upload, | ||
bucket: bucket, | ||
object: nil).absoluteString)! | ||
urlComponents.queryItems = [ | ||
.init(name: "uploadType", value: "media"), | ||
.init(name: "name", value: object), | ||
] | ||
|
||
var request = URLRequest(url: urlComponents.url!) | ||
token.addToRequest(&request) | ||
request.setValue("application/zip", forHTTPHeaderField: "Content-Type") | ||
request.httpMethod = "POST" | ||
request.httpBody = data | ||
|
||
_ = try await session.data(request: request) | ||
} | ||
|
||
// MARK: - Private helpers | ||
|
||
private func url( | ||
action: Action, | ||
bucket: String, | ||
object: String? | ||
) -> URL { | ||
.init(string: [ | ||
"https://storage.googleapis.com", | ||
action.rawValue, | ||
"storage/v1/b", | ||
bucket, | ||
"o", | ||
object, | ||
].compactMap { $0 }.joined(separator: "/"))! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.