Skip to content

Commit 94447c3

Browse files
committed
Return collection by default, add request data to builder
1 parent 7de3bd1 commit 94447c3

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You can also use [Symfony HttpClient documentation](https://symfony.com/doc/curr
1414
```php
1515
$response = $client->get("https://example.com");
1616
$response = $client->get("https://example.com", ["query" => ["key" => "value"]]);
17-
$response->getContent(); // Get response body
17+
$response->getContent(); // Get response body, or collection, if response is JSON
1818
$response->toCollection(); // Transform JSON response to collection
1919
$response->getStatusCode(); // Get response status code
2020
$response->getHeaders(); // Get response headers
@@ -29,9 +29,16 @@ $client->delete("https://example.com");
2929
### Request builder
3030
You can send your request parameters directly to client methods, but you can also use fluent request builder.
3131
```php
32+
// Add data to request
33+
$client->query(["key" => "value"])->get("https://example.com")
34+
$client->body(["key" => "value"])->post("https://example.com")
35+
$client->json(["key" => "value"])->post("https://example.com")
36+
3237
// Add custom headers to request
3338
$client->headers(["key" => "value"])->get("https://example.com");
39+
3440
// Authentication
3541
$client->auth("auth_basic", ["username", "password"])->get("https://example.com");
3642
$client->authBasic(["username", "password"])->get("https://example.com");
3743
$client->authBearer("tokenhere")->get("https://example.com");
44+
```

src/Buildable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ public function headers($headers)
4646
$this->applyRequestOptions(["headers" => $headers]);
4747
return $this;
4848
}
49+
50+
public function body($body)
51+
{
52+
$this->applyRequestOptions(["body" => $body]);
53+
return $this;
54+
}
55+
56+
public function json($json)
57+
{
58+
$this->applyRequestOptions(["json" => $json]);
59+
return $this;
60+
}
61+
62+
public function query($query)
63+
{
64+
$this->applyRequestOptions(["query" => $query]);
65+
return $this;
66+
}
4967
}

src/Response.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ivan770\HttpClient;
44

5+
use Symfony\Component\HttpClient\Exception\JsonException;
6+
57
class Response
68
{
79
protected $baseResponse;
@@ -21,6 +23,15 @@ public function toCollection()
2123
return collect($this->baseResponse->toArray());
2224
}
2325

26+
public function getContent($throw = true)
27+
{
28+
try {
29+
return $this->toCollection();
30+
} catch (JsonException $exception) {
31+
return $this->baseResponse->getContent($throw);
32+
}
33+
}
34+
2435
public function __call($name, $arguments)
2536
{
2637
return $this->baseResponse->$name(...$arguments);

0 commit comments

Comments
 (0)