Skip to content

Commit

Permalink
Swift 6
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Nigade <[email protected]>
  • Loading branch information
dezinezync committed Jul 14, 2024
1 parent a8532ce commit b6a9e2b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1530"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
28 changes: 18 additions & 10 deletions Sources/DZNetworking/Core/DZURLSession+Requests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Foundation

public typealias SuccessCallback = (_ responseObject: Any, _ response: HTTPURLResponse) -> Void
public typealias ErrorCallback = (_ error: Error) -> Void
public typealias SuccessCallback = @MainActor (_ responseObject: Any, _ response: HTTPURLResponse) -> Void
public typealias ErrorCallback = @MainActor (_ error: Error) -> Void

/// HTTP Methods as a convinience enum
public enum HTTPMethod: String, Equatable, Identifiable, Hashable {
Expand Down Expand Up @@ -124,9 +124,11 @@ extension DZURLSession {
/// - onError: called when the request completes successfully. Always called on the main-thread.
/// - Returns: Task
@discardableResult public func POST(_ uri: String, query: [String: String] = [:], json: Any?, onSuccess: @escaping SuccessCallback, onError: ErrorCallback?) -> _Concurrency.Task<Void, Never> {
Task {
let boxed = SendableBox(json)

return Task {
do {
let (responseObject, response) = try await request(with: uri, method: "POST", query: query, body: json)
let (responseObject, response) = try await request(with: uri, method: "POST", query: query, body: boxed.get())
DispatchQueue.main.async { onSuccess(responseObject, response) }
}
catch {
Expand All @@ -144,9 +146,11 @@ extension DZURLSession {
/// - onError: called when the request completes successfully. Always called on the main-thread.
/// - Returns: Task
@discardableResult public func PUT(_ uri: String, query: [String: String] = [:], json: Any?, onSuccess: @escaping SuccessCallback, onError: ErrorCallback?) -> _Concurrency.Task<Void, Never> {
Task {
let boxed = SendableBox(json)

return Task {
do {
let (responseObject, response) = try await request(with: uri, method: "PUT", query: query, body: json)
let (responseObject, response) = try await request(with: uri, method: "PUT", query: query, body: boxed.get())
DispatchQueue.main.async { onSuccess(responseObject, response) }
}
catch {
Expand All @@ -164,9 +168,11 @@ extension DZURLSession {
/// - onError: called when the request completes successfully. Always called on the main-thread.
/// - Returns: Task
@discardableResult public func PATCH(_ uri: String, query: [String: String] = [:], json: Any?, onSuccess: @escaping SuccessCallback, onError: ErrorCallback?) -> _Concurrency.Task<Void, Never> {
Task {
let boxed = SendableBox(json)

return Task {
do {
let (responseObject, response) = try await request(with: uri, method: "PATCH", query: query, body: json)
let (responseObject, response) = try await request(with: uri, method: "PATCH", query: query, body: boxed.get())
DispatchQueue.main.async { onSuccess(responseObject, response) }
}
catch {
Expand All @@ -184,9 +190,11 @@ extension DZURLSession {
/// - onError: called when the request completes successfully. Always called on the main-thread.
/// - Returns: Task
@discardableResult public func DELETE(_ uri: String, query: [String: String] = [:], body: Any?, onSuccess: @escaping SuccessCallback, onError: ErrorCallback?) -> _Concurrency.Task<Void, Never> {
Task {
let boxed = SendableBox(body)

return Task {
do {
let (responseObject, response) = try await request(with: uri, method: "DELETE", query: query, body: body)
let (responseObject, response) = try await request(with: uri, method: "DELETE", query: query, body: boxed.get())
DispatchQueue.main.async { onSuccess(responseObject, response) }
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion Sources/DZNetworking/Core/DZURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public typealias RedirectModifierBlock = (_ task: URLSessionTask, _ request: URL
/// let (data, _) = try await session.GET("/posts", query: ["userID": "1"])
/// ```
///
open class DZURLSession: NSObject {
open class DZURLSession: NSObject, @unchecked Sendable {
// MARK: Public

/// shared instance, configured with `URLSessionConfiguration.default` and a privately managed operation queue
Expand Down
8 changes: 6 additions & 2 deletions Sources/DZNetworking/Core/DZUploadSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import Foundation
/// ```
///
public final class DZUploadSession: NSObject {
static public let shared = DZUploadSession()
nonisolated(unsafe) static public let shared = DZUploadSession()

public let session = DZURLSession()

public func upload(file: URL, fieldName: String, uri: String, query: [String: String]?, parameters: [String: String]? = nil, contentType: String = "application/octet-stream") async throws -> (Any?, HTTPURLResponse) {
public func upload(file: URL, fieldName: String, uri: String, query: [String: String]?, parameters: [String: String]? = nil, contentType: String = "application/octet-stream") async throws -> (Data?, HTTPURLResponse) {

let multiPartData = MultipartFormData()

Expand All @@ -53,6 +53,10 @@ public final class DZUploadSession: NSObject {

let (result, response) = try await session.request(with: uri, method: HTTPMethod.POST.rawValue, query: query, body: multiPartData)

guard let result = result as? Data else {
throw dzError(code: 500, description: NSLocalizedString("Invalid Response", comment: ""), failure: nil)
}

return (result, response)
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/DZNetworking/Core/TaskHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ internal final class TaskHandlersDictionary {
/// TaskHandler manages delegate calls from the URLSession and dispatching of success/failure callbacks as tasks are completed.
///
/// This is internally handled by the `DZURLSession` class and shouldn't be required to use directly unless you're subclassing `DZURLSession`.
public final class TaskHandler: NSObject {
public final class TaskHandler: NSObject, @unchecked Sendable {
var completionHandler: (() -> Void)?

var handlers = TaskHandlersDictionary()
var handlers = TaskHandlersDictionary()
}

// MARK: - URLSessionDelegate
Expand Down Expand Up @@ -74,7 +74,6 @@ extension TaskHandler: URLSessionTaskDelegate {

// MARK: - URLSessionDataTaskDelegate
extension TaskHandler: URLSessionDataDelegate {

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse) async -> URLSession.ResponseDisposition {
return .allow
}
Expand Down Expand Up @@ -104,7 +103,6 @@ extension TaskHandler: URLSessionDataDelegate {
handlers[downloadTask] = handler
handlers[dataTask] = nil
}

}

// MARK: - URLSessionDownloadDelegate
Expand Down
32 changes: 32 additions & 0 deletions Sources/DZNetworking/Utilities/DZConcurrencyUtilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// DZConcurrencyUtilities.swift
// Networking
//
// Created by Nikhil Nigade on 09/07/24.
//

import Foundation

internal struct SendableBox<T>: @unchecked Sendable {
let wrappedValue: T

init(_ wrappedValue: T) {
self.wrappedValue = wrappedValue
}

func get() -> T {
wrappedValue
}
}

internal struct SendableHashableBox<T: Hashable>: @unchecked Sendable {
let wrappedValue: T

init(_ wrappedValue: T) {
self.wrappedValue = wrappedValue
}

func get() -> T {
wrappedValue
}
}

0 comments on commit b6a9e2b

Please sign in to comment.