http icon indicating copy to clipboard operation
http copied to clipboard

The use of closure as associated value of the processBody case feels unusual

Open hartbit opened this issue 7 years ago • 3 comments

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?

hartbit avatar Nov 08 '17 15:11 hartbit

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 ?

helje5 avatar Nov 08 '17 15:11 helje5

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
        }
    }
}

hartbit avatar Nov 08 '17 15:11 hartbit

And if the processBody closure is not set yet, it defaults to discard? Yeah, maybe. Looks a little nicer to me.

helje5 avatar Nov 08 '17 16:11 helje5