Skip to content

Commit

Permalink
Remove unnecessary concurrency extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
olejnjak committed Nov 10, 2024
1 parent d8a7483 commit c683522
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 92 deletions.
87 changes: 0 additions & 87 deletions Sources/GCP_Remote/Extensions/URLSessionExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,4 @@ struct RequestError: Error {

extension URLSession {
static let torino = URLSession(configuration: .ephemeral)

@available(*, deprecated, renamed: "data(request:)")
func syncDataTask(for request: URLRequest) throws -> (Data?, URLResponse?) {
let semaphore = DispatchSemaphore(value: 0)

var resultData: Data?
var resultResponse: URLResponse?
var resultError: Error?

let task = dataTask(with: request) { data, response, error in
resultData = data
resultResponse = response
resultError = error
semaphore.signal()
}

task.resume()
semaphore.wait()

if let error = resultError {
throw error
}

if let httpResponse = (resultResponse as? HTTPURLResponse),
(200...299).contains(httpResponse.statusCode) {
return (resultData, resultResponse)
}

throw RequestError(response: resultResponse, data: resultData)
}
}

// Swift Concurrency API is available from macOS 12,
// to support lower deployment target we need following extensions
@available(macOS, deprecated: 12.0, message: "Use the built-in API instead")
extension URLSession {
@discardableResult
func data(request: URLRequest) async throws -> (Data, URLResponse) {
try await withUnsafeThrowingContinuation { continuation in
let task = self.dataTask(
with: request,
completionHandler: Self.taskCompletion(continuation: continuation)
)

task.resume()
}
}

@discardableResult
func data(url: URL) async throws -> (Data, URLResponse) {
try await data(request: URLRequest(url: url))
}

@discardableResult
func upload(request: URLRequest, fromFile file: URL) async throws -> (Data, URLResponse) {
try await withUnsafeThrowingContinuation { continuation in
let task = self.uploadTask(
with: request,
fromFile: file,
completionHandler: Self.taskCompletion(continuation: continuation)
)

task.resume()
}
}

private static func taskCompletion(continuation: UnsafeContinuation<(Data, URLResponse), Error>) -> (Data?, URLResponse?, Error?) -> () {
{ data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}

if let httpResponse = (response as? HTTPURLResponse),
(200...299).contains(httpResponse.statusCode) {
continuation.resume(returning: (data, response))
} else {
continuation.resume(
throwing:
URLError(
.cannotParseResponse,
userInfo: [:]
)
)
}
}
}
}
10 changes: 5 additions & 5 deletions Sources/GCP_Remote/Services/GCPAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class GCPAPIService: GCPAPIServicing {
token.addToRequest(&request)
request.httpMethod = "GET"

return try await session.data(request: request).0
return try await session.data(for: request).0
}

public func upload(
Expand All @@ -87,8 +87,8 @@ public final class GCPAPIService: GCPAPIServicing {
token.addToRequest(&request)
request.setValue("application/zip", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
try await session.upload(request: request, fromFile: file)

_ = try await session.upload(for: request, fromFile: file)
}

public func metadata(
Expand All @@ -105,7 +105,7 @@ public final class GCPAPIService: GCPAPIServicing {
request.httpMethod = "GET"
return try await JSONDecoder().decode(
Metadata.self,
from: session.data(request: request).0
from: session.data(for: request).0
)
}

Expand All @@ -121,7 +121,7 @@ public final class GCPAPIService: GCPAPIServicing {
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(metadata)

try await session.data(request: request)
_ = try await session.data(for: request)
}

// MARK: - Private helpers
Expand Down

0 comments on commit c683522

Please sign in to comment.