postmanlabs / postman-code-generators

Common repository for all code generators shipped with Postman
Apache License 2.0
981 stars 348 forks source link

Swift Async #720

Closed maxxfrazer closed 10 months ago

maxxfrazer commented 11 months ago

The async code looks like:

do {
    let (data, response) = try await URLSession.shared.data(for: request)
    print(String(data: data, encoding: .utf8)!)
    exit(EXIT_SUCCESS)
} catch {
    print("A URL error occurred: \(error)")
    exit(EXIT_FAILURE)
}

Current non-async code:

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

IMO the async method should be default, but wanted to get your feedback first!

I have noted in ADDITIONAL_DEPENDENCIES.md that the swift version should be a minimum of 5.5 (released Sept 2021)

maxxfrazer commented 11 months ago

@dhwaneetbhatt @SahilChoudhary22 you helped me with my last Swift codegen update, hopefully you see this as an improvement too!

maxxfrazer commented 11 months ago

TIL await URLSession.shared.data isn't available on linux? will take a deeper dive.

maxxfrazer commented 10 months ago

Going to skip this PR. Unfortunately async isn’t available on Linux exactly the same way as it is for iOS/macOS etc. I think a huge majority of Swift developers would benefit from it, but unfortunately not all of them.