| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Http; |
| 4 |
|
| 5 |
class Client |
| 6 |
{ |
| 7 |
protected function request($url, $args = []) |
| 8 |
{ |
| 9 |
$response = wp_remote_request($url, $args); |
| 10 |
|
| 11 |
if (is_wp_error($response)) { |
| 12 |
throw new class( |
| 13 |
$response->get_error_message(), 500 |
| 14 |
) extends \Exception {}; |
| 15 |
} |
| 16 |
|
| 17 |
return $this->makeResponse($response); |
| 18 |
} |
| 19 |
|
| 20 |
protected function makeResponse($response) |
| 21 |
{ |
| 22 |
return new class($response) { |
| 23 |
|
| 24 |
protected $response = null; |
| 25 |
|
| 26 |
public function __construct($response) { |
| 27 |
$this->response = $response; |
| 28 |
} |
| 29 |
|
| 30 |
public function toArray() { |
| 31 |
return $this->response; |
| 32 |
} |
| 33 |
|
| 34 |
public function isOkay() { |
| 35 |
return $this->getCode() == 200; |
| 36 |
} |
| 37 |
|
| 38 |
public function throw() { |
| 39 |
$class = sprintf( |
| 40 |
'WpOrg\Requests\Exception\Http\Status%d', $this->getCode() |
| 41 |
); |
| 42 |
|
| 43 |
if (!class_exists($class)) { |
| 44 |
$class = 'WpOrg\Requests\Exception\Http\Status\Http'; |
| 45 |
} |
| 46 |
|
| 47 |
throw new $class; |
| 48 |
} |
| 49 |
|
| 50 |
public function throwIf(callable $callback) { |
| 51 |
if ($callback($this)) { |
| 52 |
return $this->throw(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function getCode() { |
| 57 |
return wp_remote_retrieve_response_code($this->response); |
| 58 |
} |
| 59 |
|
| 60 |
public function getMessage() { |
| 61 |
return wp_remote_retrieve_response_message($this->response); |
| 62 |
} |
| 63 |
|
| 64 |
public function getBody() { |
| 65 |
return wp_remote_retrieve_body($this->response); |
| 66 |
} |
| 67 |
|
| 68 |
public function isJson() { |
| 69 |
$header = $this->getHeader('content-type'); |
| 70 |
return str_contains($header, 'application/json'); |
| 71 |
} |
| 72 |
|
| 73 |
public function getJson() { |
| 74 |
if ($this->isJson()) { |
| 75 |
return json_decode($this->getBody(), true); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function getHeaders() { |
| 80 |
return wp_remote_retrieve_headers($this->response); |
| 81 |
} |
| 82 |
|
| 83 |
public function getHeader($key) { |
| 84 |
return wp_remote_retrieve_header($this->response, $key); |
| 85 |
} |
| 86 |
|
| 87 |
public function getCookies() { |
| 88 |
return wp_remote_retrieve_cookies($this->response); |
| 89 |
} |
| 90 |
|
| 91 |
public function getCookie($name, $isObject = false) { |
| 92 |
if ($isObject) { |
| 93 |
// Return the WP_Http_Cookie object |
| 94 |
return wp_remote_retrieve_cookie($this->response, $name); |
| 95 |
} |
| 96 |
return wp_remote_retrieve_cookie_value($this->response, $name); |
| 97 |
} |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
public static function __callStatic($method, $args) |
| 102 |
{ |
| 103 |
return (new static)->request(array_shift($args), array_merge( |
| 104 |
count($args) ? $args : [], ['method' => strtoupper($method)] |
| 105 |
)); |
| 106 |
} |
| 107 |
} |
| 108 |
|