Skip to content

Commit

Permalink
Retry getobject (#300)
Browse files Browse the repository at this point in the history
* Support automatic retries with GetObject when soft errors are returned by the server

* Detect XML properly and don't get tripped up with other content types

like application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • Loading branch information
troydavisson committed Feb 16, 2022
1 parent 4497a35 commit 83ec09c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parsers/GetObject/Single.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function isError(Response $response)
}

$content_type = $response->getHeader('Content-Type');
if ($content_type and strpos($content_type, 'xml') !== false) {
if ($content_type and strpos($content_type, 'text/xml') !== false) {
$xml = $response->xml();

if (isset($xml['ReplyCode']) and $xml['ReplyCode'] != 0) {
Expand Down
30 changes: 30 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,36 @@ protected function request($capability, $options = [], $is_retry = false)
}
}

if ($this->getConfiguration()->readOption('getobject_auto_retry') and $capability == 'GetObject') {
if (stripos($response->getHeader('Content-Type'), 'multipart') !== false) {
$parser = $this->grab(Strategy::PARSER_OBJECT_MULTIPLE);
$collection = $parser->parse($response);
} else {
$collection = new Collection;
$parser = $this->grab(Strategy::PARSER_OBJECT_SINGLE);
$object = $parser->parse($response);
$collection->push($object);
}

/** @var BaseObject[] $collection */
foreach ($collection as $object) {
if ($object->isError() and $object->getError()->getCode() == '20037') {
if ($is_retry) {
// this attempt was already a retry, so let's stop here
$this->debug("Request retry failed. Won't retry again");
// let this error fall through to the more generic handling below
} else {
$this->debug("RETS 20037 re-auth requested");
$this->debug("Logging in again and retrying request");
// see if logging in again and retrying the request works
$this->Login();

return $this->request($capability, $options, true);
}
}
}
}

return $response;
}

Expand Down

0 comments on commit 83ec09c

Please sign in to comment.