Skip to content

Commit

Permalink
✨ Add more logging to GCPUploader (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
olejnjak authored Mar 19, 2022
1 parent 2183371 commit a513b1d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Sources/GCP_Remote/Services/GCPUploader.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import TSCBasic
import Logger

public typealias UploadItem = (localFile: AbsolutePath, remotePath: String)

Expand All @@ -10,24 +11,30 @@ public protocol GCPUploading {
public struct GCPUploader: GCPUploading {
private let authAPI: AuthAPIServicing
private let gcpAPI: GCPAPIServicing
private let logger: Logging
private let config: GCPConfig

// MARK: - Initializers

public init(
authAPI: AuthAPIServicing = AuthAPIService(),
gcpAPI: GCPAPIServicing = GCPAPIService(),
logger: Logging = Logger.shared,
config: GCPConfig
) {
self.authAPI = authAPI
self.gcpAPI = gcpAPI
self.logger = logger
self.config = config
}

// MARK: - Public nterface

public func upload(items: [UploadItem]) async throws {
guard items.count > 0 else { return }
guard items.count > 0 else {
logger.info("Nothing to upload")
return
}

let sa = try loadServiceAccount(path: config.serviceAccountPath)
let token = try await authAPI.fetchAccessToken(
Expand All @@ -37,6 +44,10 @@ public struct GCPUploader: GCPUploading {
)

try await items.asyncForEach { localPath, remotePath in
let name = localPath.basenameWithoutExt

logger.info("Uploading dependency", name)

var urlComponents = URLComponents(string: "https://storage.googleapis.com/upload/storage/v1/b/" + config.bucket + "/o")!
urlComponents.queryItems = [
.init(name: "uploadType", value: "media"),
Expand All @@ -47,14 +58,21 @@ public struct GCPUploader: GCPUploading {
token.addToRequest(&request)
request.setValue("application/zip", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = try Data(contentsOf: localPath.asURL)

try await gcpAPI.uploadData(
try Data(contentsOf: localPath.asURL),
object: remotePath,
bucket: config.bucket,
token: token
)
do {
request.httpBody = try Data(contentsOf: localPath.asURL)

try await gcpAPI.uploadData(
try Data(contentsOf: localPath.asURL),
object: remotePath,
bucket: config.bucket,
token: token
)
logger.info("Successfully uploaded dependency", name)
} catch {
logger.info("Unable to upload dependency", name)
logger.error(error.localizedDescription)
}
}
}
}

0 comments on commit a513b1d

Please sign in to comment.