http
http copied to clipboard
The use of closure as associated value of the processBody case feels unusual
I've been looking through example code for the HTTP library and the use of a associated value closure for the HTTPBodyProcessing.processBody
case feels kind of usual. I've never seen such a pattern used in the Standard Library. And if this library's future is to be integrated into the Standard Library or shipped with Swift, I think it would be worth looking into keeping as close as possible to the API style of the Standard Library.
Any ideas?
I agree that the API isn't particularly nice (in fact I think it is pretty weird :-) ) But it does the job.
So what is the API you suggest @hartbit ?
I don't know if this is any better:
public protocol HTTPRequestHandling: class {
func handle(request: HTTPRequest, response: HTTPResponseWriter, processBody: HTTPBodyHandler)
}
class EchoHandler: HTTPRequestHandling {
func handle(request: HTTPRequest, response: HTTPResponseWrite, processBody: HTTPBodyHandler) {
//Assume the router gave us the right request - at least for now
response.writeHeader(status: .ok, headers: ["Transfer-Encoding": "chunked", "X-foo": "bar"])
processBody { (chunk, stop) in
// handle body
}
}
}
And if the processBody closure is not set yet, it defaults to discard? Yeah, maybe. Looks a little nicer to me.