Skip to content

Commit

Permalink
stream transport looks working
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 23, 2017
1 parent 2d4076c commit d632d37
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 25 deletions.
15 changes: 7 additions & 8 deletions src/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ public function setErrorChecker($checker)

/**
* Checks response with checkError method and raises exception if error.
* @param Response $response response data from API
* @param ResponseInterface $response response data from API
* @throws ErrorResponseException
* @return mixed response data
*/
public function checkResponse(Response $response)
public function checkResponse(ResponseInterface $response)
{
$error = $this->checkError($response);
if ($error) {
Expand All @@ -226,16 +226,14 @@ public function checkResponse(Response $response)
'response' => $response->getData(),
]);
}

return $response->getData();
}

/**
* Checks response with errorChecker callback and returns error text if error.
* @param Response $response
* @param ResponseInterface $response
* @return string|false error text or false
*/
public function checkError(Response $response)
public function checkError(ResponseInterface $response)
{
if (isset($this->_errorChecker)) {
return call_user_func($this->_errorChecker, $response);
Expand All @@ -246,13 +244,14 @@ public function checkError(Response $response)

/**
* Default error checker. TODO check something in response?
* @param Response $response
* @param ResponseInterface $response
* @return bool
*/
public function isError(Response $response)
public function isError(ResponseInterface $response)
{
return false;
}

public function getBaseUri()
{
return $this->baseUri;
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use yii\helpers\Json;

abstract class AbstractResponse
abstract class AbstractResponse implements ResponseInterface
{
/**
* @var RequestInterface
Expand Down Expand Up @@ -60,7 +60,7 @@ public function isRaw()

public function isJson()
{
return preg_grep('|application/json|i', $this->getHeader('Content-Type'));
return !empty(preg_grep('|application/json|i', $this->getHeader('Content-Type')));
}

abstract public function getHeader($name);
Expand Down
29 changes: 18 additions & 11 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,27 @@ public function getPrimaryValue()
}

/**
* Returns the list of all attribute names of the model.
*
* This method must be overridden by child classes to define available attributes.
*
* Attributes are names of fields of the corresponding API object.
* The primaryKey for HiArt documents is the `id` field by default which is not part of the attributes.
*
* @throws \yii\base\InvalidConfigException if not overridden in a child class
*
* @return string[] list of attribute names
* Returns the list of attribute names.
* By default, this method returns all attributes mentioned in rules.
* You may override this method to change the default behavior.
* @return string[] list of attribute names.
*/
public function attributes()
{
throw new InvalidConfigException('The attributes() method of HiArt ActiveRecord has to be implemented by child classes.');
$attributes = [];
foreach ($this->rules() as $rule) {
if (is_string(reset($rule))) {
continue;
}
foreach (reset($rule) as $attribute) {
if (substr_compare($attribute, '!', 0, 1) === 0) {
$attribute = mb_substr($attribute, 1);
}
$attributes[$attribute] = $attribute;
}
}

return array_values($attributes);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public function send($options = [])
Yii::beginProfile($profile, $category);
$response = $this->request->send($options);
Yii::endProfile($profile, $category);
$this->db->checkResponse($response);

return $this->db->checkResponse($response);
return $response->getData();
}

public static function getProfileCategory()
Expand Down
4 changes: 4 additions & 0 deletions src/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

interface ResponseInterface
{
public function getRequest();

public function getData();

public function getRawData();

public function getHeader($name);
Expand Down
18 changes: 17 additions & 1 deletion src/rest/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@

namespace hiqdev\hiart\rest;

class Connection extends AbstractConnection
use hiqdev\hiart\ResponseInterface;
use hiqdev\hiart\ResponseErrorException;

class Connection extends \hiqdev\hiart\AbstractConnection
{
public $queryBuilderClass = QueryBuilder::class;

public function checkResponse(ResponseInterface $response)
{
$code = $response->getStatusCode();
if ($code>=200 && $code<300) {
return;
}

throw new ResponseErrorException($response->getReasonPhrase(), [
'request' => $response->getRequest()->getParts(),
'response' => $response->getData(),
], $code);
}
}
40 changes: 38 additions & 2 deletions src/stream/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ class Response extends AbstractResponse

protected $headers;

public function __construct(Request $request, $rawData, array $headers)
protected $statusCode;

protected $reasonPhrase;

public function __construct(Request $request, $rawData, array $rawHeaders)
{
$this->request = $request;
$this->rawData = $rawData;
$this->headers = $headers;
$this->headers = $this->parseHeaders($rawHeaders);
}

public function getRawData()
Expand All @@ -37,6 +41,38 @@ public function getRawData()

public function getHeader($name)
{
$name = strtolower($name);

return isset($this->headers[$name]) ? $this->headers[$name] : null;
}

public function parseHeaders($headers)
{
foreach ($headers as $header) {
if (strncmp($header, 'HTTP/', 5) === 0) {
$parts = explode(' ', $header, 3);
$this->version = substr($parts[0], 6);
$this->statusCode = $parts[1];
$this->reasonPhrase = $parts[2];
} elseif (($pos = strpos($header, ':')) !== false) {
$name = strtolower(trim(substr($header, 0, $pos)));
$value = trim(substr($header, $pos + 1));
$result[$name][] = $value;
} else {
$result['raw'][] = $header;
}
}

return $result;
}

public function getStatusCode()
{
return $this->statusCode;
}

public function getReasonPhrase()
{
return $this->reasonPhrase;
}
}

0 comments on commit d632d37

Please sign in to comment.