Skip to content

Commit 7dafcbc

Browse files
committed
Documentation fixed (bug with graphql and form_params); shorthand POST/PUT methods
1 parent 8ffb3c2 commit 7dafcbc

File tree

2 files changed

+186
-5
lines changed

2 files changed

+186
-5
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ You can use three types of payload:
129129
or array of strings.
130130
* ```$json``` - for json. The passed associative ```array``` will be automatically converted to json data.
131131

132-
By the way, it is convenient to pass the GraphQL payload through ```$formParams```:
132+
There are also short forms of methods for different types of payloads:
133+
```php
134+
$response = $sdk->postBody($url, $body, $apiParams, $headers);
135+
$response = $sdk->postForm($url, $formParams, $apiParams, $headers);
136+
$response = $sdk->postJson($url, $json, $apiParams, $headers);
137+
```
138+
139+
By the way, it is convenient to pass the GraphQL payload through ```$json```:
133140

134141
```php
135142
$query = '
@@ -142,9 +149,9 @@ $query = '
142149
}
143150
}
144151
';
145-
$formParams = ['query' => $query, 'variables' => ['episode' => 'JEDI']];
152+
$json = ['query' => $query, 'variables' => ['episode' => 'JEDI']];
146153

147-
$response = $sdk->post('https://example.com', null, null, null, $formParams);
154+
$response = $sdk->postJson('https://example.com', $json);
148155
```
149156

150157
### Asynchronous
@@ -155,7 +162,7 @@ Everything is similar to synchronous, but the work is going not through requests
155162
//Create array with promises
156163
$promises = [
157164
'first' => $sdk->getPromise('https://example.com', ['country_code' => 'us']),
158-
'second' => $sdk->postPromise('https://example.com', null, null, 'payload'),
165+
'second' => $sdk->postPromiseBody('https://example.com', 'payload'),
159166
];
160167
//Asynchronous fulfillment of promises
161168
$responses = $sdk->resolvePromises($promises);

src/Client.php

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use GuzzleHttp\TransferStats;
1313
use Psr\Http\Message\RequestInterface;
1414
use Psr\Http\Message\ResponseInterface;
15-
use Exception;
1615
use Psr\Log\LoggerInterface;
1716
use Throwable;
1817

@@ -217,6 +216,51 @@ public function post(
217216
return $this->sendRequest('POST', $url, $apiParams, $headers, $body, $formParams, $json);
218217
}
219218

