Skip to content

Commit

Permalink
STARTED REDOING with Guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 11, 2016
1 parent db1a248 commit cb1f5be
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
39 changes: 36 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* 'hiart' => [
* 'class' => 'hiqdev\hiart\Connection',
* 'config' => [
* 'api_url' => 'https://api.site.com/',
* 'base_uri' => 'https://api.site.com/',
* ],
* ],
* ],
Expand All @@ -37,6 +37,9 @@ class Connection extends Component
{
const EVENT_AFTER_OPEN = 'afterOpen';

/**
* @var array Config
*/
public $config = [];

public $connectionTimeout = null;
Expand All @@ -45,6 +48,8 @@ class Connection extends Component

public static $curl = null;

protected static $guzzle = null;

/**
* Authorization config.
*
Expand Down Expand Up @@ -223,10 +228,12 @@ public function perform($url, $options = [])
*/
public function makeRequest($method, $url, $options = [], $body = null, $raw = false)
{
$result = $this->httpRequest($method, $this->createUrl($url), http_build_query($options), $raw);
#$result = $this->curlRequest($method, $this->createUrl($url), http_build_query($options), $raw);
$result = $this->guzzleRequest($method, $this->createUrl($url), $options, $raw);

return $this->checkResponse($result, $url, $options);
}

/**
* Creates URL.
* @param mixed $path path
Expand All @@ -251,6 +258,32 @@ private function createUrl($path, array $options = [])
return [$this->config['api_url'], $url];
}

protected function guzzleRequest($method, $url, $body = null, $raw = false)
{
$method = strtoupper($method);
$profile = $method . ' ' . $url[1] . '#' . (is_array($body) ? http_build_query($body) : $body);
$options = [(is_array($body) ? 'form_params' : 'body') => $body];
Yii::beginProfile($profile, __METHOD__);
$response = $this->getGuzzle()->request($method, $url[1], $options);
Yii::endProfile($profile, __METHOD__);

$res = $response->getBody()->getContents();
if (!$raw && in_array('application/json', $response->getHeader('Content-Type'))) {
$res = Json::decode($res);
}

return $res;
}

public function getGuzzle()
{
if (static::$guzzle === null) {
static::$guzzle = new \GuzzleHttp\Client($this->config);
}

return static::$guzzle;
}

/**
* Performs HTTP request.
* @param string $method method name
Expand All @@ -261,7 +294,7 @@ private function createUrl($path, array $options = [])
* @throws HiArtException
* @return mixed if request failed
*/
protected function httpRequest($method, $url, $requestBody = null, $raw = false)
protected function curlRequest($method, $url, $requestBody = null, $raw = false)
{
$method = strtoupper($method);
// response body and headers
Expand Down
2 changes: 1 addition & 1 deletion src/DebugPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function save()
{
$target = $this->module->logTarget;
$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE,
['hiqdev\hiart\Connection::httpRequest']);
['hiqdev\hiart\Connection::guzzleRequest']);

return ['messages' => $messages];
}
Expand Down

0 comments on commit cb1f5be

Please sign in to comment.