PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Infrastructure / Http / WordPressHttpClient.php

WordPressHttpClient.php in Auto Alt Text 2.7.0, at src/App/Infrastructure/Http/WordPressHttpClient.php

136 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AATXT\App\Infrastructure\Http;
6
7 use Exception;
8
9 /**
10 * WordPress HTTP client implementation.
11 *
12 * Wraps WordPress HTTP API functions (wp_remote_post, wp_remote_get) to provide
13 * a clean interface for dependency injection and testing.
14 */
15 class WordPressHttpClient implements HttpClientInterface
16 {
17 /**
18 * Default timeout for HTTP requests in seconds.
19 * Set to 90 seconds to accommodate AI provider API calls which can take longer.
20 */
21 private const DEFAULT_TIMEOUT = 90;
22
23 /**
24 * Perform an HTTP POST request using WordPress HTTP API.
25 *
26 * @param string $url The URL to send the request to
27 * @param array<string, string> $headers HTTP headers as key-value pairs
28 * @param array<string, mixed> $body Request body data (will be JSON-encoded if array)
29 *
30 * @return array<string, mixed> The decoded JSON response body
31 *
32 * @throws Exception If the HTTP request fails or returns a non-2xx status code
33 */
34 public function post(string $url, array $headers, array $body): array
35 {
36 $args = [
37 'headers' => $headers,
38 'body' => is_array($body) ? json_encode($body) : $body,
39 'timeout' => self::DEFAULT_TIMEOUT,
40 'data_format' => 'body',
41 ];
42
43 $response = wp_remote_post($url, $args);
44
45 // Check for WordPress HTTP errors
46 if (is_wp_error($response)) {
47 throw new Exception(
48 sprintf('HTTP request failed: %s', $response->get_error_message())
49 );
50 }
51
52 // Check HTTP status code
53 $statusCode = wp_remote_retrieve_response_code($response);
54 if ($statusCode < 200 || $statusCode >= 300) {
55 $body = wp_remote_retrieve_body($response);
56 throw new Exception(
57 sprintf(
58 'HTTP request returned status %d: %s',
59 $statusCode,
60 $body ?: 'No response body'
61 )
62 );
63 }
64
65 // Retrieve and decode response body
66 $responseBody = wp_remote_retrieve_body($response);
67 if (empty($responseBody)) {
68 throw new Exception('Empty response body received from server');
69 }
70
71 $decodedBody = json_decode($responseBody, true);
72 if (json_last_error() !== JSON_ERROR_NONE) {
73 throw new Exception(
74 sprintf('Failed to decode JSON response: %s', json_last_error_msg())
75 );
76 }
77
78 return $decodedBody;
79 }
80
81 /**
82 * Perform an HTTP GET request using WordPress HTTP API.
83 *
84 * @param string $url The URL to send the request to
85 * @param array<string, string> $headers HTTP headers as key-value pairs
86 *
87 * @return array<string, mixed> The decoded JSON response body
88 *
89 * @throws Exception If the HTTP request fails or returns a non-2xx status code
90 */
91 public function get(string $url, array $headers = []): array
92 {
93 $args = [
94 'headers' => $headers,
95 'timeout' => self::DEFAULT_TIMEOUT,
96 ];
97
98 $response = wp_remote_get($url, $args);
99
100 // Check for WordPress HTTP errors
101 if (is_wp_error($response)) {
102 throw new Exception(
103 sprintf('HTTP request failed: %s', $response->get_error_message())
104 );
105 }
106
107 // Check HTTP status code
108 $statusCode = wp_remote_retrieve_response_code($response);
109 if ($statusCode < 200 || $statusCode >= 300) {
110 $body = wp_remote_retrieve_body($response);
111 throw new Exception(
112 sprintf(
113 'HTTP request returned status %d: %s',
114 $statusCode,
115 $body ?: 'No response body'
116 )
117 );
118 }
119
120 // Retrieve and decode response body
121 $responseBody = wp_remote_retrieve_body($response);
122 if (empty($responseBody)) {
123 throw new Exception('Empty response body received from server');
124 }
125
126 $decodedBody = json_decode($responseBody, true);
127 if (json_last_error() !== JSON_ERROR_NONE) {
128 throw new Exception(
129 sprintf('Failed to decode JSON response: %s', json_last_error_msg())
130 );
131 }
132
133 return $decodedBody;
134 }
135 }
136