Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Now check if the getContents is set and use that if so and then use g…
Browse files Browse the repository at this point in the history
…etBody if not and the close function was set to get when it should of been a post request
  • Loading branch information
Chris-Pratt-Clystnet committed Jun 15, 2018
1 parent e4b000f commit 2427162
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function sessionId()
$sessionData = $this->storeSession();
}

if (isset($json->sessionid)) {
if (isset($sessionData->sessionid)) {
$sessionId = $sessionData->sessionid;
} else {
$sessionId = $this->login($sessionData);
Expand Down Expand Up @@ -140,7 +140,7 @@ protected function login($sessionData)
]);

// decode the response
$loginResult = json_decode($response->getBody());
$loginResult = $this->_processResponse($response);
$tryCounter++;
} while (!isset($loginResult->success) && $tryCounter <= $this->maxRetries);

Expand Down Expand Up @@ -236,7 +236,7 @@ protected function getToken()
]);

$tryCounter++;
} while (!isset(json_decode($response->getBody())->success) && $tryCounter <= $this->maxRetries);
} while (!isset($this->_processResponse($response)->success) && $tryCounter <= $this->maxRetries);

if ($tryCounter >= $this->maxRetries) {
throw new VtigerError("Could not complete get token request within ".$this->maxRetries." tries", 6);
Expand Down Expand Up @@ -272,7 +272,7 @@ protected function close($sessionId)
}

// send a request to close current connection
$response = $this->client->request('GET', $this->url, [
$response = $this->client->request('POST', $this->url, [
'query' => [
'operation' => 'logout',
'sessionName' => $sessionId
Expand Down Expand Up @@ -492,8 +492,7 @@ protected function _processResult($response)

$this->_checkResponseStatusCode($response);

// decode the response
$data = json_decode($response->getBody());
$data = $this->_processResponse($response);

if (!isset($data->success)) {
throw new VtigerError("Success property not set on VTiger response", 2);
Expand All @@ -507,6 +506,28 @@ protected function _processResult($response)

}

/**
* Get the json decoded response from either the body or the contents
*
* @param ResponseInterface $response
*
* @return object
*/
protected function _processResponse($response)
{

// decode the response
if (!empty($response->getBody()->getContents())) {
$response->getBody()->rewind();
$data = json_decode($response->getBody()->getContents());
} else {
$data = json_decode($response->getBody());
}

return $data;

}

/**
* Check the response code to make sure it isn't anything but 200
*
Expand Down

0 comments on commit 2427162

Please sign in to comment.