Comments (4)
Thanks!!
I see a lot of libraries requiring Guzzle or simply using the low-level curl calls, and I've always wondering how to remove that dependency. I appreciate the detailed explanation (and this bundle!)
from bunnynet-php.
Hey @tacman 👋
Okay let's just take it step-by-step then.
I'd like to use the Symfony Client, which implement the psr interface (second on this list: https://packagist.org/providers/psr/http-client-implementation)
While Symfony provides a PSR-18 implementation, it does not automatically mean the default HttpClient
is also PSR-18 compatible.
But when I try to initialize the library
$bunnyClient = new BunnyClient( client: (new HttpClient()) // or inject the default http client, same problem );
I get ToshY\BunnyNet\Client\BunnyClient::__construct(): Argument #1 ($client) must be of type Psr\Http\Client\ClientInterface, Symfony\Component\HttpClient\TraceableHttpClient given
As you've noticed, the exception shows that a Symfony\Component\HttpClient\TraceableHttpClient
was given, which does not implement the Psr\Http\Client\ClientInterface
but it's own Symfony\Contracts\HttpClient\HttpClientInterface
. The same goes for the majority of other Symfony HttpClient
implementations.
The solution however can be found in the HTTP Client documentation (section "PSR-18 and PSR-17").
I'm sure I'm just confused about the interfaces, perhaps you could add something to the documentation as to how to use your own http client.
The current documentation states with a comment that the HTTP client you want to use must implement Psr\Http\Client\ClientInterface
, and also gives as an example to use new \Symfony\Component\HttpClient\Psr18Client()
:
// Create a BunnyClient using any HTTP client implementing "Psr\Http\Client\ClientInterface".
$bunnyClient = new BunnyClient(
client: new \Symfony\Component\HttpClient\Psr18Client(),
);
From my perspective I'm not sure what's unclear about this, but if you have suggestions on how you think this can be improved, let me know and I can take it into consideration.
As an extra I can give you an additional example on how I currently have my own setup in a Symfony 6.4
application:
config/services.yaml
services:
custom.http_client:
class: Symfony\Component\HttpClient\Psr18Client
bunny.client:
class: ToshY\BunnyNet\Client\BunnyClient
bind:
$client: '@custom.http_client'
bunny.base.api:
class: ToshY\BunnyNet\BaseAPI
bind:
$apiKey: '%env(BUNNY_API_KEY)%'
$client: '@bunny.client'
And then in your service you can use them like so:
src/Service/AwesomeBunnyService
declare(strict_types=1);
namespace App\Service;
use ToshY\BunnyNet\BaseAPI;
class AwesomeBunnyService
{
public function __construct(
private readonly BaseAPI $bunnyBaseAPI
) {
}
}
TLDR;
Provide a HTTP client that implements Psr\Http\Client\ClientInterface
.
Symfony\Component\HttpClient\Psr18Client
GuzzleHttp\Client
- ...or any other client implementing
Psr\Http\Client\ClientInterface
from bunnynet-php.
Have you considered creating a Symfony bundle around this library? So you could skip the services.yaml in the application and just let it autowire?
from bunnynet-php.
Have you considered creating a Symfony bundle around this library? So you could skip the services.yaml in the application and just let it autowire?
Yes I have on several occasions.
In earlier days I didn't create a bundle basically because I lacked experience in both PHP as well as Symfony, and I focussed more into finding ways on making this library easier to maintain (for myself) and more flexible (like using PSR standards for example).
While I recently thought about it again I eventually decided not to. The reason: why would I spend time in creating a (framework specific) bundle while you can achieve the same result with a few extra lines in the config 🤔. If I'm not going to use it myself, creating/maintaining one would then feel more like a chore for me than actual fun to do (if that makes sense).
That being said, if you or someone else wants to create a bundle around this library go for it 🙂
from bunnynet-php.
Related Issues (20)
- [Feature]: System HOT 1
- [Feature]: Countries - Remove model unused page/perpage option
- [Feature]: Region - Remove model unused page/perpage option
- [Feature]: Stream Video Library - Get Languages endpoint
- [Feature]: Stream Video Library - List Video Libraries and Update Video Library query/body parameter updates
- [Feature]: DNS - List DNS Zones and Add/Update DNS record query/body parameter updates
- [Feature]: Pull Zone - Pull Zone and Edge Rule updates
- [Feature]: Storage Zone - List Storage Zones query parameter update
- [Feature]: TUS Resumable Uploads HOT 2
- [Feature]: Upload video from external folder like FTP HOT 7
- [Feature]: Stream API Upload Thumbnail HOT 4
- [Feature]: Fetch thumbnails with GetCollection HOT 4
- [Feature]: Stream API - Add `OEmbed` endpoint
- [Feature]: Stream API - Add `Get Video Play Data` endpoint
- [Feature]: Stream API - Add `Repackage Video` endpoint
- [Feature]: Stream API - Add `Transcribe Video` endpoint
- [Feature]: Add discussions to this repo
- [Bug]: Symfony stream error: "Registering the "symfony" stream wrapper failed" HOT 4
- [Bug]: (Laravel) Deleting video's caption not removing the vtt file HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bunnynet-php.