PluginProbe
BeyondWords – AI audio for publishers / 6.0.3
BeyondWords – AI audio for publishers v6.0.3
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Core / Request.php

Request.php in BeyondWords – AI audio for publishers 6.0.3, at src/Core/Request.php

144 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
11 public const CONTENT_TYPE_HEADER_NAME = 'Content-Type';
12
13 public const CONTENT_TYPE_HEADER_VALUE = 'application/json';
14
15 private string $method = '';
16
17 private string $url = '';
18
19 private string $body = '';
20
21 private array $headers = [];
22
23 /**
24 * Request constructor.
25 *
26 * @param mixed $body
27 *
28 * @return void
29 */
30 public function __construct(
31 string $method,
32 string $url,
33 string $body = '',
34 array $headers = []
35 ) {
36 $this->setMethod($method);
37 $this->setUrl($url);
38 $this->setBody($body);
39
40 // Add API key header to all requests.
41 $this->addHeaders([
42 self::AUTH_HEADER_NAME => get_option('beyondwords_api_key'),
43 ]);
44
45 // Add Content-Type header for non-GET requests.
46 if (in_array($method, ['POST', 'PUT', 'DELETE'])) {
47 // Default headers.
48 $this->addHeaders([
49 self::CONTENT_TYPE_HEADER_NAME => self::CONTENT_TYPE_HEADER_VALUE,
50 ]);
51 }
52
53 // Add custom headers.
54 $this->addHeaders($headers);
55 }
56
57 /**
58 * Get the HTTP method for the request.
59 */
60 public function getMethod(): string
61 {
62 return $this->method;
63 }
64
65 /**
66 * Set the HTTP method for the request.
67 *
68 *
69 */
70 public function setMethod(string $method): void
71 {
72 $this->method = strtoupper($method);
73 }
74
75 /**
76 * Get the URL for the request.
77 */
78 public function getUrl(): string
79 {
80 return $this->url;
81 }
82
83 /**
84 * Set the URL for the request.
85 *
86 *
87 */
88 public function setUrl(string $url): void
89 {
90 $this->url = $url;
91 }
92
93 /**
94 * Get the body for the request.
95 */
96 public function getBody(): string
97 {
98 return $this->body;
99 }
100
101 /**
102 * Set the body for the request.
103 *
104 *
105 */
106 public function setBody(string $body): void
107 {
108 $this->body = $body;
109 }
110
111 /**
112 * Get the headers for the request.
113 */
114 public function getHeaders(): array
115 {
116 return $this->headers;
117 }
118
119 /**
120 * Set the headers to the request.
121 *
122 *
123 */
124 public function setHeaders(array $headers): void
125 {
126 $this->headers = $headers;
127 }
128
129 /**
130 * Add extra headers to the request.
131 *
132 * @since 6.0.0 Introduced.
133 *
134 *
135 */
136 public function addHeaders(array $headers): void
137 {
138 $this->setHeaders(array_merge(
139 $this->getHeaders(),
140 $headers
141 ));
142 }
143 }
144