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