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 #2484

Merged
merged 3 commits into from
Dec 3, 2021
Merged
Changes from all commits
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
95 changes: 95 additions & 0 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1336,4 +1336,99 @@ public function call_ep_cli_put_mapping( $index_meta, $indexable ) {
*/
do_action( 'ep_cli_put_mapping', $indexable, $this->args, $this->assoc_args );
}

/**
* 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
*
* @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'] : '';
$request_args = [
'method' => $method,
];
if ( 'GET' !== $method && ! empty( $body ) ) {
$request_args['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(
/* 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' );

if ( is_wp_error( $response ) ) {
WP_CLI::error( $response->get_error_message() );
}

$response_body = wp_remote_retrieve_body( $response );
$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 );
}

WP_CLI::log( $response_body );
}
}