Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/openai/Http/Request.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Helper class for handling magic ai functionalities
4 + *
5 + * @package TutorPro\OpenAI
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.0.0
9 + */
10 +
11 + namespace TutorPro\OpenAI\Http;
12 +
13 + if ( ! defined( 'ABSPATH' ) ) {
14 + exit;
15 + }
16 +
17 + /**
18 + * Extend the HttpHelper class for consistency.
19 + *
20 + * @since 3.0.0
21 + */
22 + class Request {
23 + /**
24 + * 200 serial HTTP status code constants
25 + */
26 + const STATUS_OK = 200;
27 + const STATUS_CREATED = 201;
28 + const STATUS_ACCEPTED = 202;
29 +
30 + /**
31 + * 400 serial HTTP status code constants
32 + */
33 + const STATUS_BAD_REQUEST = 400;
34 + const STATUS_UNAUTHORIZED = 401;
35 + const STATUS_FORBIDDEN = 403;
36 + const STATUS_NOT_FOUND = 404;
37 + const STATUS_METHOD_NOT_ALLOWED = 405;
38 + const STATUS_TOO_MANY_REQUESTS = 429;
39 + const STATUS_UNPROCESSABLE_ENTITY = 422;
40 +
41 + /**
42 + * 500 serial HTTP status code constants
43 + */
44 + const STATUS_INTERNAL_SERVER_ERROR = 500;
45 + const STATUS_SERVICE_UNAVAILABLE = 503;
46 + const STATUS_BAD_GATEWAY = 502;
47 + const STATUS_GATEWAY_TIMEOUT = 504;
48 +
49 + /**
50 + * Set the request timeout for the http requests.
51 + *
52 + * @since 3.0.0
53 + *
54 + * @var float The timeout value.
55 + */
56 + const REQUEST_TIMEOUT = 120;
57 +
58 + /**
59 + * Response body
60 + *
61 + * @since 3.0.0
62 + *
63 + * @var mixed
64 + */
65 + private $body;
66 +
67 + /**
68 + * Response headers
69 + *
70 + * @since 3.0.0
71 + *
72 + * @var mixed
73 + */
74 + private $headers;
75 +
76 + /**
77 + * Response status code
78 + *
79 + * @since 3.0.0
80 + *
81 + * @var int
82 + */
83 + private $status_code;
84 +
85 + /**
86 + * Hold WP error for request.
87 + *
88 + * @since 3.0.0
89 + *
90 + * @var \WP_Error
91 + */
92 + private $wp_error;
93 +
94 + /**
95 + * Parse response from HTTP response.
96 + *
97 + * @since 3.0.0
98 + *
99 + * @param mixed $response response of request.
100 + *
101 + * @return void
102 + */
103 + private function parse_response( $response ) {
104 + if ( is_wp_error( $response ) ) {
105 + $this->wp_error = $response;
106 + } else {
107 + $this->body = wp_remote_retrieve_body( $response );
108 + $this->headers = wp_remote_retrieve_headers( $response );
109 + $this->status_code = wp_remote_retrieve_response_code( $response );
110 + }
111 + }
112 +
113 + /**
114 + * Make HTTP GET request.
115 + *
116 + * @since 3.0.0
117 + *
118 + * @param string $url The URL for the request.
119 + * @param array $data Optional. The data to include in the request (added to the URL as query parameters).
120 + * @param array $headers Optional. Additional headers for the request.
121 + *
122 + * @return self
123 + */
124 + public static function get( $url, $data = array(), $headers = array() ) {
125 + $url_with_params = add_query_arg( $data, $url );
126 +
127 + $response = wp_remote_get( $url_with_params, array( 'headers' => $headers ) );
128 +
129 + $self = new self();
130 + $self->parse_response( $response );
131 +
132 + return $self;
133 + }
134 +
135 + /**
136 + * Make HTTP POST request.
137 + *
138 + * @since 3.0.0
139 + *
140 + * @param string $url The URL for the request.
141 + * @param array $data Optional. The data to include in the request body.
142 + * @param array $headers Optional. Additional headers for the request.
143 + *
144 + * @return self
145 + */
146 + public static function post( $url, $data = array(), $headers = array() ) {
147 + $response = wp_remote_post(
148 + $url,
149 + array(
150 + 'body' => $data,
151 + 'headers' => $headers,
152 + 'timeout' => self::REQUEST_TIMEOUT,
153 + ),
154 + );
155 +
156 + $self = new self();
157 + $self->parse_response( $response );
158 +
159 + return $self;
160 + }
161 +
162 + /**
163 + * Get body
164 + *
165 + * @since 3.0.0
166 + *
167 + * @return mixed
168 + */
169 + public function get_body() {
170 + return $this->body;
171 + }
172 +
173 + /**
174 + * Get body data as JSON
175 + *
176 + * @since 3.0.0
177 + *
178 + * @return mixed
179 + */
180 + public function get_json() {
181 + return json_decode( $this->body );
182 + }
183 +
184 + /**
185 + * Get headers
186 + *
187 + * @since 3.0.0
188 + *
189 + * @return mixed
190 + */
191 + public function get_headers() {
192 + return $this->headers;
193 + }
194 +
195 + /**
196 + * Get status code.
197 + *
198 + * @since 3.0.0
199 + *
200 + * @return int
201 + */
202 + public function get_status_code() {
203 + return $this->status_code;
204 + }
205 +
206 + /**
207 + * Check any error occur.
208 + *
209 + * @since 3.0.0
210 + *
211 + * @return boolean
212 + */
213 + public function has_error() {
214 + return ! is_null( $this->wp_error );
215 + }
216 +
217 + /**
218 + * Get error message.
219 + *
220 + * @since 3.0.0
221 + *
222 + * @return mixed
223 + */
224 + public function get_error_message() {
225 + return $this->wp_error->get_error_message();
226 + }
227 + }
228 +