219+
/**
220+
* POST request with a BODY of one of three types: raw string, fopen() resource or Psr\Http\Message\StreamInterface.
221+
*
222+
* @param string $url
223+
* @param array|null $apiParams
224+
* @param array|null $headers
225+
* @param null $body
226+
* @return ResponseInterface
227+
* @throws GuzzleException
228+
*/
229+
public function postBody(string $url, $body = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
230+
{
231+
return $this->post($url, $apiParams, $headers, $body);
232+
}
233+
234+
/**
235+
* POST request with FORM payload (Content-Type: application/x-www-form-urlencoded)
236+
*
237+
* @param string $url
238+
* @param array|null $apiParams
239+
* @param array|null $headers
240+
* @param array|null $formParams
241+
* @return ResponseInterface
242+
* @throws GuzzleException
243+
*/
244+
public function postForm(string $url, ?array $formParams = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
245+
{
246+
return $this->post($url, $apiParams, $headers, null, $formParams);
247+
}
248+
249+
/**
250+
* POST request with JSON payload (Content-Type: application/json)
251+
*
252+
* @param string $url
253+
* @param array|null $apiParams
254+
* @param array|null $headers
255+
* @param array|null $json
256+
* @return ResponseInterface
257+
* @throws GuzzleException
258+
*/
259+
public function postJson(string $url, ?array $json = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
260+
{
261+
return $this->post($url, $apiParams, $headers, null, null, $json);
262+
}
263+
220264
/**
221265
* PUT request
222266
*
@@ -242,6 +286,52 @@ public function put(
242286
}
243287

244288

289+
/**
290+
* PUT request with a BODY of one of three types: raw string, fopen() resource or Psr\Http\Message\StreamInterface.
291+
*
292+
* @param string $url
293+
* @param array|null $apiParams
294+
* @param array|null $headers
295+
* @param null $body
296+
* @return ResponseInterface
297+
* @throws GuzzleException
298+
*/
299+
public function putBody(string $url, $body = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
300+
{
301+
return $this->put($url, $apiParams, $headers, $body);
302+
}
303+
304+
/**
305+
* PUT request with FORM payload (Content-Type: application/x-www-form-urlencoded)
306+
*
307+
* @param string $url
308+
* @param array|null $apiParams
309+
* @param array|null $headers
310+
* @param array|null $formParams
311+
* @return ResponseInterface
312+
* @throws GuzzleException
313+
*/
314+
public function putForm(string $url, ?array $formParams = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
315+
{
316+
return $this->put($url, $apiParams, $headers, null, $formParams);
317+
}
318+
319+
/**
320+
* PUT request with JSON payload (Content-Type: application/json)
321+
*
322+
* @param string $url
323+
* @param array|null $apiParams
324+
* @param array|null $headers
325+
* @param array|null $json
326+
* @return ResponseInterface
327+
* @throws GuzzleException
328+
*/
329+
public function putJson(string $url, ?array $json = null, ?array $apiParams = null, ?array $headers = null): ResponseInterface
330+
{
331+
return $this->put($url, $apiParams, $headers, null, null, $json);
332+
}
333+
334+
245335
/**
246336
* @param string $method
247337
* @param string $url
@@ -368,6 +458,48 @@ public function postPromise(
368458
return $this->createPromise('POST', $url, $apiParams, $headers, $body, $formParams, $json);
369459
}
370460

461+
/**
462+
* Create POST promise with a BODY of one of three types: raw string, fopen() resource or Psr\Http\Message\StreamInterface.
463+
*
464+
* @param string $url
465+
* @param null $body
466+
* @param array|null $apiParams
467+
* @param array|null $headers
468+
* @return PromiseInterface
469+
*/
470+
public function postPromiseBody(string $url, $body = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
471+
{
472+
return $this->postPromise($url, $apiParams, $headers, $body);
473+
}
474+
475+
/**
476+
* Create POST promise with FORM payload (Content-Type: application/x-www-form-urlencoded)
477+
*
478+
* @param string $url
479+
* @param array|null $formParams
480+
* @param array|null $apiParams
481+
* @param array|null $headers
482+
* @return PromiseInterface
483+
*/
484+
public function postPromiseForm(string $url, ?array $formParams = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
485+
{
486+
return $this->postPromise($url, $apiParams, $headers, null, $formParams);
487+
}
488+
489+
/**
490+
* Create POST promise with JSON payload (Content-Type: application/json)
491+
*
492+
* @param string $url
493+
* @param array|null $json
494+
* @param array|null $apiParams
495+
* @param array|null $headers
496+
* @return PromiseInterface
497+
*/
498+
public function postPromiseJson(string $url, ?array $json = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
499+
{
500+
return $this->postPromise($url, $apiParams, $headers, null, null, $json);
501+
}
502+
371503
/**
372504
* Create PUT promise
373505
*
@@ -391,6 +523,48 @@ public function putPromise(
391523
return $this->createPromise('PUT', $url, $apiParams, $headers, $body, $formParams, $json);
392524
}
393525

526+
/**
527+
* Create PUT promise with a BODY of one of three types: raw string, fopen() resource or Psr\Http\Message\StreamInterface.
528+
*
529+
* @param string $url
530+
* @param null $body
531+
* @param array|null $apiParams
532+
* @param array|null $headers
533+
* @return PromiseInterface
534+
*/
535+
public function putPromiseBody(string $url, $body = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
536+
{
537+
return $this->putPromise($url, $apiParams, $headers, $body);
538+
}
539+
540+
/**
541+
* Create PUT promise with FORM payload (Content-Type: application/x-www-form-urlencoded)
542+
*
543+
* @param string $url
544+
* @param array|null $formParams
545+
* @param array|null $apiParams
546+
* @param array|null $headers
547+
* @return PromiseInterface
548+
*/
549+
public function putPromiseForm(string $url, ?array $formParams = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
550+
{
551+
return $this->putPromise($url, $apiParams, $headers, null, $formParams);
552+
}
553+
554+
/**
555+
* Create PUT promise with JSON payload (Content-Type: application/json)
556+
*
557+
* @param string $url
558+
* @param array|null $json
559+
* @param array|null $apiParams
560+
* @param array|null $headers
561+
* @return PromiseInterface
562+
*/
563+
public function putPromiseJson(string $url, ?array $json = null, ?array $apiParams = null, ?array $headers = null): PromiseInterface
564+
{
565+
return $this->putPromise($url, $apiParams, $headers, null, null, $json);
566+
}
567+
394568
/**
395569
* @param string $method
396570
* @param string $url

0 commit comments

Comments
 (0)