This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Setting response data directly via the server acts like a form of global state. It makes it much harder to embed requests in each other (e.g.
/post/2
embeds a call for its post parent to/post/1
), since the headers are always sent. This is a little crappy, since the API is designed to be composible, and this is standing in the way of that.However, we don't want this to be overly complicated to use. The benefit of returning raw data is you can work with it easily.
Proposal
WP_JSON_ResponseInterface
interface which can be returned from any endpoint with the following methods:get_headers()
- Returns an array of headers which can be sentget_status()
- Returns an integer code for HTTP response statusget_data()
- Returns whatever data it's encapsulatingjsonSerialize()
- A wrapper to callget_data()
in most cases, allowing this interface to work right off the bat after Introduce support for JsonSerializable聽#24 is merged, without changing the server classesWP_JSON_Response
class which implementsWP_JSON_ResponseInterface
and adds the following methods:__construct($data, $status = 200, $headers = array())
header($key, $value, $replace = true)
- Set a single header (replacesWP_JSON_Server::header()
)link_header($rel, $link, $other = array())
- Set a single link header (replacesWP_JSON_Server::link_header()
)set_headers($headers)
- Set all headersset_status($code)
- Set the status code (replacesWP_JSON_Server::send_status
)After #24 is merged, this can be dropped in with no changes, but will only send the data. We can then work on moving all the status/header setting code to the response object, and the sending code to the server.
Bad Parts
$response->get_data()
instead of just$response
) - On the other hand: You can now access extra data that you couldn't before (status/headers)WP_Error
- Maybe somedayWP_Error
can extendWP_JSON_ResponseInterface
馃挱Further feedback much appreciated.