Skip to content

[HttpClient] add ResponseInterface::toArray() #30499

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

Merged
merged 1 commit into from
Mar 10, 2019

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

I'd like we discuss adding a toArray() method to ResponseInterface.

JSON responses are so common when doing server-side requests that this may help remove boilerplate - especially the logic dealing with errors.

WDYT?

(about flags, I don't think we should make them configurable: if one really needs to deal with custom flags, there's always ResponseInterface::getContent() - but it should be very rare.).

}

if (!\is_array($content)) {
throw new JsonException(sprintf('Response returned %s while an array was expected.', \gettype($content)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quoted %s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to:
JSON content was expected to decode to an array, %s returned.

@@ -59,6 +59,18 @@ public function getHeaders(bool $throw = true): array;
*/
public function getContent(bool $throw = true): string;

/**
* Gets the response body decoded as array, typically from a JSON payload.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean component will deal with e.g. XML if requested? What if ppl want to leverage a serializer for this?

Honestly, do want the API? :D

From #30502

ppl can always encode/decode on their own if they really want precise control on these

yet, we kinda want this 30+ line method :) should it be util instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration with Serializer should be provided by decoration.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I see no reasons why integration with Serializer should implement HttpClientInterface: turning strings (even when coming from response bodies) to objects is not the domain of an http client to me.

Copy link
Contributor

@ro0NL ro0NL Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case i think we need to update the description to something like Gets a decoded body array representation (typically ...).

Let's say i decorate this toArray to mutate data, but im passing this client globally around in my app; some services might be effected by this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I'm not sure to spot the difference, semantically :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed your comment, you're right we can expect HTTP domain; thus the body decoded as-is.

👍 with a simple decoration solution for extension in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would also help #30494 btw

@nicolas-grekas nicolas-grekas merged commit aabd1d4 into symfony:master Mar 10, 2019
nicolas-grekas added a commit that referenced this pull request Mar 10, 2019
…-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[HttpClient] add ResponseInterface::toArray()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

I'd like we discuss adding a `toArray()` method to `ResponseInterface`.

JSON responses are so common when doing server-side requests that this may help remove boilerplate - especially the logic dealing with errors.

WDYT?

(about flags, I don't think we should make them configurable: if one really needs to deal with custom flags, there's always `ResponseInterface::getContent()` - but it should be very rare.).

Commits
-------

aabd1d4 [HttpClient] add ResponseInterface::toArray()
@nicolas-grekas nicolas-grekas deleted the hc-response-toarray branch March 10, 2019 17:26
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants