response_factory = $response_factory;
$this->stream_factory = $stream_factory;
}
/**
* Sends a PSR-7 request and returns a PSR-7 response.
*
* @since 7.0.0
*
* @param RequestInterface $request The PSR-7 request.
* @return ResponseInterface The PSR-7 response.
*
* @throws NetworkException If the WordPress HTTP request fails.
*/
public function sendRequest( RequestInterface $request ): ResponseInterface {
$args = $this->prepare_wp_args( $request );
$url = (string) $request->getUri();
$response = wp_safe_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
$message = sprintf(
/* translators: 1: HTTP method (e.g. GET, POST). 2: Request URL. 3: Error message. */
__( 'Network error occurred while sending %1$s request to %2$s: %3$s' ),
$request->getMethod(),
$url,
$response->get_error_message()
);
throw new NetworkException( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
return $this->create_psr_response( $response );
}
/**
* Sends a PSR-7 request with transport options and returns a PSR-7 response.
*
* @since 7.0.0
*
* @param RequestInterface $request The PSR-7 request.
* @param RequestOptions $options Transport options for the request.
* @return ResponseInterface The PSR-7 response.
*
* @throws NetworkException If the WordPress HTTP request fails.
*/
public function sendRequestWithOptions( RequestInterface $request, RequestOptions $options ): ResponseInterface {
$args = $this->prepare_wp_args( $request, $options );
$url = (string) $request->getUri();
$response = wp_safe_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
$message = sprintf(
/* translators: 1: Request URL. 2: Error message. */
__( 'Network error occurred while sending request to %1$s: %2$s' ),
$url,
$response->get_error_message()
);
throw new NetworkException(
$message, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
$response->get_error_code() ? (int) $response->get_error_code() : 0
);
}
return $this->create_psr_response( $response );
}
/**
* Prepares WordPress HTTP API arguments from a PSR-7 request.
*
* @since 7.0.0
*
* @param RequestInterface $request The PSR-7 request.
* @param RequestOptions|null $options Optional transport options for the request.
* @return array