Skip to content

Commit

Permalink
Started migrating to a delegate based URLSession task system
Browse files Browse the repository at this point in the history
  • Loading branch information
donnywals committed Nov 19, 2024
1 parent 7db7982 commit 102a4ed
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
25 changes: 25 additions & 0 deletions Sources/TransloaditKit/TransloaditAPI+URLSessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

extension TransloaditAPI: URLSessionDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let callback = callbacks[task]?.1 else {
return
}

if let error {
callback(.failure(error))
return
}

guard let data = callbacks[task]?.0, let response = task.response else {
//callback(.failure(TransloaditAPIError.unknown))
return
}

callback(.success((data, response)))
}

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
callbacks[dataTask]?.0?.append(data)
}
}
62 changes: 36 additions & 26 deletions Sources/TransloaditKit/TransloaditAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ final class TransloaditAPI {
case assemblies = "/assemblies"
}

private let session: URLSession
private let configuration: URLSessionConfiguration
private let delegateQueue: OperationQueue
private lazy var session: URLSession = {
return URLSession(configuration: configuration, delegate: self, delegateQueue: delegateQueue)
}()

static private let formatter: DateFormatter = {
let dateFormatter = DateFormatter()
Expand All @@ -36,10 +40,12 @@ final class TransloaditAPI {
}()

private let credentials: Transloadit.Credentials
var callbacks: [URLSessionTask: (Data?, (Result<(Data, URLResponse), Error>) -> Void)] = [:]

init(credentials: Transloadit.Credentials, session: URLSession) {
self.credentials = credentials
self.session = session
self.configuration = session.configuration
self.delegateQueue = session.delegateQueue
}

func createAssembly(
Expand All @@ -60,13 +66,17 @@ final class TransloaditAPI {
return
}

let task = session.dataTask(with: request) { (data, response, error) in
if let data = data {
let task = session.dataTask(with: request)
callbacks[task] = (Data(), { result in
switch result {
case .failure(let error):
completion(.failure(.couldNotCreateAssembly(error)))
case .success((let data, _)):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let assembly = try decoder.decode(Assembly.self, from: data)

if let error = assembly.error {
completion(.failure(.assemblyError(error)))
} else {
Expand All @@ -76,7 +86,7 @@ final class TransloaditAPI {
completion(.failure(TransloaditAPIError.couldNotCreateAssembly(error)))
}
}
}
})
task.resume()
}

Expand All @@ -98,13 +108,17 @@ final class TransloaditAPI {
return
}

let task = session.dataTask(with: request) { (data, response, error) in
if let data = data {
let task = session.dataTask(with: request)
callbacks[task] = (Data(), { result in
switch result {
case .failure(let error):
completion(.failure(.couldNotCreateAssembly(error)))
case .success((let data, _)):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let assembly = try decoder.decode(Assembly.self, from: data)

if let error = assembly.error {
completion(.failure(.assemblyError(error)))
} else {
Expand All @@ -114,7 +128,7 @@ final class TransloaditAPI {
completion(.failure(TransloaditAPIError.couldNotCreateAssembly(error)))
}
}
}
})
task.resume()
}

Expand Down Expand Up @@ -268,9 +282,12 @@ final class TransloaditAPI {
return request
}

let task = session.dataTask(request: makeRequest()) { result in
let task = session.dataTask(request: makeRequest())
callbacks[task] = (Data(), { result in
switch result {
case .success((let data?, _)):
case .failure:
completion(.failure(.couldNotFetchStatus))
case .success((let data, _)):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
Expand All @@ -279,13 +296,8 @@ final class TransloaditAPI {
} catch {
completion(.failure(.couldNotFetchStatus))
}
case .success((nil, _)):
completion(.failure(.couldNotFetchStatus))
case .failure:
completion(.failure(.couldNotFetchStatus))
}
}

})
task.resume()
}

Expand All @@ -296,9 +308,12 @@ final class TransloaditAPI {
return request
}

let task = session.dataTask(request: makeRequest()) { result in
let task = session.dataTask(request: makeRequest())
callbacks[task] = (Data(), { result in
switch result {
case .success((let data?, _)):
case .failure:
completion(.failure(.couldNotFetchStatus))
case .success((let data, _)):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
Expand All @@ -307,13 +322,8 @@ final class TransloaditAPI {
} catch {
completion(.failure(.couldNotFetchStatus))
}
case .success((nil, _)):
completion(.failure(.couldNotFetchStatus))
case .failure:
completion(.failure(.couldNotFetchStatus))
}
}

})
task.resume()
}
}
Expand Down

0 comments on commit 102a4ed

Please sign in to comment.