Skip to content

Commit 8fba3c6

Browse files
committed
docs: added some methods documentation
1 parent bafeb53 commit 8fba3c6

1 file changed

Lines changed: 137 additions & 1 deletion

File tree

README.md

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,148 @@ class YourApi extends Api
5555
path: '/records',
5656
query: [
5757
'page' => $page
58-
]
58+
]
5959
);
6060
}
6161
}
6262
```
6363

64+
## Protected Methods
65+
66+
Methods available only to developers of the API SDK, for configuration and validation of data.
67+
End users have no access to these methods.
68+
69+
### Base URL
70+
71+
Getter and setter for the base URL.
72+
Base URL is the common part of the API URL and will be used in all requests.
73+
74+
```php
75+
$this->getBaseUrl(): string
76+
```
77+
78+
```php
79+
$this->setBaseUrl(string $baseUrl): self
80+
```
81+
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+
84+
```php
85+
$this->setBaseUrl('https://api.example.com/v1');
86+
$baseUrl = $this->getBaseUrl(); // returns "https://api.exampel.com/v1";
87+
```
88+
89+
### Requests
90+
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.
94+
95+
```php
96+
$this->request(string $method, string $path, array $query [], array $headers = [], StreamInterface|string $body = null): mixed
97+
```
98+
99+
> [!NOTE]
100+
> A `ConfigException` will be thrown if a base URL is not set (this is, if it is empty).
101+
> Check the [`setBaseUrl`](#base-url) method for more information.
102+
103+
> [!NOTE]
104+
> A `ClientException` will be thrown if there is an error while processing the request.
105+
106+
For example, if you wanted to get a list of users with pagination:
107+
108+
```php
109+
use ProgrammatorDev\Api\Api;
110+
111+
class YourApi extends Api
112+
{
113+
public function __construct()
114+
{
115+
parent::__construct();
116+
117+
// minimum required config
118+
$this->setBaseUrl('https://api.example.com/v1');
119+
}
120+
121+
public function getUsers(int $page = 1, int $perPage = 20): string
122+
{
123+
// GET https://api.example.com/v1/users?page=1&limit=20
124+
return $this->request(
125+
method: 'GET',
126+
path: '/users',
127+
query: [
128+
'page' => $page,
129+
'limit' => $perPage
130+
]
131+
);
132+
}
133+
}
134+
```
135+
136+
By default, this method will return a `string` as it will be the response of the request as is.
137+
If you want to change how the response is handled in all requests (for example, decode a JSON string into an array),
138+
check the [`addResponseContentsHandler`]() method in the [Events]() section.
139+
140+
## Query Defaults
141+
142+
These methods are used for handling default query parameters.
143+
Default query parameters are applied to every API request.
144+
145+
```php
146+
$this->addQueryDefault(string $name, mixed $value): self
147+
```
148+
149+
```php
150+
$this->getQueryDefault(string $name): mixed
151+
```
152+
153+
```php
154+
$this->removeQueryDefault(string $name): self
155+
```
156+
157+
For example, if you want to add a language parameter on all requests:
158+
159+
```php
160+
use ProgrammatorDev\Api\Api;
161+
162+
class YourApi extends Api
163+
{
164+
public function __construct(string $language = 'en')
165+
{
166+
parent::__construct();
167+
168+
$this->setBaseUrl('https://api.example.com/v1');
169+
$this->addQueryDefault('lang', $language);
170+
}
171+
172+
public function getPosts(): string
173+
{
174+
// GET https://api.example.com/v1/posts?lang=en
175+
return $this->request(
176+
method: 'GET',
177+
path: '/posts'
178+
);
179+
}
180+
181+
public function getCategories(): string
182+
{
183+
// a query parameter with the same name, passed in the request method, will overwrite a query default
184+
// GET https://api.example.com/v1/categories?lang=pt
185+
return $this->request(
186+
method: 'GET',
187+
path: '/categories',
188+
query: [
189+
'lang' => 'pt'
190+
]
191+
);
192+
}
193+
}
194+
```
195+
196+
> [!NOTE]
197+
> A `query` parameter with the same name, passed in the `request` method, will overwrite a query default.
198+
> Check the `getCategories` method in the example above.
199+
64200
## Contributing
65201

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

0 commit comments

Comments
 (0)