Skip to content

Commit

Permalink
- add headers array to WtiApiRequest
Browse files Browse the repository at this point in the history
- add pagination support to listStrings API method
  • Loading branch information
HarryLyu committed Apr 23, 2014
1 parent a46f61f commit b2e5fd8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/LinguaLeo/wti/WtiApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,35 @@ public function getStringsByKey($key, $fileId)
return $this->listStrings($params);
}

public function listStrings($params = null) {
public function listStrings($params = null, $page = 1)
{
if ($params == null) {
$params = [];
}
$params['page'] = intval($page);
$this->request = $this->builder()
->setMethod(RequestMethod::GET)
->setEndpoint('strings')
->setParams($params)
->build();
$this->request->run();
$result = $this->request->getResult();
return $result ? $result : [];
$requestResult = $this->request->getResult();
$result = $requestResult ? $requestResult : [];

if (count($result) && $this->isRequestHasNextPage()) {
$result += $this->listStrings($params, ++$page);
}

return $result;
}

private function isRequestHasNextPage()
{
$responseHeaders = $this->request->getHeaders();
if (isset($responseHeaders['Link'])) {
return true;
}
return false;
}

/**
Expand All @@ -141,7 +161,8 @@ public function listStrings($params = null) {
* @param $localeCode
* @return mixed|null
*/
public function getTranslation ($stringId, $localeCode) {
public function getTranslation($stringId, $localeCode)
{
$this->request = $this->builder()
->setMethod(RequestMethod::GET)
->setEndpoint('strings/' . $stringId . '/locales/' . $localeCode . '/translations')
Expand Down Expand Up @@ -384,7 +405,8 @@ public function updateFile(
$minorChanges = false,
$label = null,
$mime = 'application/json'
) {
)
{
$params = [
'name' => $name,
'merge' => $merge,
Expand Down
35 changes: 34 additions & 1 deletion src/LinguaLeo/wti/WtiApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WtiApiRequest
private $error;
private $errno;
private $result;
private $headers;
private $isRequestRunned = false;

public function __construct($resource)
Expand All @@ -21,13 +22,38 @@ public function __construct($resource)
public function run()
{
$this->isRequestRunned = true;
$this->result = curl_exec($this->resource);
$result = curl_exec($this->resource);

$header_size = curl_getinfo($this->resource, CURLINFO_HEADER_SIZE);
$this->headers = $this->prepareHeaders(substr($result, 0, $header_size));
$this->result = substr($result, $header_size);

if ($this->result === false) {
$this->error = curl_error($this->resource);
$this->errno = curl_errno($this->resource);
}
}

private function prepareHeaders ($headersString) {
$headersArray = explode("\n", $headersString);
// Remove first header, HTTP code
array_shift($headersArray);

$headersAssoc = [];

foreach ($headersArray as $header) {
if ($header === '') {
continue;
}
preg_match('~^([^:]*)\:(.*)$~', $header, $matches);
if (count($matches) == 3) {
$headersAssoc[$matches[1]] = trim($matches[2]);
}
}

return $headersAssoc;
}

public function getError()
{
return $this->error;
Expand All @@ -54,4 +80,11 @@ public function getResult($assoc = false)
return $this->result ? json_decode($this->result, $assoc) : null;
}

public function getHeaders () {
if (!$this->isRequestRunned) {
throw new WtiApiException("Request must be performed before getting results.");
}
return $this->headers;
}

}
1 change: 1 addition & 0 deletions src/LinguaLeo/wti/WtiRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function build()
}
curl_setopt($this->resource, CURLOPT_URL, $this->buildRequestUrl());
curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->resource, CURLOPT_HEADER, 1);
curl_setopt($this->resource, CURLOPT_CUSTOMREQUEST, $this->method);
if ($this->jsonEncodeParams) {
curl_setopt($this->resource, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
Expand Down

0 comments on commit b2e5fd8

Please sign in to comment.