Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to provide my own http client implementation? #1004

Closed
enumag opened this issue Apr 27, 2020 · 6 comments
Closed

How to provide my own http client implementation? #1004

enumag opened this issue Apr 27, 2020 · 6 comments

Comments

@enumag
Copy link
Contributor

enumag commented Apr 27, 2020

Hi, I'd like to use this library but since it's not tied into any specific Http library I'd like to provide my own implementation. How can I do that?

I'm not interested in any existing guzzle-based, curl-based or any other adapter. I don't even want to use php-http/discovery. I simply want to specify my own implementation which will be based on amphp/http-client. (As far as I know there is no existing package providing the integration.)

So far I was unable to find a way to do that. The documentation only states that the sdk is not tied to any specific implementation but there doesn't seem to be any option to provide my own.

@enumag
Copy link
Contributor Author

enumag commented Apr 27, 2020

I found a setHttpClient method but it's deprecated. And it doesn't specify what I should use instead.

@Jean85
Copy link
Collaborator

Jean85 commented Apr 27, 2020

That deprecation was added in #855, where we introduced the setTransportFactory method. You should use that one, and create your transport inside the TransportFactory implementation that you would provide.

@enumag
Copy link
Contributor Author

enumag commented Apr 27, 2020

Yeah I'm trying to do that... I got this far:

$options = ['dsn' => '...'];
$client = ClientBuilder::create($options)
    ->setTransportFactory(
		new class implements \Sentry\Transport\TransportFactoryInterface {
			public function create(Options $options): TransportInterface
			{
				return new HttpTransport(
					$options,
					/* provide http client factory */,
                    /* provide request factory */,
					true,
					false
				);
			}
		}
    )
    ->getClient();
SentrySdk::init()->bindClient($client);

The current problem I'm trying to solve is finding request factory. This library is using MessageRequestDiscovery::find() but that is deprecated as well so I don't want to use it.

It says I should use \Http\Discovery\Psr17FactoryDiscovery instead, presumably this means Psr17FactoryDiscovery::findRequestFactory(). So I tried that.

This however returns Psr\Http\Message\RequestFactoryInterface but Sentry\Transport\HttpTransport wants Http\Message\RequestFactory.


Overall navigating through all of this deprecated stuff is getting very complicated. Especially since this library itself is also relying on deprecated API. From a library that claims to not be tied to any specific Http implementation, providing my own is extremely difficult.

@Jean85
Copy link
Collaborator

Jean85 commented Apr 27, 2020

Unfortunately this lib predates a few of the PSR standards that we should use for that. For example, HttpTransport should use Psr\Http\Message\RequestFactoryInterface, but we probably couldn't change that to avoid breaking BC.

You do not have to use HttpTransport though, you can implement TransportInterface directly, and ClosableTransportInterface if you want.

@enumag
Copy link
Contributor Author

enumag commented Apr 28, 2020

Yeah, I did that... It works in the end... but the entire hack has like 120 lines and I return the event id even on failure because I can't wait for the result (#1005).

Also I'm still not sure how to deal with exceptions since Deferred::reject() requires a ClientExceptionInterface instead of Throwable. Can you give me some advice how to handle that?

@ste93cry
Copy link
Collaborator

I'm closing this due to the fact that a lot of time passed, and in the meanwhile the version 3 of the SDK got released. Feel free to open a new issue if the issue persist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants