| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Http\ConnectServer\Client; |
| 4 |
|
| 5 |
use Give\Framework\Http\ConnectServer\Client\Exceptions\RequestException; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class ConnectClient |
| 10 |
* |
| 11 |
* @since 2.8.0 |
| 12 |
*/ |
| 13 |
class ConnectClient |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Api Url |
| 17 |
* |
| 18 |
* @since 2.8.0 |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $apiUrl; |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 2.25.0 |
| 26 |
*/ |
| 27 |
public function __construct($giveConnectUrl) |
| 28 |
{ |
| 29 |
$this->apiUrl = $giveConnectUrl; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get rest api endpoint url for requests. |
| 34 |
* |
| 35 |
* @since 2.25.0 Set default endpoint value as empty |
| 36 |
* @since 2.8.0 |
| 37 |
* |
| 38 |
* @param string $endpoint The route on the server. E.g: 'gateway-name/connect' |
| 39 |
*/ |
| 40 |
public function getApiUrl(string $endpoint = ''): string |
| 41 |
{ |
| 42 |
if ( ! empty($endpoint)) { |
| 43 |
return trailingslashit($this->apiUrl) . ltrim($endpoint, '/'); |
| 44 |
} |
| 45 |
|
| 46 |
return $this->apiUrl; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @since 2.25.0 |
| 51 |
* |
| 52 |
* @param string $endpoint The route on the server. E.g: 'gateway-name/connect' |
| 53 |
* @param array $args Optional. Can contains 'headers' and 'body' |
| 54 |
* |
| 55 |
* @throws RequestException |
| 56 |
*/ |
| 57 |
public function get(string $endpoint, array $args = []): array |
| 58 |
{ |
| 59 |
$url = $this->getApiUrl($endpoint); |
| 60 |
|
| 61 |
$response = wp_remote_get($url, $args); |
| 62 |
|
| 63 |
$this->validateWpErrorInResponse($response); |
| 64 |
|
| 65 |
return $response; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 2.25.0 |
| 70 |
* |
| 71 |
* @param string $endpoint The route on the server. E.g: 'gateway-name/connect' |
| 72 |
* @param array $args Optional. Can contains 'headers' and 'body' |
| 73 |
* |
| 74 |
* @throws RequestException |
| 75 |
*/ |
| 76 |
public function post(string $endpoint, array $args = []): array |
| 77 |
{ |
| 78 |
$url = $this->getApiUrl($endpoint); |
| 79 |
|
| 80 |
$response = wp_remote_post($url, $args); |
| 81 |
|
| 82 |
$this->validateWpErrorInResponse($response); |
| 83 |
|
| 84 |
return $response; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @since 2.25.0 |
| 89 |
* |
| 90 |
* @param string $endpoint The route on the server. E.g: 'gateway-name/connect' |
| 91 |
* @param array $args Optional. Can contains 'method', 'headers' and 'body' |
| 92 |
* |
| 93 |
* @throws RequestException |
| 94 |
*/ |
| 95 |
public function request(string $endpoint, array $args = []): array |
| 96 |
{ |
| 97 |
$url = $this->getApiUrl($endpoint); |
| 98 |
|
| 99 |
$response = wp_remote_request($url, $args); |
| 100 |
|
| 101 |
$this->validateWpErrorInResponse($response); |
| 102 |
|
| 103 |
return $response; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @since 2.25.0 |
| 108 |
* |
| 109 |
* @param array|WP_Error $response |
| 110 |
* |
| 111 |
* @throws RequestException |
| 112 |
*/ |
| 113 |
private function validateWpErrorInResponse($response) |
| 114 |
{ |
| 115 |
if (is_wp_error($response)) { |
| 116 |
throw new RequestException( |
| 117 |
sprintf( |
| 118 |
esc_html__( |
| 119 |
'The request to the %1$s failed. Error: %2$s', |
| 120 |
'give' |
| 121 |
), |
| 122 |
$this->apiUrl, |
| 123 |
$response->get_error_message() |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|