Giter Club home page Giter Club logo

Comments (4)

tacman avatar tacman commented on July 28, 2024 1

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.

ToshY avatar ToshY commented on July 28, 2024

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.

from bunnynet-php.

tacman avatar tacman commented on July 28, 2024

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.

ToshY avatar ToshY commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.