| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\AI_HTTP_Request\Infrastructure; |
| 4 |
|
| 5 |
use WPSEO_Utils; |
| 6 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\WP_Request_Exception; |
| 7 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Request; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class API_Client |
| 11 |
* Handles the API requests to the AI Generator API. |
| 12 |
* |
| 13 |
* @makePublic |
| 14 |
*/ |
| 15 |
class API_Client implements API_Client_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The base URL for the API. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $base_url = 'https://ai.yoa.st/api/v1'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Performs a request to the API. |
| 26 |
* |
| 27 |
* @param string $action_path The action path for the request. |
| 28 |
* @param array<string>|null $body The body of the request, or null/empty to send no body. |
| 29 |
* @param array<string> $headers The headers for the request. |
| 30 |
* @param string $http_method The HTTP method for the request. One of `Request::METHOD_*`. |
| 31 |
* |
| 32 |
* @return array<int|string|array<string>> The response from the API. |
| 33 |
* |
| 34 |
* @throws WP_Request_Exception When the underlying WordPress HTTP call returns an error, or the HTTP method is not supported. |
| 35 |
*/ |
| 36 |
public function perform_request( string $action_path, $body, $headers, string $http_method ): array { |
| 37 |
// Our API expects JSON. |
| 38 |
$headers = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] ); |
| 39 |
$arguments = [ |
| 40 |
'timeout' => $this->get_request_timeout(), |
| 41 |
'headers' => $headers, |
| 42 |
]; |
| 43 |
|
| 44 |
// Only POST sends a body to the AI API today; GET and DELETE endpoints do not. An empty body is |
| 45 |
// omitted entirely: an empty array is ambiguous once JSON-encoded (`[]` vs `{}`) and the AI |
| 46 |
// service rejects it, so a bodyless POST is sent instead. |
| 47 |
if ( $http_method === Request::METHOD_POST && ! empty( $body ) ) { |
| 48 |
// phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Reason: We don't want the debug/pretty possibility. |
| 49 |
$arguments['body'] = WPSEO_Utils::format_json_encode( $body ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Filter: 'Yoast\WP\SEO\ai_api_url' - Replaces the default URL for the AI API with a custom one. |
| 54 |
* |
| 55 |
* @internal |
| 56 |
* |
| 57 |
* @param string $url The default URL for the AI API. |
| 58 |
*/ |
| 59 |
$url = \apply_filters( 'Yoast\WP\SEO\ai_api_url', $this->base_url ); |
| 60 |
|
| 61 |
switch ( $http_method ) { |
| 62 |
case Request::METHOD_POST: |
| 63 |
$response = \wp_remote_post( $url . $action_path, $arguments ); |
| 64 |
break; |
| 65 |
case Request::METHOD_GET: |
| 66 |
$response = \wp_remote_get( $url . $action_path, $arguments ); |
| 67 |
break; |
| 68 |
case Request::METHOD_DELETE: |
| 69 |
$response = \wp_remote_request( $url . $action_path, \array_merge( $arguments, [ 'method' => 'DELETE' ] ) ); |
| 70 |
break; |
| 71 |
default: |
| 72 |
// Defensive: the Request constructor already validates the method, so we should never reach this branch. |
| 73 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive. |
| 74 |
throw new WP_Request_Exception( "Unsupported HTTP method: $http_method" ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( \is_wp_error( $response ) ) { |
| 78 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive. |
| 79 |
throw new WP_Request_Exception( $response->get_error_message() ); |
| 80 |
} |
| 81 |
|
| 82 |
return $response; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Gets the timeout of the requests in seconds. |
| 87 |
* |
| 88 |
* @return int The timeout of the suggestion requests in seconds. |
| 89 |
*/ |
| 90 |
public function get_request_timeout(): int { |
| 91 |
/** |
| 92 |
* Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes. |
| 93 |
* |
| 94 |
* @since 22.7 |
| 95 |
* @internal |
| 96 |
* |
| 97 |
* @param int $timeout The default timeout in seconds. |
| 98 |
*/ |
| 99 |
return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 ); |
| 100 |
} |
| 101 |
} |
| 102 |
|