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