Add request object #563

Merged
merged 12 commits into from Nov 2, 2014

Projects

None yet

3 participants

@rmccue
Member
rmccue commented Nov 1, 2014

This is essentially a bag of everything relating to the request.

Fixes #552.

@rmccue
Member
rmccue commented Nov 1, 2014

@WP-API/amigos #reviewmerge

@rmccue rmccue added this to the 2.0 milestone Nov 1, 2014
rmccue added some commits Nov 1, 2014
@rmccue rmccue Switch to single argument for controller arguments 08e42a3
@rmccue rmccue Update json_pre_dispatch to also send request object f0e5844
@rmccue rmccue Include params parsed from the route
Also changes get_param to use POST (if relevant), then GET, then the URL
parameters. This mirrors the previous behaviour from v1.
a82fdc6
@danielbachhuber danielbachhuber commented on the diff Nov 1, 2014
lib/class-wp-json-request.php
+ /**
+ * Body data
+ *
+ * @var string Binary data from the request
+ */
+ protected $body = null;
+
+ /**
+ * Route matched for the request
+ *
+ * @var string
+ */
+ protected $route;
+
+ /**
+ * Attributes (options) for the route that was matched
@danielbachhuber
danielbachhuber Nov 1, 2014 Member

What are some example options?

@rmccue
rmccue Nov 2, 2014 Member

Added documentation; this is the array used in the registration.

@danielbachhuber
Member

Generally, I like this idea a lot. The conservative side of me wonders if it's too magical for core.

@nacin @markjaquith opinions? To summarize, the default argument to our new WP_JSON_Controller class is object implementing ArrayAccess so it can also be used as an array.

@danielbachhuber danielbachhuber commented on the diff Nov 1, 2014
lib/class-wp-json-request.php
*/
-class WP_JSON_Request {
+class WP_JSON_Request implements ArrayAccess {
+ /**
+ * HTTP method
+ *
+ * @var string
+ */
+ protected $method = '';
@danielbachhuber
danielbachhuber Nov 1, 2014 Member

Should this default to GET?

@rmccue
rmccue Nov 2, 2014 Member

I think it's better to not default here so that it's evident if it hasn't been set. I'd even be happy with defaulting to null.

@danielbachhuber danielbachhuber commented on the diff Nov 1, 2014
lib/class-wp-json-server.php
// Compatibility for clients that can't use PUT/PATCH/DELETE
if ( isset( $_GET['_method'] ) ) {
- $this->method = strtoupper( $_GET['_method'] );
+ $request->set_method( strtoupper( $_GET['_method'] ) );
@danielbachhuber
danielbachhuber Nov 1, 2014 Member

Should the strtoupper() happen within set_method() so we can be sure it always conforms? I see you're already using it twice here.

@TimothyBJacobs
TimothyBJacobs Nov 1, 2014

+1 to sanitizing inside the set method.

@rmccue
rmccue Nov 1, 2014 Member

I agree, I kept it this way to match PSR-7 though. I've no strong argument either way, but it's nice being compatible-ish with PSR-7.

@TimothyBJacobs
TimothyBJacobs Nov 2, 2014

Does PSR-7 mean that two strings with the same value, but different case are different methods?

@danielbachhuber danielbachhuber commented on an outdated diff Nov 1, 2014
@@ -518,6 +518,22 @@ function json_url( $path = '', $scheme = 'json' ) {
}
/**
+ * Ensure request arguments are a request object.
+ *
+ * This ensures that the request is consistent.
+ *
+ * @param array|WP_JSON_Request $reques Request to check.
@danielbachhuber
danielbachhuber Nov 1, 2014 Member

Typo: $reques

@rmccue
Member
rmccue commented Nov 2, 2014

Updated as per review.

Generally, I like this idea a lot. The conservative side of me wonders if it's too magical for core.

The ArrayAccess is a bit magical, but I don't think it's any worse than things like WP_Post using the post fields or falling back to meta otherwise.

I'm going to go ahead and merge this, as I think we're OK with the idea as a whole, and we can PR any other changes we want here.

@rmccue rmccue merged commit 5478d4f into joes-registration Nov 2, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@rmccue rmccue deleted the two-request-object branch Nov 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment