Skip to content

Commit

Permalink
Introduce read/post JSON returning wrappers
Browse files Browse the repository at this point in the history
Methods currently call read() or post() which returns a
Response - however, almost all methods do not care
about the response but always want JSON data.

Previously, Guzzle allowed chaing a ->json call on a
Response to get decoded JSON as array but this is no
longer possible, so these methods are introduced to
again cut down the code duplication in Response parsing.
  • Loading branch information
NamelessCoder authored and ksjogo committed Mar 14, 2017
1 parent e7fb792 commit adc3880
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 179 deletions.
35 changes: 27 additions & 8 deletions src/Bindings/Browser/AbstractBrowserBindingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Exception\RequestException;
use function json_decode;
use League\Url\Url;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -179,10 +180,7 @@ protected function getRepositoriesInternal($repositoryId = null)
}

$repositoryInfos = [];
$result = \json_decode(
$this->read($url)->getBody(),
true
);
$result = $this->readJson($url);
if (!is_array($result)) {
throw new CmisConnectionException(
'Could not fetch repository info! Response is not a valid JSON.',
Expand Down Expand Up @@ -234,6 +232,17 @@ protected function getServiceUrl()
return $this->getSession()->get(SessionParameter::BROWSER_URL);
}

/**
* Wrapper to read URL response as JSON as is the general use case.
*
* @param Url $url
* @return mixed
*/
protected function readJson(Url $url)
{
return json_decode($this->read($url)->getBody(), true);
}

/**
* Do a get request for the given url
*
Expand Down Expand Up @@ -395,6 +404,19 @@ protected function getSuccinct()
return $this->succinct;
}

/**
* Wrapper for calling post() and reading response as JSON, as is the general use case.
*
* @param Url $url
* @param array $content
* @param array $headers
* @return mixed
*/
protected function postJson(Url $url, $content = [], array $headers = [])
{
return \json_decode($this->post($url, $content, $headers)->getBody(), true);
}

/**
* Performs a POST on an URL, checks the response code and returns the
* result.
Expand Down Expand Up @@ -444,10 +466,7 @@ protected function getTypeDefinitionInternal($repositoryId, $typeId)
$url->getQuery()->modify([Constants::PARAM_TYPE_ID => $typeId]);

return $this->getJsonConverter()->convertTypeDefinition(
(array) \json_decode(
$this->read($url)->getBody(),
true
)
(array) $this->readJson($url)
);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Bindings/Browser/DiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getContentChanges(
$url->getQuery()->modify([Constants::PARAM_MAX_ITEMS => (string) $maxItems]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// $changeLogToken was passed by reference. The value is changed here
$changeLogToken = $responseData[JSONConstants::JSON_OBJECTLIST_CHANGE_LOG_TOKEN] ?? null;
Expand Down Expand Up @@ -142,8 +142,6 @@ public function query(
$url->getQuery()->modify([Constants::PARAM_MAX_ITEMS => (string) $maxItems]);
}

$responseData = (array) \json_decode($this->post($url)->getBody(), true);

return $this->getJsonConverter()->convertQueryResultList($responseData);
return $this->getJsonConverter()->convertQueryResultList((array) $this->postJson($url));
}
}
12 changes: 6 additions & 6 deletions src/Bindings/Browser/NavigationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getCheckedOutDocs(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertObjectList($responseData);
Expand Down Expand Up @@ -159,7 +159,7 @@ public function getChildren(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertObjectInFolderList($responseData);
Expand Down Expand Up @@ -215,7 +215,7 @@ public function getDescendants(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertDescendants($responseData);
Expand Down Expand Up @@ -249,7 +249,7 @@ public function getFolderParent(
$url->getQuery()->modify([Constants::PARAM_FILTER => (string) $filter]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertObject($responseData);
Expand Down Expand Up @@ -305,7 +305,7 @@ public function getFolderTree(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertDescendants($responseData);
Expand Down Expand Up @@ -358,7 +358,7 @@ public function getObjectParents(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

// TODO Implement Cache
return $this->getJsonConverter()->convertObjectParents($responseData);
Expand Down
57 changes: 17 additions & 40 deletions src/Bindings/Browser/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,16 @@ public function createDocument(
$queryArray[Constants::PARAM_VERSIONING_STATE] = (string) $versioningState;
}

$responseData = (array) \json_decode(
$this->post(
$newObject = $this->getJsonConverter()->convertObject(
(array) $this->postJson(
$url,
array_merge(
$queryArray,
['body' => $contentStream]
)
)->getBody(),
true
)
);

$newObject = $this->getJsonConverter()->convertObject($responseData);

if ($newObject) {
$newObjectId = $newObject->getId();
if ($contentStream) {
Expand Down Expand Up @@ -257,9 +254,8 @@ public function createDocumentFromSource(
if ($versioningState !== null) {
$queryArray[Constants::PARAM_VERSIONING_STATE] = (string) $versioningState;
}
$responseData = (array) \json_decode($this->post($url, $queryArray)->getBody(), true);

$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url, $queryArray));

return ($newObject === null) ? null : $newObject->getId();
}
Expand Down Expand Up @@ -301,9 +297,7 @@ public function createFolder(
$extension
);

$responseData = (array) \json_decode($this->post($url, $queryArray)->getBody(), true);

$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url, $queryArray));

return ($newObject === null) ? null : $newObject->getId();
}
Expand Down Expand Up @@ -349,9 +343,7 @@ public function createItem(
$extension
);

$responseData = (array) \json_decode($this->post($url, $queryArray)->getBody(), true);

$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url, $queryArray));

return ($newObject === null) ? null : $newObject->getId();
}
Expand Down Expand Up @@ -418,9 +410,7 @@ public function createRelationship(
$extension
);

$responseData = (array) \json_decode($this->post($url, $queryArray)->getBody(), true);

$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url, $queryArray));

return ($newObject === null) ? null : $newObject->getId();
}
Expand Down Expand Up @@ -460,8 +450,7 @@ public function deleteContentStream(
$url->getQuery()->modify([Constants::PARAM_CHANGE_TOKEN => $changeToken]);
}

$responseData = (array) \json_decode($this->post($url)->getBody(), true);
$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url));

// $objectId was passed by reference. The value is changed here to new object id
$objectId = null;
Expand Down Expand Up @@ -540,14 +529,7 @@ public function deleteTree(
$url->getQuery()->modify([Constants::PARAM_UNFILE_OBJECTS => (string) $unfileObjects]);
}

$response = $this->post($url);

return $this->getJsonConverter()->convertFailedToDelete(
(array) (array) \json_decode(
$response->getBody(),
true
)
);
return $this->getJsonConverter()->convertFailedToDelete($this->postJson($url));
}

/**
Expand Down Expand Up @@ -680,7 +662,7 @@ public function getObject(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->cache(
$cacheKey,
Expand Down Expand Up @@ -758,7 +740,7 @@ public function getObjectByPath(
$url->getQuery()->modify([Constants::PARAM_RELATIONSHIPS => (string) $includeRelationships]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->cache(
$cacheKey,
Expand Down Expand Up @@ -808,7 +790,7 @@ public function getProperties(
$url->getQuery()->modify([Constants::PARAM_FILTER => (string) $filter]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

if ($this->getSuccinct()) {
$objectData = $this->getJsonConverter()->convertSuccinctProperties($responseData);
Expand Down Expand Up @@ -866,7 +848,7 @@ public function getRenditions(
$url->getQuery()->modify([Constants::PARAM_MAX_ITEMS => (string) $maxItems]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->getJsonConverter()->convertRenditions($responseData);
}
Expand Down Expand Up @@ -901,8 +883,7 @@ public function moveObject(
]
);

$responseData = (array) \json_decode($this->post($url)->getBody(), true);
$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject($this->postJson($url));

// $objectId was passed by reference. The value is changed here to new object id
$objectId = ($newObject === null) ? null : $newObject->getId();
Expand Down Expand Up @@ -953,13 +934,10 @@ public function setContentStream(
$url->getQuery()->modify([Constants::PARAM_CHANGE_TOKEN => $changeToken]);
}

$responseData = (array) \json_decode(
$this->post($url, ['content' => $contentStream])->getBody(),
true
$newObject = $this->getJsonConverter()->convertObject(
(array) $this->postJson($url, ['content' => $contentStream])
);

$newObject = $this->getJsonConverter()->convertObject($responseData);

// $objectId was passed by reference. The value is changed here to new object id
$objectId = null;
if ($newObject !== null) {
Expand Down Expand Up @@ -1006,8 +984,7 @@ public function updateProperties(
$queryArray = $this->convertPropertiesToQueryArray($properties);
$queryArray[Constants::CONTROL_CMISACTION] = Constants::CMISACTION_UPDATE_PROPERTIES;
$queryArray[Constants::PARAM_SUCCINCT] = $this->getSuccinct() ? 'true' : 'false';
$responseData = (array) \json_decode($this->post($url, $queryArray)->getBody(), true);
$newObject = $this->getJsonConverter()->convertObject($responseData);
$newObject = $this->getJsonConverter()->convertObject((array) $this->postJson($url, $queryArray));

// $objectId was passed by reference. The value is changed here to new object id
$objectId = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Bindings/Browser/RelationshipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getObjectRelationships(
$query->modify([Constants::PARAM_MAX_ITEMS => $maxItems]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->getJsonConverter()->convertObjectList($responseData);
}
Expand Down
12 changes: 4 additions & 8 deletions src/Bindings/Browser/RepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public function createType($repositoryId, TypeDefinitionInterface $type, Extensi
]
);

$responseData = (array) \json_decode($this->post($url)->getBody(), true);

return $this->getJsonConverter()->convertTypeDefinition($responseData);
return $this->getJsonConverter()->convertTypeDefinition($this->postJson($url));
}

/**
Expand Down Expand Up @@ -141,7 +139,7 @@ public function getTypeChildren(
$url->getQuery()->modify([Constants::PARAM_MAX_ITEMS => $maxItems]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->getJsonConverter()->convertTypeChildren($responseData);
}
Expand Down Expand Up @@ -217,7 +215,7 @@ public function getTypeDescendants(
$url->getQuery()->modify([Constants::PARAM_DEPTH => $depth]);
}

$responseData = (array) \json_decode($this->read($url)->getBody(), true);
$responseData = (array) $this->readJson($url);

return $this->getJsonConverter()->convertTypeDescendants($responseData);
}
Expand All @@ -241,8 +239,6 @@ public function updateType($repositoryId, TypeDefinitionInterface $type, Extensi
]
);

$responseData = (array) \json_decode($this->post($url)->getBody(), true);

return $this->getJsonConverter()->convertTypeDefinition($responseData);
return $this->getJsonConverter()->convertTypeDefinition($this->postJson($url));
}
}
Loading

0 comments on commit adc3880

Please sign in to comment.