@@ -61,7 +61,7 @@ class YourApi extends Api
6161}
6262```
6363
64- ## Protected Methods
64+ ## Documentation
6565
6666Methods available only to developers of the API SDK, for configuration and validation of data.
6767End users have no access to these methods.
@@ -81,6 +81,11 @@ $this->getBaseUrl(): string
8181
8282### Requests
8383
84+ - [ ` request ` ] ( #request )
85+ - [ ` buildPath ` ] ( #buildpath )
86+
87+ #### ` request `
88+
8489This method is used to send a request to an API.
8590
8691``` php
@@ -134,7 +139,42 @@ class YourApi extends Api
134139
135140By default, this method will return a ` string ` as it will be the response of the request as is.
136141If you want to change how the response is handled in all requests (for example, decode a JSON string into an array),
137- check the [ ` addResponseContentsHandler ` ] ( ) method in the [ Events] ( ) section.
142+ check the [ ` addResponseContentsHandler ` ] ( #addresponsecontentshandler ) method in the [ Event Listeners] ( #event-listeners ) section.
143+
144+ #### ` buildPath `
145+
146+ The purpose of this method is to have an easy way to build a properly formatted path URL depending on the inputs or parameters you might have.
147+
148+ ``` php
149+ $this->buildPath(string $path, array $parameters): string;
150+ ```
151+
152+ For example, if you want to build a path that has a dynamic id:
153+
154+ ``` php
155+ use ProgrammatorDev\Api\Api;
156+
157+ class YourApi extends Api
158+ {
159+ public function __construct()
160+ {
161+ parent::__construct();
162+
163+ $this->setBaseUrl('https://api.example.com/v1');
164+ }
165+
166+ public function getPostComments(int $postId): string
167+ {
168+ // GET https://api.example.com/v1/posts/1/comments
169+ return $this->request(
170+ method: 'GET',
171+ path: $this->buildPath('/posts/{postId}/comments', [
172+ 'postId' => $postId
173+ ])
174+ );
175+ }
176+ }
177+ ```
138178
139179### Query Defaults
140180
@@ -313,7 +353,6 @@ class YourApi extends Api
313353
314354### Event Listeners
315355
316- Currently, there are two event listeners available:
317356- [ ` addPostRequestHandler ` ] ( #addpostrequesthandler )
318357- [ ` addResponseContentsHandler ` ] ( #addresponsecontentshandler )
319358
@@ -339,7 +378,9 @@ class YourApi extends Api
339378 {
340379 // ...
341380
381+ // a PostRequestEvent is passed as an argument
342382 $this->addPostRequestHandler(function(PostRequestEvent $event) {
383+ // $event->getRequest() is also available
343384 $response = $event->getResponse();
344385 $statusCode = $response->getStatusCode();
345386
@@ -380,10 +421,13 @@ class YourApi extends Api
380421 {
381422 // ...
382423
424+ // a ResponseContentsEvent is passed as an argument
383425 $this->addResponseContentsHandler(function(ResponseContentsEvent $event) {
384426 // get response contents and decode json string into an array
385- $contents = json_decode($event->getContents(), true);
386- // set new handled contents
427+ $contents = $event->getContents();
428+ $contents = json_decode($contents, true);
429+
430+ // set handled contents
387431 $event->setContents($contents);
388432 });
389433 }
0 commit comments