Skip to content

Commit

Permalink
Merge pull request #31 from storyblok/feature/api-v2
Browse files Browse the repository at this point in the history
Feature/api v2
  • Loading branch information
christianzoppi authored Jan 12, 2022
2 parents 9924c74 + a8836a3 commit 1b93e7d
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 82 deletions.
186 changes: 182 additions & 4 deletions src/Storyblok/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BaseClient
* @param string $apiVersion
* @param bool $ssl
*/
function __construct($apiKey = null, $apiEndpoint = "api.storyblok.com", $apiVersion = "v1", $ssl = false)
function __construct($apiKey = null, $apiEndpoint = "api.storyblok.com", $apiVersion = "v2", $ssl = false)
{
$handlerStack = HandlerStack::create(new CurlHandler());
$handlerStack->push(Middleware::retry($this->retryDecider(), $this->retryDelay()));
Expand Down Expand Up @@ -222,21 +222,21 @@ public function get($endpointUrl, $queryString = array())
if ($this instanceof ManagementClient) {
$requestOptions[RequestOptions::HEADERS] = ['Authorization' => $this->apiKey];
}

$responseObj = $this->client->request('GET', $endpointUrl, $requestOptions);

return $this->responseHandler($responseObj);
return $this->responseHandler($responseObj, $queryString);
} catch (\GuzzleHttp\Exception\ClientException $e) {
throw new ApiException(self::EXCEPTION_GENERIC_HTTP_ERROR . ' - ' . $e->getMessage(), $e->getCode());
}
}

/**
* @param \Psr\Http\Message\ResponseInterface $responseObj
* @param array $queryString
*
* @return \stdClass
*/
public function responseHandler($responseObj)
public function responseHandler($responseObj, $queryString = array())
{
$httpResponseCode = $responseObj->getStatusCode();
$data = (string) $responseObj->getBody();
Expand All @@ -247,6 +247,10 @@ public function responseHandler($responseObj)
$result->httpResponseBody = $data && empty($jsonResponseData) ? $data : $jsonResponseData;
$result->httpResponseCode = $httpResponseCode;
$result->httpResponseHeaders = $responseObj->getHeaders();

if (is_array($result->httpResponseBody) && isset($result->httpResponseBody['story']) || isset($result->httpResponseBody['stories'])) {
$result->httpResponseBody = $this->enrichStories($result->httpResponseBody, $queryString);
}
return $result;
}

Expand Down Expand Up @@ -302,4 +306,178 @@ public function getCode()
{
return $this->responseCode;
}

private function enrichStories($data, $queryString) {
$enrichedData = $data;
$this->getResolvedRelations($data, $queryString);
$this->getResolvedLinks($data, $queryString);

if(isset($data['story'])) {
$enrichedData['story']['content'] = $this->enrichContent($data['story']['content']);
} else if(isset($data['stories'])) {
$stories = [];
foreach($data['stories'] as $index => $story) {
$story = $data['stories'][$index];
$story['content'] = $this->enrichContent($story['content']);
$stories[] = $story;
}
$enrichedData['stories'] = $stories;
}
return $enrichedData;
}

/**
* Enrich the Stories with resolved links and stories
* @param \stdClass
*
* @return \stdClass
*/
function enrichContent($data) {
$enrichedContent = $data;

if (is_array($data) && isset($data['component'])) {
if(!isset($story['_stopResolving'])) {
foreach($data as $fieldName => $fieldValue) {
$enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue);
$enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]);
$enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName]);
}
}
} else if (is_array($data)) {
foreach($data as $key => $value) {
$enrichedContent[$key] = $this->enrichContent($value);
}
}

return $enrichedContent;
}

/**
* Retrieve or resolve the relations
* @param \stdClass $data
* @param array $queryString
*
* @return null
*/
function getResolvedRelations($data, $queryString) {
$this->resolvedRelations = [];
$relations = [];

if(isset($data['rels'])) {
$relations = $data['rels'];
} else if(isset($data['rel_uuids'])) {
$relSize = count($data['rel_uuids']);
$chunks = [];
$chunkSize = 50;

for ($i = 0; $i < $relSize; $i += $chunkSize) {
$end = min($relSize, $i + $chunkSize);
$chunks[] = array_slice($data['rel_uuids'], $i, $end);
}

for ($chunkIndex = 0; $chunkIndex < count($chunks); $chunkIndex++) {
$relationsParams = array(
'per_page' => $chunkSize,
'by_uuids' => implode(',', $chunks[$chunkIndex])
);
if(isset($queryString['language'])) {
$relationsParams['language'] = $queryString['language'];
}
$relationsRes = $this->getStories($relationsParams);

$relations = array_merge($relations, $relationsRes->responseBody['stories']);
}
}

foreach($relations as $rel) {
$this->resolvedRelations[$rel['uuid']] = $rel;
}
}


/**
* Retrieve or resolve the Links
* @param \stdClass $data
* @param array $queryString
*
* @return null
*/
function getResolvedLinks($data) {
$this->resolvedLinks = [];
$links = [];

if(isset($data['links'])) {
$links = $data['links'];
} else if(isset($data['link_uuids'])) {
$linksSize = count($data['link_uuids']);
$chunks = [];
$chunkSize = 50;

for ($i = 0; $i < $linksSize; $i += $chunkSize) {
$end = min($linksSize, $i + $chunkSize);
$chunks[] = array_slice($data['link_uuids'], $i, $end);
}

for ($chunkIndex = 0; $chunkIndex < count($chunks); $chunkIndex++) {
$linksRes = $this->getStories(array(
'per_page' => $chunkSize,
'language' => isset($queryString['language']) ? $queryString['language'] : 'default',
'by_uuids' => implode(',', $chunks[$chunkIndex])
));

$links = array_merge($links, $linksRes->responseBody['stories']);
}
}
foreach($links as $link) {
$this->resolvedLinks[$link['uuid']] = $link;
}
}

/**
* Insert the resolved relations in a story
* @param string $component
* @param string $field
* @param string|array $value
*
* @return null
*/
private function insertRelations($component, $field, $value) {
$filteredNode = $value;
if(isset($this->_relationsList[$component]) && $field == $this->_relationsList[$component]) {
if(is_string($value)) {
if(isset($this->resolvedRelations[$value])) {
$filteredNode = $this->resolvedRelations[$value];
$filteredNode['_stopResolving'] = true;
}
} else if(is_array($value)) {
$filteredNode = [];
foreach($value as $item) {
if(is_string($item) && isset($this->resolvedRelations[$item])) {
$story = $this->resolvedRelations[$item];
$story['_stopResolving'] = true;
$filteredNode[] = $story;
}
}
}
}
return $filteredNode;
}

/**
* Insert the resolved links in a story
* @param \stdClass $node
*
* @return \stdClass
*/
private function insertLinks($node) {
$filteredNode = $node;
if(isset($node['fieldtype']) && $node['fieldtype'] == 'multilink' && $node['linktype'] == 'story') {
if(isset($node['id']) && is_string($node['id']) && isset($this->resolvedLinks[$node['id']])) {
$filteredNode['story'] = $this->resolvedLinks[$node['id']];
} else if(isset($node['uuid']) && is_string($node['uuid']) && isset($this->resolvedLinks[$node['uuid']])) {
$filteredNode['story'] = $this->resolvedLinks[$node['uuid']];
}
}
return $filteredNode;
}
}
Loading

0 comments on commit 1b93e7d

Please sign in to comment.