Skip to content

Commit

Permalink
Merge pull request #157 from avadev/21.12.0
Browse files Browse the repository at this point in the history
Update for 21.12.0
  • Loading branch information
svc-developer authored Dec 15, 2021
2 parents 3567dd5 + 7674981 commit 7da6df8
Show file tree
Hide file tree
Showing 4 changed files with 1,586 additions and 766 deletions.
35 changes: 25 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public function getClient()
* @param string $apiUrl The relative path of the API on the server
* @param string $verb The HTTP verb being used in this request
* @param string $guzzleParams The Guzzle parameters for this request, including query string and body parameters
*
* @param string $apiversion API Version of the request
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/
protected function restCall($apiUrl, $verb, $guzzleParams)
protected function restCall($apiUrl, $verb, $guzzleParams, $apiversion='')
{
// Set authentication on the parameters
if (count($this->auth) == 2) {
Expand All @@ -168,16 +168,16 @@ protected function restCall($apiUrl, $verb, $guzzleParams)
}
$guzzleParams['headers'] = [
'Accept' => 'application/json',
'X-Avalara-Client' => "{$this->appName}; {$this->appVersion}; PhpRestClient; 21.10.0; {$this->machineName}"
'X-Avalara-Client' => "{$this->appName}; {$this->appVersion}; PhpRestClient; {$apiversion}; {$this->machineName}"
];
} else {
$guzzleParams['headers'] = [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$this->auth[0],
'X-Avalara-Client' => "{$this->appName}; {$this->appVersion}; PhpRestClient; 21.10.0; {$this->machineName}"
'X-Avalara-Client' => "{$this->appName}; {$this->appVersion}; PhpRestClient; {$apiversion}; {$this->machineName}"
];
}

// Check the client config, if set - update guzzleParams['timeout']
if (isset($this->client->getConfig()['timeout']) ) {
$guzzleParams['timeout'] = $this->client->getConfig()['timeout'];
Expand All @@ -186,7 +186,7 @@ protected function restCall($apiUrl, $verb, $guzzleParams)
if (!isset($guzzleParams['timeout'])) {
$guzzleParams['timeout'] = 1200;
}

// Check the client config, if set - update guzzleParams['connect_timeout']
if (isset($this->client->getConfig()['connect_timeout'])) {
$guzzleParams['connect_timeout'] = $this->client->getConfig()['connect_timeout'];
Expand All @@ -199,14 +199,29 @@ protected function restCall($apiUrl, $verb, $guzzleParams)
// Contact the server
try {
$response = $this->client->request($verb, $apiUrl, $guzzleParams);

$body = $response->getBody();

$length = 0;

$contentLength =$response->getHeader('Content-Length');
if ($contentLength!=null)
{
$length=$contentLength[0];
}
$code=$response->getStatusCode();
$contentTypes = $response->getHeader('Content-Type');

if ( in_array ("application/json",$contentTypes))
{
if ($length ==0 and intdiv($code , 100) ==2 ){
return null;
}
}
$JsonBody = json_decode($body);
if (is_null($JsonBody)) {
if (json_last_error() === JSON_ERROR_SYNTAX) {
throw new \Exception('The response is in unexpected format. The response is: ' . $JsonBody);
}
if (json_last_error() === JSON_ERROR_SYNTAX) {
throw new \Exception('The response is in unexpected format. The response is: ' . $JsonBody);
}
return $body;
} else {
return $JsonBody;
Expand Down
Loading

0 comments on commit 7da6df8

Please sign in to comment.