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

Add a "request" subcommand #2523

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Docs + Formatting + conditional json output
  • Loading branch information
felipeelia committed Dec 15, 2021
commit fb0fd258d40f391c3a872e19b8b320163cd147ea
84 changes: 68 additions & 16 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1811,30 +1811,79 @@ public static function skip_number_format_i18n( $formatted, $number, $decimals )
/**
* Send a HTTP request to Elasticsearch
*
* ## OPTIONS
*
* <path>
* : Path of the request. Example: `_cat/indices`
*
* [--method=<method>]
* : HTTP Method (GET, POST, etc.)
*
* [--body=<json-body>]
* : Request body
*
* [--debug-http-request]
* : Enable debugging
*
* @subcommand request
* @synopsis <path> [--method=<method>] [--body=<json-body>] [--debug-http-request]
*
* @since 4.0.0
*
* @param array $args Positional CLI args.
* @param array $assoc_args Associative CLI args.
*/
public function request( $args, $assoc_args ) {
$path = $args[0];
$method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET';
$body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : '';
$path = $args[0];
$method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET';
$body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : '';
$request_args = [
'method' => $method,
'body' => $body,
'body' => $body,
];

if ( ! empty( $assoc_args['debug-http-request'] ) ) {
add_filter( 'http_api_debug', function ( $response, $context, $transport, $request_args, $url ) {
WP_CLI::line( sprintf( 'URL: %s', $url ) );
WP_CLI::line( sprintf( 'Request Args: %s', print_r( $request_args, true ) ) );
WP_CLI::line( sprintf( 'Transport: %s', $transport ) );
WP_CLI::line( sprintf( 'Context: %s', $context ) );
WP_CLI::line( sprintf( 'Response: %s', print_r( $response, true ) ) );

}, 10, 5 );
add_filter(
'http_api_debug',
function ( $response, $context, $transport, $request_args, $url ) {
WP_CLI::line(
sprintf(
/* translators: URL of the request */
esc_html__( 'URL: %s', 'elasticpress' ),
$url
)
);
WP_CLI::line(
sprintf(
/* translators: Request arguments (outputted with print_r()) */
esc_html__( 'Request Args: %s', 'elasticpress' ),
print_r( $request_args, true )
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP transport used */
esc_html__( 'Transport: %s', 'elasticpress' ),
$transport
)
);
WP_CLI::line(
sprintf(
/* translators: Context under which the http_api_debug hook is fired */
esc_html__( 'Context: %s', 'elasticpress' ),
$context
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP response (outputted with print_r()) */
esc_html__( 'Response: %s', 'elasticpress' ),
print_r( $response, true )
)
);
},
10,
5
);
}
$response = Elasticsearch::factory()->remote_request( $path, $request_args, [], 'wp_cli_request' );

Expand All @@ -1843,9 +1892,12 @@ public function request( $args, $assoc_args ) {
}

$response_body = wp_remote_retrieve_body( $response );
// Re-encode the JSON to add space formatting
$response_body = json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT );
$content_type = wp_remote_retrieve_header( $response, 'Content-Type' );
if ( preg_match( '/json/', $content_type ) ) {
// Re-encode the JSON to add space formatting
$response_body = wp_json_encode( json_decode( $response_body ), JSON_PRETTY_PRINT );
}

echo $response_body;
WP_CLI::log( $response_body );
}
}