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

Use HttpClient in SessionTracker #582

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 85 additions & 52 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
class Client
{
/**
* The default endpoint.
*
* @var string
* @deprecated Use {@see Configuration::NOTIFY_ENDPOINT} instead
*/
const ENDPOINT = 'https://notify.bugsnag.com';
imjoehaines marked this conversation as resolved.
Show resolved Hide resolved
const ENDPOINT = Configuration::NOTIFY_ENDPOINT;

/**
* The config instance.
Expand Down Expand Up @@ -78,21 +76,34 @@ class Client
*
* If you don't pass in a key, we'll try to read it from the env variables.
*
* @param string|null $apiKey your bugsnag api key
* @param string|null $endpoint your bugsnag endpoint
* @param bool $default if we should register our default callbacks
* @param string|null $apiKey your bugsnag api key
* @param string|null $notifyEndpoint your bugsnag notify endpoint
* @param bool $default if we should register our default callbacks
*
* @return static
*/
public static function make($apiKey = null, $endpoint = null, $defaults = true)
{
// Retrieves environment variables
public static function make(
$apiKey = null,
$notifyEndpoint = null,
$defaults = true
) {
$env = new Env();

$config = new Configuration($apiKey ?: $env->get('BUGSNAG_API_KEY'));
$guzzle = static::makeGuzzle($endpoint ?: $env->get('BUGSNAG_ENDPOINT'));
if ($apiKey === null) {
$apiKey = $env->get('BUGSNAG_API_KEY');
}

$config = new Configuration($apiKey);

if ($notifyEndpoint === null) {
$notifyEndpoint = $env->get('BUGSNAG_ENDPOINT');
}

if (is_string($notifyEndpoint)) {
$config->setNotifyEndpoint($notifyEndpoint);
}

$client = new static($config, null, $guzzle);
$client = new static($config);

if ($defaults) {
$client->registerDefaultCallbacks();
Expand All @@ -102,23 +113,23 @@ public static function make($apiKey = null, $endpoint = null, $defaults = true)
}

/**
* Create a new client instance.
*
* @param \Bugsnag\Configuration $config
* @param \Bugsnag\Request\ResolverInterface|null $resolver
* @param \GuzzleHttp\ClientInterface|null $guzzle
* @param \Bugsnag\Shutdown\ShutdownStrategyInterface|null $shutdownStrategy
*
* @return void
* @param Configuration $config
* @param ResolverInterface|null $resolver
* @param ClientInterface|null $guzzle
* @param ShutdownStrategyInterface|null $shutdownStrategy
*/
public function __construct(Configuration $config, ResolverInterface $resolver = null, ClientInterface $guzzle = null, ShutdownStrategyInterface $shutdownStrategy = null)
{
public function __construct(
Configuration $config,
ResolverInterface $resolver = null,
ClientInterface $guzzle = null,
ShutdownStrategyInterface $shutdownStrategy = null
) {
$this->config = $config;
$this->resolver = $resolver ?: new BasicResolver();
$this->recorder = new Recorder();
$this->pipeline = new Pipeline();
$this->http = new HttpClient($config, $guzzle ?: static::makeGuzzle());
$this->sessionTracker = new SessionTracker($config);
$this->sessionTracker = new SessionTracker($config, $this->http);

$this->registerMiddleware(new NotificationSkipper($config));
$this->registerMiddleware(new BreadcrumbData($this->recorder));
Expand All @@ -132,17 +143,15 @@ public function __construct(Configuration $config, ResolverInterface $resolver =
/**
* Make a new guzzle client instance.
*
* @param string|null $base
* @param array $options
* @param string $base No longer used
* @param array $options
*
* @return ClientInterface
*
* @return \GuzzleHttp\ClientInterface
* @deprecated Will be removed in a future release
*/
public static function makeGuzzle($base = null, array $options = [])
{
$key = method_exists(ClientInterface::class, 'request') ? 'base_uri' : 'base_url';

$options[$key] = $base ?: static::ENDPOINT;

imjoehaines marked this conversation as resolved.
Show resolved Hide resolved
if ($path = static::getCaBundlePath()) {
$options['verify'] = $path;
}
Expand Down Expand Up @@ -768,23 +777,33 @@ public function shouldIgnoreErrorCode($code)
}

/**
* Set session tracking state and pass in optional guzzle.
* Set notification delivery endpoint.
*
* @param bool $track whether to track sessions
* @param string $endpoint
*
* @return $this
*/
public function setAutoCaptureSessions($track)
public function setNotifyEndpoint($endpoint)
{
$this->config->setAutoCaptureSessions($track);
$this->config->setNotifyEndpoint($endpoint);

return $this;
}

/**
* Get notification delivery endpoint.
*
* @return string
*/
public function getNotifyEndpoint()
{
return $this->config->getNotifyEndpoint();
}

/**
* Set session delivery endpoint.
*
* @param string $endpoint the session endpoint
* @param string $endpoint
*
* @return $this
*/
Expand All @@ -796,27 +815,17 @@ public function setSessionEndpoint($endpoint)
}

/**
* Get the session client.
*
* @return \GuzzleHttp\ClientInterface
*/
public function getSessionClient()
{
return $this->config->getSessionClient();
}

/**
* Whether should be auto-capturing sessions.
* Get session delivery endpoint.
*
* @return bool
* @return string
*/
public function shouldCaptureSessions()
public function getSessionEndpoint()
{
return $this->config->shouldCaptureSessions();
return $this->config->getSessionEndpoint();
}

/**
* Sets the build endpoint.
* Set the build endpoint.
*
* @param string $endpoint the build endpoint
*
Expand All @@ -830,12 +839,36 @@ public function setBuildEndpoint($endpoint)
}

/**
* Returns the build endpoint.
* Get the build endpoint.
*
* @return string
*/
public function getBuildEndpoint()
{
return $this->config->getBuildEndpoint();
}

/**
* Set session tracking state and pass in optional guzzle.
*
* @param bool $track whether to track sessions
*
* @return $this
*/
public function setAutoCaptureSessions($track)
{
$this->config->setAutoCaptureSessions($track);

return $this;
}

/**
* Whether should be auto-capturing sessions.
*
* @return bool
*/
public function shouldCaptureSessions()
{
return $this->config->shouldCaptureSessions();
}
}
Loading