Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Generator for Swift Async/Await #633

Open
idelfonsog2 opened this issue Aug 19, 2022 · 1 comment
Open

Code Generator for Swift Async/Await #633

idelfonsog2 opened this issue Aug 19, 2022 · 1 comment
Labels

Comments

@idelfonsog2
Copy link

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)!)
@dhwaneetbhatt
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants