Skip to content

Commit

Permalink
ensure callbacks are called and removed when no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
donnywals committed Nov 20, 2024
1 parent 102a4ed commit cdb1d78
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
16 changes: 9 additions & 7 deletions Sources/TransloaditKit/TransloaditAPI+URLSessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import Foundation

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

defer { callbacks[task] = nil }

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

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

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

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
callbacks[dataTask]?.0?.append(data)
callbacks[dataTask]?.data.append(data)
}
}
19 changes: 11 additions & 8 deletions Sources/TransloaditKit/TransloaditAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ enum TransloaditAPIError: Error {
case couldNotFetchStatus
case couldNotCreateAssembly(Error)
case assemblyError(String)
case incompleteServerResponse
}

/// The `TransloaditAPI` class makes API calls, such as creating assemblies or checking an assembly's status.
final class TransloaditAPI {
final class TransloaditAPI: NSObject {

private let basePath = URL(string: "https://api2.transloadit.com")!

Expand All @@ -40,12 +41,13 @@ final class TransloaditAPI {
}()

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

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

func createAssembly(
Expand All @@ -67,7 +69,7 @@ final class TransloaditAPI {
}

let task = session.dataTask(with: request)
callbacks[task] = (Data(), { result in
callbacks[task] = URLSessionCompletionHandler(callback: { result in
switch result {
case .failure(let error):
completion(.failure(.couldNotCreateAssembly(error)))
Expand Down Expand Up @@ -109,7 +111,8 @@ final class TransloaditAPI {
}

let task = session.dataTask(with: request)
callbacks[task] = (Data(), { result in
//task.delegate = self
callbacks[task] = URLSessionCompletionHandler(callback: { result in
switch result {
case .failure(let error):
completion(.failure(.couldNotCreateAssembly(error)))
Expand Down Expand Up @@ -282,8 +285,8 @@ final class TransloaditAPI {
return request
}

let task = session.dataTask(request: makeRequest())
callbacks[task] = (Data(), { result in
let task = session.dataTask(with: makeRequest())
callbacks[task] = URLSessionCompletionHandler(callback: { result in
switch result {
case .failure:
completion(.failure(.couldNotFetchStatus))
Expand All @@ -308,8 +311,8 @@ final class TransloaditAPI {
return request
}

let task = session.dataTask(request: makeRequest())
callbacks[task] = (Data(), { result in
let task = session.dataTask(with: makeRequest())
callbacks[task] = URLSessionCompletionHandler(callback: { result in
switch result {
case .failure:
completion(.failure(.couldNotFetchStatus))
Expand Down
11 changes: 11 additions & 0 deletions Sources/TransloaditKit/URLSessionCompletionHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

class URLSessionCompletionHandler {
var data: Data
let callback: (Result<(Data, URLResponse), Error>) -> Void

init(callback: @escaping (Result<(Data, URLResponse), Error>) -> Void) {
self.callback = callback
self.data = Data()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct PhotoPicker: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
configuration.selectionLimit = 30
configuration.filter = .images

let picker = PHPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ import Atlantis
final class MyUploader: ObservableObject {
let transloadit: Transloadit

func upload2(_ urls: [URL]) {
let templateID = "1a84d2f1f2584f92981bda285bbc4e84"

transloadit.createAssembly(templateId: templateID, andUpload: urls, customFields: ["hello": "world"]) { result in
switch result {
case .success(let assembly):
print("Retrieved \(assembly)")
case .failure(let error):
print("Assembly error \(error)")
}
}.pollAssemblyStatus { result in
switch result {
case .success(let assemblyStatus):
print("Received assemblystatus \(assemblyStatus)")
case .failure(let error):
print("Caught polling error \(error)")
}
}
}

func upload(_ urls: [URL]) {
let resizeStep = StepFactory.makeResizeStep(width: 200, height: 100)
transloadit.createAssembly(steps: [resizeStep], andUpload: urls, customFields: ["hello": "world"]) { result in
Expand All @@ -32,7 +52,7 @@ final class MyUploader: ObservableObject {
}

init() {
let credentials = Transloadit.Credentials(key: "my_key", secret: "my_secret")
let credentials = Transloadit.Credentials(key: "Ru1rwq3ITrgSEDtgna6SGZa4yY71YJgW", secret: "Xo6xlnn42cfBkNxLSDWJNCQoSNL0j1aFB9wNyaAR")
self.transloadit = Transloadit(credentials: credentials, session: URLSession.shared)
self.transloadit.fileDelegate = self
}
Expand Down Expand Up @@ -90,7 +110,7 @@ struct TransloaditKitExampleApp: App {

init() {
self.uploader = MyUploader()
// Atlantis.start(hostName: "")
Atlantis.start(hostName: "donnys-macbook-pro-2.local.")
}

var body: some Scene {
Expand Down

0 comments on commit cdb1d78

Please sign in to comment.