postman-code-generators icon indicating copy to clipboard operation
postman-code-generators copied to clipboard

Code Generator for Swift Async/Await

Open idelfonsog2 opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. It's not related to a problem

Describe the solution you'd like In the Swift Codegen for URLSession the template is given a "closure" based approach. The language has moved away from 1+ years now. I would like to update the template

Additional context

Previously

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

var semaphore = DispatchSemaphore (value: 0)

var request = URLRequest(url: URL(string: "https://test-websvcs.nm.org/BusAdmin/Services/WebAPI/NMEmployeeMiddleTier/NMConnectAdmin/CurrentDate")!,timeoutInterval: Double.infinity)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { data, response, error in 
  guard let data = data else {
    print(String(describing: error))
    semaphore.signal()
    return
  }
  print(String(data: data, encoding: .utf8)!)
  semaphore.signal()
}

task.resume()
semaphore.wait()

Now

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

var request = URLRequest(url: URL(string: "https://test-websvcs.nm.org/BusAdmin/Services/WebAPI/NMEmployeeMiddleTier/NMConnectAdmin/CurrentDate")!,timeoutInterval: Double.infinity)
request.httpMethod = "GET"

let (data, response) = try await URLSession.shared.data(from: request)

guard let data = data else {
  print(String(describing: error))
  return
}

print(String(data: data, encoding: .utf8)!)

idelfonsog2 avatar Aug 19 '22 16:08 idelfonsog2

@idelfonsog2 thank you for suggesting this improvement. I see that async/await has been added to Swift recently. Some people would still want to use the older style due to legacy codebases. We can add a toggle to generate code using the preferred style.

Feel free to create a PR for this change if you're primarily working with Swift.

dhwaneetbhatt avatar Mar 08 '23 00:03 dhwaneetbhatt