wppaste
WordPress

WP_AI_Client_HTTP_Client

Since
7.0.0
Source
wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php:29
PSR-18 HTTP Client adapter using WordPress HTTP API.

Description

Allows WordPress HTTP functions to be used as a PSR-18 compliant HTTP client for the AI Client SDK.

Compatibility

WordPress
since 7.0.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0.

Properties · 2

$response_factoryprivate
Response factory instance.
$stream_factoryprivate
Stream factory instance.

Methods · 7

Source code

class WP_AI_Client_HTTP_Client implements ClientInterface, ClientWithOptionsInterface { 	/**	 * Response factory instance.	 *	 * @since 7.0.0	 */	private ResponseFactoryInterface $response_factory; 	/**	 * Stream factory instance.	 *	 * @since 7.0.0	 */	private StreamFactoryInterface $stream_factory; 	/**	 * Constructor.	 *	 * @since 7.0.0	 *	 * @param ResponseFactoryInterface $response_factory PSR-17 Response factory.	 * @param StreamFactoryInterface   $stream_factory   PSR-17 Stream factory.	 */	public function __construct( ResponseFactoryInterface $response_factory, StreamFactoryInterface $stream_factory ) {		$this->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: 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' ),

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.