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

Commit

Permalink
Some more code cleanup of varible names, put the while loops back in …
Browse files Browse the repository at this point in the history
…for the login and get token as these are still faling on certain VTgier crms and put in some more error checking for when the loops dont get a response after the max tries
  • Loading branch information
Chris-Pratt-Clystnet committed Jun 15, 2018
1 parent efb3701 commit c35e603
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
'accesskey' => '',
'sessiondriver' => 'file', //reddis or file
'persistconnection' => true,
'max_retries' => 10
];
132 changes: 77 additions & 55 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Vtiger
/** @var Client */
protected $client;

/** @var int */
protected $maxRetries;

/**
* Vtiger constructor.
*/
Expand All @@ -35,6 +38,7 @@ public function __construct()
$this->accesskey = Config::get('vtiger.accesskey');
$this->sessionDriver = Config::get('vtiger.sessiondriver');
$this->persistConnection = Config::get('vtiger.persistconnection');
$this->maxRetries = Config::get('vtiger.max_retries');

$this->client = new Client(['http_errors' => false, 'verify' => false]); //GuzzleHttp\Client
}
Expand Down Expand Up @@ -65,7 +69,7 @@ public function connection($url, $username, $accesskey)
* @throws VtigerError
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function sessionid()
protected function sessionId()
{

// Check the session file exists
Expand All @@ -85,22 +89,22 @@ protected function sessionid()
if (isset($sessionData)) {
if (isset($sessionData) && property_exists($sessionData, 'expireTime') && property_exists($sessionData, 'token')) {
if ($sessionData->expireTime < time() || empty($sessionData->token)) {
$sessionData = $this->storesession();
$sessionData = $this->storeSession();
}
} else {
$sessionData = $this->storesession();
$sessionData = $this->storeSession();
}
} else {
$sessionData = $this->storesession();
$sessionData = $this->storeSession();
}

if (isset($json->sessionid)) {
$sessionid = $json->sessionid;
$sessionId = $sessionData->sessionid;
} else {
$sessionid = $this->login($sessionData);
$sessionId = $this->login($sessionData);
}

return $sessionid;
return $sessionId;

}

Expand All @@ -117,22 +121,31 @@ protected function sessionid()
protected function login($sessionData)
{

$sessionId = null;
$token = $sessionData->token;

// Create unique key using combination of challengetoken and accesskey
$generatedkey = md5($token . $this->accesskey);

// login using username and accesskey
$response = $this->client->request('POST', $this->url, [
'form_params' => [
'operation' => 'login',
'username' => $this->username,
'accessKey' => $generatedkey
]
]);

// decode the response
$loginResult = json_decode($response->getBody()->getContents());
$generatedKey = md5($token . $this->accesskey);

$tryCounter = 1;
do {
// login using username and accesskey
$response = $this->client->request('POST', $this->url, [
'form_params' => [
'operation' => 'login',
'username' => $this->username,
'accessKey' => $generatedKey
]
]);

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

if ($tryCounter >= $this->maxRetries) {
throw new VtigerError("Could not complete login request within ".$this->maxRetries." tries", 5);
}

// If api login failed
if ($response->getStatusCode() !== 200 || !$loginResult->success) {
Expand All @@ -153,28 +166,28 @@ protected function login($sessionData)
}
} else {
// login ok so get sessionid and update our session
$sessionid = $loginResult->result->sessionName;
$sessionId = $loginResult->result->sessionName;

switch ($this->sessionDriver) {
case "file":
if (Storage::disk('local')->exists('session.json')) {
$json = json_decode(Storage::disk('local')->get('session.json'));
$json->sessionid = $sessionid;
$json->sessionid = $sessionId;
Storage::disk('local')->put('session.json', json_encode($json));
}
break;
case "redis":
Redis::incr('loggedin');
$json = json_decode(Redis::get('clystnet_vtiger'));
$json->sessionid = $sessionid;
$json->sessionid = $sessionId;
Redis::set('clystnet_vtiger', json_encode($json));
break;
default:
throw new VtigerError("Session driver type of ".$this->sessionDriver." is not supported", 4);
}
}

return $sessionid;
return $sessionId;

}

Expand All @@ -183,11 +196,12 @@ protected function login($sessionData)
*
* @return object
* @throws GuzzleException
* @throws VtigerError
*/
protected function storesession()
protected function storeSession()
{

$updated = $this->gettoken();
$updated = $this->getToken();

$output = (object)$updated;
if ($this->sessionDriver == 'file') {
Expand All @@ -207,16 +221,24 @@ protected function storesession()
* @throws GuzzleException
* @throws VtigerError
*/
protected function gettoken()
protected function getToken()
{

// perform API GET request
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'getchallenge',
'username' => $this->username
]
]);
$tryCounter = 1;
do {
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'getchallenge',
'username' => $this->username
]
]);
$processedResponse = json_decode($response->getBody()->getContents());
} while (!isset($processedResponse->success) && $tryCounter <= $this->maxRetries);

if ($tryCounter >= $this->maxRetries) {
throw new VtigerError("Could not complete get token request within ".$this->maxRetries." tries", 6);
}

// decode the response
$challenge = $this->_processResult($response);
Expand All @@ -234,13 +256,13 @@ protected function gettoken()
/**
* Logout from the VTiger API
*
* @param string $sessionid
* @param string $sessionId
*
* @return object
* @throws GuzzleException
* @throws VtigerError
*/
protected function close($sessionid)
protected function close($sessionId)
{

if ($this->persistConnection) {
Expand All @@ -251,7 +273,7 @@ protected function close($sessionid)
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'logout',
'sessionName' => $sessionid
'sessionName' => $sessionId
]
]);

Expand All @@ -272,18 +294,18 @@ protected function close($sessionid)
public function query($query)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request using a database query to get back any matching records
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'query',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'query' => $query
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand All @@ -303,18 +325,18 @@ public function query($query)
public function retrieve($id)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request to retrieve a record
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'retrieve',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'id' => $id
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand All @@ -341,22 +363,22 @@ public function retrieve($id)
* @throws VtigerError
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function create(string $elem, string $data)
public function create($elem, $data)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request to create a record
$response = $this->client->request('POST', $this->url, [
'form_params' => [
'operation' => 'create',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'element' => $data,
'elementType' => $elem
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand All @@ -377,18 +399,18 @@ public function create(string $elem, string $data)
public function update($object)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request to update a record
$response = $this->client->request('POST', $this->url, [
'form_params' => [
'operation' => 'update',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'element' => json_encode($object),
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand All @@ -408,18 +430,18 @@ public function update($object)
public function delete($id)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request to delete a record
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'delete',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'id' => $id
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand All @@ -438,18 +460,18 @@ public function delete($id)
public function describe($elementType)
{

$sessionid = self::sessionid();
$sessionId = $this->sessionId();

// send a request to describe a module (which returns a list of available fields) for a Vtiger module
$response = $this->client->request('GET', $this->url, [
'query' => [
'operation' => 'describe',
'sessionName' => $sessionid,
'sessionName' => $sessionId,
'elementType' => $elementType
]
]);

self::close($sessionid);
$this->close($sessionId);

return $this->_processResult($response);

Expand Down

0 comments on commit c35e603

Please sign in to comment.