Skip to content

Commit 280365f

Browse files
committed
docs: adds header defaults and authentication
1 parent 8fba3c6 commit 280365f

1 file changed

Lines changed: 130 additions & 14 deletions

File tree

README.md

Lines changed: 130 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,28 @@ End users have no access to these methods.
7171
Getter and setter for the base URL.
7272
Base URL is the common part of the API URL and will be used in all requests.
7373

74-
```php
75-
$this->getBaseUrl(): string
76-
```
77-
7874
```php
7975
$this->setBaseUrl(string $baseUrl): self
8076
```
8177

82-
For example, if you have an endpoint https://api.example.com/v1/users, then https://api.example.com/v1 is the base URL:
83-
8478
```php
85-
$this->setBaseUrl('https://api.example.com/v1');
86-
$baseUrl = $this->getBaseUrl(); // returns "https://api.exampel.com/v1";
79+
$this->getBaseUrl(): string
8780
```
8881

8982
### Requests
9083

91-
This method is used to send a request to an API.
92-
It takes an HTTP `method` (like GET or POST), a `path` (the API endpoint), an array of `query` parameters,
93-
an array of `headers`, and a `body` as arguments.
84+
This method is used to send a request to an API.
9485

9586
```php
96-
$this->request(string $method, string $path, array $query [], array $headers = [], StreamInterface|string $body = null): mixed
87+
use Psr\Http\Message\StreamInterface;
88+
89+
$this->request(
90+
string $method,
91+
string $path,
92+
array $query [],
93+
array $headers = [],
94+
StreamInterface|string $body = null
95+
): mixed
9796
```
9897

9998
> [!NOTE]
@@ -137,7 +136,7 @@ By default, this method will return a `string` as it will be the response of the
137136
If you want to change how the response is handled in all requests (for example, decode a JSON string into an array),
138137
check the [`addResponseContentsHandler`]() method in the [Events]() section.
139138

140-
## Query Defaults
139+
### Query Defaults
141140

142141
These methods are used for handling default query parameters.
143142
Default query parameters are applied to every API request.
@@ -154,7 +153,7 @@ $this->getQueryDefault(string $name): mixed
154153
$this->removeQueryDefault(string $name): self
155154
```
156155

157-
For example, if you want to add a language parameter on all requests:
156+
For example, if you want to add a language query parameter in all requests:
158157

159158
```php
160159
use ProgrammatorDev\Api\Api;
@@ -197,6 +196,123 @@ class YourApi extends Api
197196
> A `query` parameter with the same name, passed in the `request` method, will overwrite a query default.
198197
> Check the `getCategories` method in the example above.
199198
199+
### Header Defaults
200+
201+
These methods are used for handling default headers.
202+
Default headers are applied to every API request.
203+
204+
```php
205+
$this->addHeaderDefault(string $name, mixed $value): self
206+
```
207+
208+
```php
209+
$this->getHeaderDefault(string $name): mixed
210+
```
211+
212+
```php
213+
$this->removeHeaderDefault(string $name): self
214+
```
215+
216+
For example, if you want to add a language header value in all requests:
217+
218+
```php
219+
use ProgrammatorDev\Api\Api;
220+
221+
class YourApi extends Api
222+
{
223+
public function __construct(string $language = 'en')
224+
{
225+
parent::__construct();
226+
227+
$this->setBaseUrl('https://api.example.com/v1');
228+
$this->addHeaderDefault('X-LANGUAGE', $language);
229+
}
230+
231+
public function getPosts(): string
232+
{
233+
// GET https://api.example.com/v1/posts with an 'X-LANGUAGE' => 'en' header value
234+
return $this->request(
235+
method: 'GET',
236+
path: '/posts'
237+
);
238+
}
239+
240+
public function getCategories(): string
241+
{
242+
// a header with the same name, passed in the request method, will overwrite a header default
243+
// GET https://api.example.com/v1/categories with an 'X-LANGUAGE' => 'pt' header value
244+
return $this->request(
245+
method: 'GET',
246+
path: '/categories',
247+
headers: [
248+
'X-LANGUAGE' => 'pt'
249+
]
250+
);
251+
}
252+
}
253+
```
254+
255+
> [!NOTE]
256+
> A header with the same name, passed in the `request` method, will overwrite a header default.
257+
> Check the `getCategories` method in the example above.
258+
259+
### Authentication
260+
261+
Getter and setter for API authentication.
262+
Uses the [authentication component](https://docs.php-http.org/en/latest/message/authentication.html) from [PHP HTTP](https://docs.php-http.org/en/latest/index.html).
263+
264+
```php
265+
use Http\Message\Authentication;
266+
267+
$this->setAuthentication(?Authentication $authentication): self;
268+
```
269+
270+
```php
271+
use Http\Message\Authentication;
272+
273+
$this->getAuthentication(): ?Authentication;
274+
```
275+
276+
Available authentication methods:
277+
- [`BasicAuth`](https://docs.php-http.org/en/latest/message/authentication.html#id1) Username and password
278+
- [`Bearer`](https://docs.php-http.org/en/latest/message/authentication.html#bearer) Token
279+
- [`Wsse`](https://docs.php-http.org/en/latest/message/authentication.html#id2) Username and password
280+
- [`QueryParam`](https://docs.php-http.org/en/latest/message/authentication.html#query-params) Array of query parameter values
281+
- [`Header`](https://docs.php-http.org/en/latest/message/authentication.html#header) Header name and value
282+
- [`Chain`](https://docs.php-http.org/en/latest/message/authentication.html#chain) Array of authentication instances
283+
- `RequestConditional` A request matcher and authentication instances
284+
285+
You can also [implement your own](https://docs.php-http.org/en/latest/message/authentication.html#implement-your-own) authentication method.
286+
287+
For example, if you have an API that is authenticated through a query parameter:
288+
289+
```php
290+
use ProgrammatorDev\Api\Api;
291+
use Http\Message\Authentication\QueryParam;
292+
293+
class YourApi extends Api
294+
{
295+
public function __construct(string $applicationKey)
296+
{
297+
parent::__construct();
298+
299+
$this->setBaseUrl('https://api.example.com/v1');
300+
$this->setAuthentication(
301+
new QueryParam(['api_key' => $applicationKey])
302+
);
303+
}
304+
305+
public function getPosts(): string
306+
{
307+
// GET https://api.example.com/v1/posts?api_key=cd982h3diwh98dd23d32j
308+
return $this->request(
309+
method: 'GET',
310+
path: '/posts'
311+
);
312+
}
313+
}
314+
```
315+
200316
## Contributing
201317

202318
Any form of contribution to improve this library (including requests) will be welcome and appreciated.

0 commit comments

Comments
 (0)