csalex
Phoenix API batch endpoint
In our Phoenix API we have implemented a batch endpoint that executes multiple requests in a single network call. It uses Task.async_stream/3 and Phoenix.ConnTest.dispatch/5 to execute each eachrequest asynchronously and return a unified response once all requests are executed.
So far it works as expected, although we use a test related functionality (dispatch/5 is defined in Phoenix.ConnTest).
I came across an issue when an endpoint expects both query and body params. dispatch/5 expects both body and query params to be passed on body_or_params (the 5th argument of the function) and automatically converts them to query or body params based on the HTTP request method. It seems like a distinction between body and query params is not possible, judging from the source code.
I tried to explicitly set those fields on the %Plug.Conn{} struct before dispatching but again, looking at the dispatch/5 source code, the connection is recycled, essentially keeping only some cookies and relevant headers, bringing me back to the use of body_or_params parameter.
The above raise my following questions:
- Is there a way to differentiate body and query params using the existing functionality?
- On a higher level, using a test related function to achieve this batching feature does not feel as the best approach, is there any alternative to that?
Marked As Solved
krasenyp
You might want to look into using YourRouter.call/2 instead of Phoenix.ConnTest.dispatch/5. Using the latter in a non-test setup just feels strange to me.
Also Liked
csalex
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









