Skip to content

Commit

Permalink
Merge pull request #3 from productsupcom/CPU-899
Browse files Browse the repository at this point in the history
WIP CPU-899 - JSON Testing
  • Loading branch information
Werner Spiegel committed Apr 1, 2016
2 parents 1429600 + a37a905 commit baca642
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
test/
examples/customers/*
composer.lock
examples/customers/*
46 changes: 45 additions & 1 deletion lib/Productsup/Service/ProductData.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function setImportType($type) {
*/
public function commit() {
$this->checkSubmit(1); // send all unsent products

if(!$this->didSubmit) {
throw new Exceptions\ClientException('no data submitted yet');
}

$request = $this->getRequest();
$request->method = Request::METHOD_POST;
$request->postBody = array(
Expand Down Expand Up @@ -227,12 +232,51 @@ private function _submit() {
$request->method = Request::METHOD_POST;
$request->url .= '/upload';
$request->postBody = $this->_productData;
$response = $this->getIoHandler()->executeRequest($request);

try {
$response = $this->getIoHandler()->executeRequest($request);
} catch (\Exception $e) {
$found = $this->testJson($this->_productData);

if ($found > 0) {
$this->_submitLog[] = sprintf(
'%s: %u products with malformed json. removing affected products and retrying batch: %s',
date('Y-m-d H:i:s'),
$found,
$this->_batchId
);

$this->_submit();
} else {
throw $e;
}
}
$this->_productData = array();
$this->logResponse($response);
return $response->getData();
}

private function testJson(array &$a)
{
$found = 0;

foreach ($a as $key => $row) {
$j = json_encode($row);
if (json_last_error()) {
unset($a[$key]);
$found++;
continue;
}
json_decode($j);
if (json_last_error()) {
unset($a[$key]);
$found++;
}
}

return $found;
}

/**
* convert api response to a log message
* @param Response $response
Expand Down

0 comments on commit baca642

Please sign in to comment.