| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Requests; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Client { |
| 8 |
private string $api_url; |
| 9 |
private array $default_headers; |
| 10 |
|
| 11 |
public function __construct( $api_url, $default_headers = array() ) { |
| 12 |
$this->api_url = $api_url; |
| 13 |
$this->default_headers = $default_headers; |
| 14 |
} |
| 15 |
|
| 16 |
public function get( $endpoint, $params = array(), $headers = array(), $timeout = 120 ) { |
| 17 |
$url = $this->api_url . $endpoint; |
| 18 |
$request_args = array( |
| 19 |
'method' => 'GET', |
| 20 |
'headers' => array_merge( $this->default_headers, $headers ), |
| 21 |
'timeout' => $timeout, |
| 22 |
); |
| 23 |
|
| 24 |
if ( ! empty( $params ) ) { |
| 25 |
$url = add_query_arg( $params, $url ); |
| 26 |
} |
| 27 |
|
| 28 |
$response = wp_remote_get( $url, $request_args ); |
| 29 |
|
| 30 |
return $response; |
| 31 |
} |
| 32 |
|
| 33 |
public function post( $endpoint, $params = array(), $headers = array(), $timeout = 120 ) { |
| 34 |
$url = $this->api_url . $endpoint; |
| 35 |
$request_args = array( |
| 36 |
'method' => 'POST', |
| 37 |
'timeout' => $timeout, |
| 38 |
'headers' => array_merge( $this->default_headers, $headers ), |
| 39 |
'body' => $params, |
| 40 |
); |
| 41 |
|
| 42 |
$response = wp_remote_post( $url, $request_args ); |
| 43 |
|
| 44 |
return $response; |
| 45 |
} |
| 46 |
} |
| 47 |
|