PluginProbe
BeyondWords – AI audio for publishers / 6.1.0
BeyondWords – AI audio for publishers v6.1.0
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.1.0, at src/Core/Request.php

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