Skip to content

Commit

Permalink
persistent connection to Db, verbosing bad response code in ApiHelper…
Browse files Browse the repository at this point in the history
…, fix for null value parameters in request
  • Loading branch information
Pavel Asanov committed Sep 7, 2015
1 parent 37ce3a5 commit a8a44c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
21 changes: 15 additions & 6 deletions classes/api/BaseRequest.php
Expand Up @@ -96,16 +96,25 @@ protected function createUrlPath()
*/
protected function prepareParams()
{
// Избавляемся от параметров со значением null
$params = [];
if (isset($this->params))
foreach($this->params as $key => $value)
if (isset($value))
$params[$key] = $value;

// Для запросов с query помещаем параметры в URL, а массив параметров сбрасываем
if (strcasecmp($this->http_method, 'GET') == 0
|| strcasecmp($this->http_method, 'DELETE') == 0) {
if (strcasecmp($this->http_method, 'GET') === 0
|| strcasecmp($this->http_method, 'DELETE') === 0) {

$this->url->setQuery($this->params);
$this->params = null;
$this->url->setQuery($params);
$params = null;

} // Для запросов с body проверяем наличие картинки, и если она есть - подставляем к пути @, чтобы Guzzle загрузил её
elseif (isset($this->params['image']))
$this->params['image'] = '@' . $this->params['image'];
elseif (isset($params['image']))
$params['image'] = '@' . $params['image'];

$this->params = $params;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions classes/helpers/ApiHelper.php
Expand Up @@ -33,8 +33,17 @@ protected function send()

$this->response = $this->request->send();

if (substr($this->getResponseBody()->code, 0, 2) !== '20')
throw new Exception('FlampAPIHelper returned wrong response code');
if (!$this->getResponseBody())
throw new Exception(
"\n{$this->request->logRequestHttpMethod()} {$this->request->logRequestUrl()}\nAPIHelper returned no response code"
);
else
$response_code = $this->getResponseBody()->code;

if (substr($response_code, 0, 2) !== '20')
throw new Exception(
"\n{$this->request->logRequestHttpMethod()} {$this->request->logRequestUrl()}\nAPIHelper returned wrong response code:\n\n{$this->response->getBody()}"
);
}

public function getResponseBody()
Expand Down
2 changes: 1 addition & 1 deletion classes/helpers/BaseDbHelper.php
Expand Up @@ -25,7 +25,7 @@ class BaseDbHelper
public function __construct($db_name, $db_host, $db_user, $db_password, $verbose = false)
{
$this->db = new PDO("pgsql:dbname={$db_name};host={$db_host};",
$db_user, $db_password);
$db_user, $db_password, [PDO::ATTR_PERSISTENT => true]);
$this->verbose = $verbose;
}

Expand Down

0 comments on commit a8a44c6

Please sign in to comment.