Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| AiServiceRequest | |
0.00% |
0 / 41 |
|
0.00% |
0 / 8 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| get_api_url | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
30 | |||
| send | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| is_successful | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| get_response_body | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_error_response_body | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_response_code | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get_error_message | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\Services\Ai; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
| 6 | |
| 7 | /** |
| 8 | * AI Service Request Class |
| 9 | * |
| 10 | * Handles HTTP requests to the AI API endpoint. |
| 11 | */ |
| 12 | class AiServiceRequest { |
| 13 | |
| 14 | /** |
| 15 | * Default production base URL. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | const DEFAULT_PRODUCTION_BASE_URL = 'https://patterns.hiive.cloud'; |
| 20 | |
| 21 | /** |
| 22 | * Default local base URL |
| 23 | */ |
| 24 | const DEFAULT_LOCAL_BASE_URL = 'http://localhost:8888'; |
| 25 | |
| 26 | /** |
| 27 | * API URL |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $url; |
| 32 | |
| 33 | /** |
| 34 | * API endpoint |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | protected $endpoint; |
| 39 | |
| 40 | /** |
| 41 | * Request headers |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
| 45 | protected $headers; |
| 46 | |
| 47 | /** |
| 48 | * Request body |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | protected $body; |
| 53 | |
| 54 | /** |
| 55 | * Response |
| 56 | * |
| 57 | * @var array|\WP_Error|null |
| 58 | */ |
| 59 | protected $response = null; |
| 60 | |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param string $endpoint The endpoint to send the request to. |
| 65 | * @param array $body Request body data. |
| 66 | * @param array $headers Additional headers to include in the request. |
| 67 | * @param string|null $url The URL to send the request to (Not recommended to override this). |
| 68 | */ |
| 69 | public function __construct( string $endpoint, array $body, array $headers = array(), ?string $url = null ) { |
| 70 | $this->url = $url ?? $this->get_api_url(); |
| 71 | $this->endpoint = $endpoint; |
| 72 | $this->body = $body; |
| 73 | $this->headers = array_merge( |
| 74 | array( |
| 75 | 'Content-Type' => 'application/json', |
| 76 | 'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(), |
| 77 | ), |
| 78 | $headers |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the API URL based on configuration |
| 84 | * |
| 85 | * @return string The API URL to use. |
| 86 | */ |
| 87 | private function get_api_url(): string { |
| 88 | |
| 89 | if ( defined( 'NFD_WB_DEV_MODE' ) && NFD_DATA_WB_DEV_MODE ) { |
| 90 | $base_url = defined( 'NFD_WB_LOCAL_BASE_URL' ) ? NFD_WB_LOCAL_BASE_URL : self::DEFAULT_LOCAL_BASE_URL; |
| 91 | } else { |
| 92 | $base_url = defined( 'NFD_WB_PRODUCTION_BASE_URL' ) ? NFD_WB_PRODUCTION_BASE_URL : self::DEFAULT_PRODUCTION_BASE_URL; |
| 93 | } |
| 94 | |
| 95 | return $base_url . '/api/v1/'; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Send the HTTP request |
| 100 | * |
| 101 | * @return AiServiceRequest The instance of the class. |
| 102 | */ |
| 103 | public function send(): AiServiceRequest { |
| 104 | $response = wp_remote_post( |
| 105 | $this->url . $this->endpoint, |
| 106 | array( |
| 107 | 'headers' => $this->headers, |
| 108 | 'body' => wp_json_encode( $this->body ), |
| 109 | 'timeout' => 60, |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | $this->response = $response; |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if the request was successful |
| 119 | * |
| 120 | * @return bool True if the request was successful, false otherwise. |
| 121 | */ |
| 122 | public function is_successful(): bool { |
| 123 | if ( ! $this->response || is_wp_error( $this->response ) ) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | $code = wp_remote_retrieve_response_code( $this->response ); |
| 128 | return $code >= 200 && $code < 300; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the response body |
| 133 | * |
| 134 | * @return array|null The response body or null if the request was not successful. |
| 135 | */ |
| 136 | public function get_response_body(): ?array { |
| 137 | // If the request was not successful, return null. |
| 138 | if ( ! $this->is_successful() ) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | return json_decode( wp_remote_retrieve_body( $this->response ), true ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the error response body |
| 147 | * |
| 148 | * @return array|null The error response body or null if the request was successful. |
| 149 | */ |
| 150 | public function get_error_response_body(): ?array { |
| 151 | if ( $this->is_successful() ) { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | return json_decode( wp_remote_retrieve_body( $this->response ), true ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the response code |
| 160 | * |
| 161 | * @return int|null The response code or null if the request was not successful. |
| 162 | */ |
| 163 | public function get_response_code(): ?int { |
| 164 | if ( ! $this->response || is_wp_error( $this->response ) ) { |
| 165 | return 500; |
| 166 | } |
| 167 | |
| 168 | return wp_remote_retrieve_response_code( $this->response ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the error message |
| 173 | * |
| 174 | * @return string|null The error message or null if the request was successful. |
| 175 | */ |
| 176 | public function get_error_message(): ?string { |
| 177 | if ( $this->is_successful() ) { |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | return $this->get_error_response_body()['message'] ?? __( 'An unknown error occurred', 'wp-module-onboarding' ); |
| 182 | } |
| 183 | } |