Skip to content

Commit

Permalink
Merge pull request #13 from ViacomInc/strict-concurrency
Browse files Browse the repository at this point in the history
Enable Strict Concurrency mode
  • Loading branch information
emildzwonek authored Aug 2, 2024
2 parents 43e55be + 1a5665b commit 1890c27
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ on:
jobs:
tests:
name: Tests
runs-on: macOS-latest
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Select Xcode 14.2
run: sudo xcode-select -s /Applications/Xcode_14.2.app
- name: Run swiftlint
run: swiftlint
- name: Select Xcode 15.4
run: sudo xcode-select -s /Applications/Xcode_15.4.app
- name: Run tests
run: swift test
25 changes: 15 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
// swift-tools-version:5.7
// swift-tools-version:5.10

import PackageDescription

let package = Package(
name: "URLSessionDecodable",
platforms: [
.macOS(.v10_13),
.iOS(.v11),
.tvOS(.v11),
.macOS(.v13),
.iOS(.v12),
.tvOS(.v12),
.watchOS(.v4)
],
products: [
.library(
name: "URLSessionDecodable",
targets: ["URLSessionDecodable"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: [],
targets: [
.target(
name: "URLSessionDecodable",
dependencies: []),
dependencies: [],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "URLSessionDecodableTests",
dependencies: ["URLSessionDecodable"])
dependencies: ["URLSessionDecodable"],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
]
)
14 changes: 7 additions & 7 deletions Sources/URLSessionDecodable/URLSessionDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension URLSession {
method: HTTPMethod,
parameters: ParametersEncoding?,
headers: HTTPHeaders?,
decoder: DataDecoder,
completionHandler: @escaping (Result<T, URLSessionDecodableError>) -> Void
decoder: @autoclosure @escaping @Sendable () -> DataDecoder,
completionHandler: @escaping @Sendable (Result<T, URLSessionDecodableError>) -> Void
) -> URLSessionDataTask {
let request = self.request(with: url, method: method, parameters: parameters, headers: headers)
let task = dataTask(with: request) { data, response, error in
Expand All @@ -45,7 +45,7 @@ extension URLSession {
return
}

completionHandler(Self.handle(response: httpResponse, data: data, decoder: decoder, url: url))
completionHandler(Self.handle(response: httpResponse, data: data, decoder: decoder(), url: url))
}
return task
}
Expand All @@ -60,23 +60,23 @@ extension URLSession {
method: HTTPMethod,
parameters: ParametersEncoding?,
headers: HTTPHeaders?,
decoder: DataDecoder
decoder: @autoclosure @Sendable () -> DataDecoder
) async throws -> T {
let request = self.request(with: url, method: method, parameters: parameters, headers: headers)
let (data, response) = try await data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
os_log("%@", "Non-http response \(String(describing: type(of: T.self))) \(url) - \(response)")
throw URLSessionDecodableError.nonHTTPResponse(response)
}
return try Self.handle(response: httpResponse, data: data, decoder: decoder, url: url).get()
return try Self.handle(response: httpResponse, data: data, decoder: decoder(), url: url).get()
}

// MARK: - Private

private static func handle<T: Decodable>(
response: HTTPURLResponse,
data: Data,
decoder: DataDecoder,
decoder: @autoclosure @Sendable () -> DataDecoder,
url: URL
) -> Result<T, URLSessionDecodableError> {
guard 200..<300 ~= response.statusCode else {
Expand All @@ -87,7 +87,7 @@ extension URLSession {
}

do {
return try .success(decoder.decode(T.self, from: data))
return try .success(decoder().decode(T.self, from: data))
} catch {
os_log("%@", "Error while decoding \(String(describing: type(of: T.self))) \(error)")
let deserializationError = URLSessionDecodableError.Deserialization(statusCode: response.statusCode,
Expand Down
4 changes: 2 additions & 2 deletions Sources/URLSessionDecodable/URLSessionDecodableError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public enum URLSessionDecodableError: Error {
case nonHTTPResponse(URLResponse)
case serverResponse(ServerResponse)

public struct Deserialization {
public struct Deserialization: Sendable {
public let statusCode: Int
public let url: URL
public let responseBody: Data
public let underlyingError: Error?
}

public struct ServerResponse {
public struct ServerResponse: Sendable {
public let statusCode: Int
public let url: URL
public let responseBody: Data
Expand Down

0 comments on commit 1890c27

Please sign in to comment.