4 ms·
Never understood why people prefer APIs like this: page1, page2, ... = HTTPX.get("/page1", "/page2", ...) Why encode parallelism into a single polymorphic f
by 0xFACEFEED 7y ago
Never understood why people prefer APIs like this:
page1, page2, ... = HTTPX.get("/page1", "/page2", ...)
Why encode parallelism into a single polymorphic function? Why not use composition instead? Now you're stuck with this API forever. And if you want to customize parallel fetches (eg: batching) you'll need another different looking API.
- cranej 7y agoAnd I guess it won't return unless all pages are fetched? even worse...
- chucke 7y agoThere are timeouts involved, that is, you'll get error responses. Feel free to try the example scripts.
- cranej 7y ago> ...while baking in support for concurrent requests... Timeout does not help here - if the api is for concurrent requests, it's bad that caller cannot do anything before `(the slowest request finished || timeout exceeded)`.
- chucke 7y agoHi, I maintain httpx. I'll try to answer your concerns. The API was not a question of preference. If you look at most http lib APIs (python requests included), a single call returns a response object. My goal was to keep compatibility with this API to ease the migration effort, while baking in support for concurrent requests, which was the whole point of the lib. There is no parallelism in that call btw. It's in the Readme. If it's http/2, it'll multiplex requests. If it's http/1, it'll try to pipeline, and if doesn't work, it'll fetch them one by one (with or without keep-alive). If they're different origins, the requests will be managed inside an event loop. > Why not use composition instead? You'll have to explain with an example of your own. However, there's already a ticket for implementing an event-based request handling, I just never prioritized it because no one asked for it yet, no one has shown interest in contributing, and frankly, I haven't needed it yet. > Now you're stuck with this API forever. It's still early 0.x days, so I might still remove APIs I don't think are future proof. That said, a lot of libs have lived with similar APIs all these years, and I don't see any reason for removing it.
- sudhirj 7y ago> It's still early 0.x days Any idea why the project is posted now? Is there a beta or RC release that’s significant?
- bovermyer 7y agoMy guess is that someone found it interesting and worth sharing with the HN community.
- jbverschoor 7y agoSuperb syntactical sugar.
- michaelbuckbee 7y agoHey, very impressed with httpx. I'm not in a spot to contribute code but would like to sponsor (if that's an option). My email's in my HN profile and apologies for dropping this into the thread I couldn't find contact info elsewhere.
- chucke 7y agoThx for the kind words. I thank, but will not accept financial contributions from individuals. Feel free to use it, modify it, or talk about it to your colleagues :)
- maximegarcia 7y agoNice lib, eager to use it in replacement of HTTP.rb, auto managed persistent connection is a great addition. Do you plan to support webmock and httplog ? Or add support for httpx into those ? Can't integrate a networking lib that can't be mocked in my specs.
- chucke 7y agoThx! I don't have any plans to integrate them, as I don't use any of them. But it should be trivial to create a plugin for them using the httpx plugin system: https://honeyryderchuck.gitlab.io/httpx/wiki/Custom-Plugins https://honeyryderchuck.gitlab.io/httpx/wiki/Custom-Plugins , feel free to give it a try. httpx also ships with its own request/response logger, set HTTPX_DEBUG=1 or set the options so that you see an example.
- pmontra 7y agoDo you mean, that vs something like this? responses = HTTPX.get("/page1"). get("/page2"). start My guess is that the focus of HTTPX being parallel requests, the author choose an API that explicitly groups them together. The documentation is pretty sparse, I wonder if we can pass an array of URLs to get().
- fouc 7y agoyes, ruby has a splat * operator that will basically expand an array into a set of arguments. array = ["https://news.ycombinator.com/news", "https://news.ycombinator.com/news?p=2", "https://news.ycombinator.com/news?p=3"] page1, page2, page3 = HTTPX.get(*array)
- stormbrew 7y agoNote you can also splat the return as well: *pages = HTTPX.get(*urls)
- lloeki 7y agosplat? urls = ['https://example.com/foo', 'https://example.com/bar'] HTTPX.get(*urls)