Skip to content

Commit

Permalink
Merge pull request #76 from wp-cli/headers-envelope-formt
Browse files Browse the repository at this point in the history
Add `--format=headers` and `--format=envelope` to `list` and `get`
  • Loading branch information
danielbachhuber authored Jul 11, 2016
2 parents a46bb23 + fe1be22 commit c41e737
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
18 changes: 18 additions & 0 deletions features/comment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ Feature: Manage WordPress comments through the REST API
[{"author_name":"Mr WordPress"}]
"""

When I run `wp rest comment list --format=headers`
Then STDOUT should be JSON containing:
"""
{"X-WP-TotalPages":1}
"""

When I run `wp rest comment list --format=envelope`
Then STDOUT should be JSON containing:
"""
{"headers":{"X-WP-TotalPages":1}}
"""

Scenario: List comments with different contexts
When I run `wp rest comment list --context=embed --format=csv`
Then STDOUT should contain:
Expand Down Expand Up @@ -98,6 +110,12 @@ Feature: Manage WordPress comments through the REST API
{"author_name":"Mr WordPress"}
"""

When I run `wp rest comment get 1 --format=envelope`
Then STDOUT should be JSON containing:
"""
{"body":{"author_name":"Mr WordPress"}}
"""

Scenario: Create a comment
When I run `wp rest comment create --post=1 --content="Hello World, again" --user=1`
Then STDOUT should contain:
Expand Down
24 changes: 20 additions & 4 deletions inc/RestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function delete_item( $args, $assoc_args ) {
* @subcommand get
*/
public function get_item( $args, $assoc_args ) {
list( $status, $body ) = $this->do_request( 'GET', $this->get_filled_route( $args ), $assoc_args );
list( $status, $body, $headers ) = $this->do_request( 'GET', $this->get_filled_route( $args ), $assoc_args );

if ( ! empty( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
Expand All @@ -137,8 +137,16 @@ public function get_item( $args, $assoc_args ) {
}
}

if ( ! empty( $assoc_args['format'] ) && 'body' === $assoc_args['format'] ) {
if ( 'headers' === $assoc_args['format'] ) {
echo json_encode( $headers );
} else if ( 'body' === $assoc_args['format'] ) {
echo json_encode( $body );
} else if ( 'envelope' === $assoc_args['format'] ) {
echo json_encode( array(
'body' => $body,
'headers' => $headers,
'status' => $status,
) );
} else {
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $body );
Expand Down Expand Up @@ -177,8 +185,16 @@ public function list_items( $args, $assoc_args ) {

if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
echo (int) $headers['X-WP-Total'];
} else if ( ! empty( $assoc_args['format'] ) && 'body' === $assoc_args['format'] ) {
} else if ( 'headers' === $assoc_args['format'] ) {
echo json_encode( $headers );
} else if ( 'body' === $assoc_args['format'] ) {
echo json_encode( $body );
} else if ( 'envelope' === $assoc_args['format'] ) {
echo json_encode( array(
'body' => $body,
'headers' => $headers,
'status' => $status,
) );
} else {
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $items );
Expand Down Expand Up @@ -341,7 +357,7 @@ private function do_request( $method, $route, $assoc_args ) {
}
}
}
return array( $response->status_code, json_decode( $response->body, true ), $response->headers );
return array( $response->status_code, json_decode( $response->body, true ), $response->headers->getAll() );
}
WP_CLI::error( 'Invalid scope for REST command.' );
}
Expand Down
2 changes: 2 additions & 0 deletions inc/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ private static function register_route_commands( $rest_command, $route, $route_d
'ids',
'yaml',
'count',
'headers',
'body',
'envelope',
),
);
}
Expand Down

0 comments on commit c41e737

Please sign in to comment.