Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Adds a collect method to the HTTP Client response #36331

Merged
merged 5 commits into from
Feb 21, 2021
Merged

[8.x] Adds a collect method to the HTTP Client response #36331

merged 5 commits into from
Feb 21, 2021

Conversation

lukeraymonddowning
Copy link
Contributor

Hi all,

This PR is a DX improvement for retrieving HTTP REST responses as a Collection when using the HTTP Client.

Here's an example:

Http::get("https://api.foo.bar/users")->collect();

This is syntactical sugar for:

collect(Http::get("https://api.foo.bar/users")->json());

I feel that this allows for a more declarative experience, especially as more often than not the next step is to map the response into another object.

return Http::get("https://api.foo.bar/users")
    ->collect()
    ->map(fn($data) => new DataObject($data['name'], $data['description']));

Would be glad to hear your thoughts!

Thanks for all the hard work,
Luke

@BrandonSurowiec
Copy link
Contributor

BrandonSurowiec commented Feb 20, 2021

Neat idea! What about accepting a $key = null param on collect() to pass through to the ->json() method? Or does that look out of place on a collect method?

return Http::get("https://api.foo.bar/users")
    ->collect('activeUsers')
    ->map(fn($data) => new DataObject($data['name'], $data['description']));

…t of the json response before wrapping it in the collection.
@lukeraymonddowning
Copy link
Contributor Author

lukeraymonddowning commented Feb 20, 2021

Neat idea! What about accepting a $key = null param on collect() to pass through to the ->json() method? Or does that look out of place on a collect method?

return Http::get("https://api.foo.bar/users")
    ->collect('activeUsers')
    ->map(fn($data) => new DataObject($data['name'], $data['description']));

I like it personally. I think it makes sense if you have a data or results node on the response, as per your example. I'm happy to remove if others think otherwise. I think in this context it works well.

"Collect the activeUsers in the response"

@taylorotwell taylorotwell merged commit bb9078a into laravel:8.x Feb 21, 2021
@GrahamCampbell GrahamCampbell changed the title [8.x] Adds a collect method to the HTTP Client response. [8.x] Adds a collect method to the HTTP Client response Feb 21, 2021
@lukeraymonddowning lukeraymonddowning deleted the http_response_collect_method branch April 17, 2023 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants