Skip to content

Commit a621531

Browse files
Merge pull request #51 from sonnymilton/gh-49_symfony-http-client
Add Symfony HttpClient support for loading JSON from URL
2 parents e614085 + b91125b commit a621531

4 files changed

Lines changed: 64 additions & 1 deletion

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,34 @@ echo $workflow->get("jobs.test.runs-on");
535535
echo $workflow->get("on.0"); // push , the first event
536536
```
537537

538+
### Loading Data from JSON URL via Symfony HttpClient
539+
540+
If you want more control over the HTTP request (headers, authentication, timeouts, retries, etc.) or your environment restricts PHP stream functions (for example `allow_url_fopen=0`), you can use `fromHttpJsonUrl()`.
541+
This method relies on your Symfony HttpClient implementation and allows you to pass any request options supported by the client.
542+
543+
```php
544+
use HiFolks\DataType\Block;
545+
use HiFolks\DataType\Enums\Operator;
546+
use Symfony\Component\HttpClient\HttpClient;
547+
548+
$url = "https://api.github.com/repos/hi-folks/data-block/commits";
549+
$client = HttpClient::create();
550+
551+
$commits = Block::fromHttpJsonUrl($url, $client, [
552+
'headers' => [
553+
'User-Agent' => 'my-app',
554+
'Accept' => 'application/json',
555+
],
556+
]);
557+
558+
$myCommits = $commits->where("commit.author.name", Operator::LIKE, "Roberto");
559+
foreach ($myCommits as $value) {
560+
echo $value->get("commit.message") . PHP_EOL;
561+
}
562+
```
563+
Don't forget to install the client's implementation `composer require symfony/http-client`
564+
565+
538566
## Adding and appending elements
539567

540568
### Appending the elements of a Block object to another Block object

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
"require": {
2121
"php": "^8.2|^8.3|^8.4",
2222
"swaggest/json-schema": "^0.12.42",
23+
"symfony/http-client-contracts": "^3.4.4",
2324
"symfony/yaml": "^6.4|^7.1"
2425
},
2526
"require-dev": {
2627
"laravel/pint": "^1.2",
2728
"pestphp/pest": "^3.0",
2829
"phpstan/phpstan": "^2",
29-
"rector/rector": "^2"
30+
"rector/rector": "^2",
31+
"symfony/http-client": "^7.4"
32+
},
33+
"suggest": {
34+
"symfony/http-client": "HTTP client implementation"
3035
},
3136
"autoload": {
3237
"psr-4": {

src/Traits/LoadableBlock.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace HiFolks\DataType\Traits;
66

77
use Symfony\Component\Yaml\Yaml;
8+
use Symfony\Contracts\HttpClient\HttpClientInterface;
89

910
trait LoadableBlock
1011
{
@@ -56,6 +57,20 @@ public static function fromJsonUrl(string $jsonUrl, ?array $headers = null): sel
5657

5758
}
5859

60+
/**
61+
* @param array<string, mixed> $options Symfony client's request options
62+
*/
63+
public static function fromHttpJsonUrl(string $jsonUrl, HttpClientInterface $client, array $options = []): self
64+
{
65+
$content = $client->request('GET', $jsonUrl, $options)->getContent(false);
66+
67+
if ('' === $content) {
68+
return self::make([]);
69+
}
70+
71+
return self::fromJsonString($content);
72+
}
73+
5974
public static function fromYamlFile(string $yamlFile): self
6075
{
6176
if (file_exists($yamlFile)) {

tests/Feature/UrlTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use HiFolks\DataType\Block;
4+
use Symfony\Component\HttpClient\HttpClient;
45

56
it('remote json', function (): void {
67

@@ -15,6 +16,20 @@
1516

1617
})->group("url");
1718

19+
it('remote json (Symfony\Contracts\HttpClient\HttpClientInterface)', function (): void {
20+
$url = "https://api.github.com/repos/hi-folks/data-block/commits";
21+
22+
$commits = Block::fromHttpJsonUrl($url, HttpClient::create());
23+
expect($commits)->toBeInstanceOf(Block::class);
24+
expect($commits)->toHaveCount(30);
25+
26+
$myCommits = $commits->where("commit.author.name", "like", "Roberto");
27+
foreach ($myCommits as $value) {
28+
expect($value->get("commit.message"))->toBeString();
29+
}
30+
31+
})->group("url");
32+
1833
it('remote dummyjson post', function (): void {
1934

2035
$url = "https://dummyjson.com/posts";

0 commit comments

Comments
 (0)