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

Adding param force_ip_resolve for HttpClient #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class Analytics
* @var array
*/
protected $options = [];

/**
* Initializes to a list of all the available parameters to be sent in a hit.
*
Expand Down Expand Up @@ -609,6 +609,10 @@ protected function getHttpClientOptions()
$options['timeout'] = $this->options['timeout'];
}

if (isset($this->options['force_ip_resolve'])) {
$options['force_ip_resolve'] = $this->options['force_ip_resolve'];
}

return $options;
}

Expand Down
30 changes: 25 additions & 5 deletions src/Network/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Promise;
use GuzzleHttp\FORCE_IP_RESOLVE;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -95,11 +96,9 @@ public function post($url, array $options = [])
);

$opts = $this->parseOptions($options);
$response = $this->getClient()->sendAsync($request, [
'synchronous' => !$opts['async'],
'timeout' => $opts['timeout'],
'connect_timeout' => $opts['timeout'],
]);
$opts = $this->prepareAsyncOptions($opts);

$response = $this->getClient()->sendAsync($request, $opts);

if ($opts['async']) {
self::$promises[] = $response;
Expand All @@ -110,6 +109,26 @@ public function post($url, array $options = [])
return $this->getAnalyticsResponse($request, $response);
}


/**
* @param array $options
* @return array
*/
public function prepareAsyncOptions(array $options)
{
$opts = array(
'synchronous' => !$options['async'],
'timeout' => $options['timeout'],
'connect_timeout' => $options['timeout'],
);

if (!empty($options['force_ip_resolve'])) {
$opts['force_ip_resolve'] = $options['force_ip_resolve'];
}

return $opts;
}

/**
* Parse the given options and fill missing fields with default values.
*
Expand All @@ -121,6 +140,7 @@ private function parseOptions(array $options)
$defaultOptions = [
'timeout' => static::REQUEST_TIMEOUT_SECONDS,
'async' => false,
'force_ip_resolve' => '',
];

$opts = [];
Expand Down