Recently, I had the idea to create a Swift HTTP macro that would streamline requests.
This is how I imagine it to work:
@HTTPClient(
baseURL: "https://api.example.com",
contentType: "application/json",
useCookies: true
)
struct MyAPIClient {
@Endpoint(method: .GET, path: "/users")
func getUsers(queryParams: [String: String] = [:], headers: [String: String] = [:], completion: @escaping (Data?, Error?) -> Void)
@Endpoint(method: .POST, path: "/users", contentType: "application/json")
func createUser(body: Data, headers: [String: String] = [:], completion: @escaping (Data?, Error?) -> Void)
@Endpoint(method: .POST, path: "/users", contentType: "application/x-www-form-urlencoded")
func createUserForm(body: [String: String], headers: [String: String] = [:], completion: @escaping (Data?, Error?) -> Void)
}
Then the goal would be to make it work for Apple, Linux, and WebAssembly.
Since I have never created a Swift macro, I’m not sure if it makes sense to use a macro for that or if it would be better to use the Swift OpenAPI generator instead. What do you think?