| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Beyondwords\Wordpress\Core; |
| 6 |
|
| 7 |
class Request |
| 8 |
{ |
| 9 |
public const AUTH_HEADER_NAME = 'X-Api-Key'; |
| 10 |
public const CONTENT_TYPE_HEADER_NAME = 'Content-Type'; |
| 11 |
public const CONTENT_TYPE_HEADER_VALUE = 'application/json'; |
| 12 |
|
| 13 |
private $method; |
| 14 |
private $url; |
| 15 |
private $body; |
| 16 |
private $headers; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param string $method |
| 20 |
* @param string $url |
| 21 |
* @param mixed $body |
| 22 |
*/ |
| 23 |
public function __construct($method, $url, $body = null, $headers = null) |
| 24 |
{ |
| 25 |
$this->setMethod($method); |
| 26 |
$this->setUrl($url); |
| 27 |
$this->setBody($body); |
| 28 |
|
| 29 |
if ($headers === null) { |
| 30 |
$headers = $this->getDefaultHeaders(); |
| 31 |
} |
| 32 |
|
| 33 |
$this->setHeaders($headers); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return mixed |
| 38 |
*/ |
| 39 |
public function getMethod() |
| 40 |
{ |
| 41 |
return $this->method; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param mixed $method |
| 46 |
*/ |
| 47 |
public function setMethod($method) |
| 48 |
{ |
| 49 |
$this->method = strtoupper($method); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @return mixed |
| 54 |
*/ |
| 55 |
public function getUrl() |
| 56 |
{ |
| 57 |
return $this->url; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param mixed $url |
| 62 |
*/ |
| 63 |
public function setUrl($url) |
| 64 |
{ |
| 65 |
$this->url = $url; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
public function getBody() |
| 72 |
{ |
| 73 |
return $this->body; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param mixed $body |
| 78 |
*/ |
| 79 |
public function setBody($body) |
| 80 |
{ |
| 81 |
$this->body = $body; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get default headers (Authorization & Content-Type). |
| 86 |
* |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
public function getDefaultHeaders() |
| 90 |
{ |
| 91 |
return [ |
| 92 |
self::AUTH_HEADER_NAME => get_option('beyondwords_api_key'), |
| 93 |
self::CONTENT_TYPE_HEADER_NAME => self::CONTENT_TYPE_HEADER_VALUE, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return mixed |
| 99 |
*/ |
| 100 |
public function getHeaders() |
| 101 |
{ |
| 102 |
return $this->headers; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param mixed $headers |
| 107 |
*/ |
| 108 |
public function setHeaders($headers) |
| 109 |
{ |
| 110 |
$this->headers = $headers; |
| 111 |
} |
| 112 |
} |
| 113 |
|