async-http-client
async-http-client copied to clipboard
Declarative API
I wonder if when function builders have formally been added to Swift a declarative API with function builders could be made akin to this library: https://github.com/carson-katri/swift-request
We certainly could. However, I remain unconvinced that a declarative syntax actually buys us that much.
Here is the readme’s example of a complex request:
Request {
Url("https://jsonplaceholder.typicode.com/posts")
Method(.post)
Header.ContentType(.json)
Body(Json {
JsonProperty(key: "title", value: "foo")
JsonProperty(key: "body", value: "bar")
JsonProperty(key: "usedId", value: 1)
}.string)
}
Is this actually more concise than our API? It doesn’t seem that it is. Nor does it seem clearer, at least to me. It also raises many questions: what happens if you specify URL twice? What about method? Are header names custom coded requiring a new method for any header name you want, or is it a dynamic property lookup that analyses the string to determine hyphenation?
I am profoundly unconvinced that a declarative style actually gains us anything in the area of declaring HTTP request objects.
Incidentally, the part of this that may be worth analysing is the chained and grouped requests feature. We could build sugar on top of our API to allow that DSL style of declaring control flow. However bear in mind that the control flow there does not allow parameter passing or any kind of data flow from one request to another; so it’s unlikely to be useful in non-trivial programs.
Sorry for the slow response - these are all valid points.
I had come across that library in a few threads and thought it might be something worthwhile to sit on awhile :)