From 028b455ff0cf3f1419e9fdef6c5e81eca5d777cd Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Thu, 4 Nov 2021 17:13:13 +0100 Subject: [PATCH 1/4] Add a "request" subcommand To help when debugging, or otherwise wanting to send arbitrary requests to ElasticSearch, this adds a new `request` sub-command, that is some what similar to the idea of `wp db query` or the like. For example `wp elasticpress request /_cat/indices --method=GET` --- includes/classes/Command.php | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index b72a568c5a..9f3bbfc6bb 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -1807,4 +1807,45 @@ protected function render_stats( $current_index, $body ) { public static function skip_number_format_i18n( $formatted, $number, $decimals ) { return number_format( $number, absint( $decimals ), '.', '' ); } + + /** + * Send a HTTP request to Elasticsearch + * + * @subcommand request + * @synopsis [--method=] [--body=] [--debug-http-request] + * + * @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'] : ''; + $request_args = [ + 'method' => $method, + '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 ); + } + $response = Elasticsearch::factory()->remote_request( $path, $request_args, [], 'wp_cli_request' ); + + if ( is_wp_error( $response ) ) { + WP_CLI::error( $response->get_error_message() ); + } + + $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 ); + + echo $response_body; + } } From fb0fd258d40f391c3a872e19b8b320163cd147ea Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 3 Dec 2021 09:52:37 -0300 Subject: [PATCH 2/4] Docs + Formatting + conditional json output --- includes/classes/Command.php | 84 +++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 9f3bbfc6bb..7a31de21d7 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -1811,30 +1811,79 @@ public static function skip_number_format_i18n( $formatted, $number, $decimals ) /** * Send a HTTP request to Elasticsearch * + * ## OPTIONS + * + * + * : Path of the request. Example: `_cat/indices` + * + * [--method=] + * : HTTP Method (GET, POST, etc.) + * + * [--body=] + * : Request body + * + * [--debug-http-request] + * : Enable debugging + * * @subcommand request - * @synopsis [--method=] [--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' ); @@ -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 ); } } From f9b22c047ebb474771c2c1eaf0a97496b01ca993 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 3 Dec 2021 13:29:58 -0300 Subject: [PATCH 3/4] For GET requests don't send the body --- includes/classes/Command.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 7a31de21d7..b2d8be2a1a 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -1838,8 +1838,10 @@ public function request( $args, $assoc_args ) { $body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : ''; $request_args = [ 'method' => $method, - 'body' => $body, ]; + if ( 'GET' !== $method && ! empty( $body ) ) { + $request_args['body'] = $body; + } if ( ! empty( $assoc_args['debug-http-request'] ) ) { add_filter( From 603ec5febb954b85d6a63a6a3787f4a68a4378a2 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 15 Dec 2021 17:55:18 -0300 Subject: [PATCH 4/4] Fix since doc --- includes/classes/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index b2d8be2a1a..4453c43a1f 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -1827,7 +1827,7 @@ public static function skip_number_format_i18n( $formatted, $number, $decimals ) * * @subcommand request * - * @since 4.0.0 + * @since 3.6.6 * * @param array $args Positional CLI args. * @param array $assoc_args Associative CLI args.