Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Api.php #1

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "hadian90/freshdesk-php-sdk",
"name": "runcloudio/freshdesk-php-sdk",
"description": "PHP SDK for the Freshdesk API (v2)",
"type": "library",
"homepage": "https://github.com/hadian90/freshdesk-php-sdk",
"homepage": "https://github.com/RunCloudIO/freshdesk-php-sdk",
"keywords": [
"freshdesk",
"customer service",
Expand All @@ -12,6 +12,11 @@
],
"license": "MIT",
"authors": [
{
"name": "Ahmad Fikrizaman",
"email": "[email protected]",
"homepage": "https://runcloud.io"
},
{
"name": "Abdul Hadi",
"email": "[email protected]",
Expand All @@ -25,7 +30,7 @@
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": ">=4.0,<7.0"
"guzzlehttp/guzzle": "^7.0.1"
},
"require-dev": {
"phpunit/phpunit": "4.8.*",
Expand Down
41 changes: 22 additions & 19 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,42 @@ class Api
* @var Client
*/
protected $client;

/**
* @internal
* @var string
*/
private $baseUrl;

/**
* @api
* @var string
*/
public $headers;


/**
* @api
* @var string
*/
public $result;

/**
* Constructs a new api instance
*
* @api
* @param string $apiKey
* @param string $domain
* @param array $options
* @throws Exceptions\InvalidConfigurationException
*/
public function __construct($apiKey, $domain)
public function __construct($apiKey, $domain, $options = [])
{
$this->validateConstructorArgs($apiKey, $domain);

$this->baseUrl = sprintf('https://%s.freshdesk.com/api/v2', $domain);

$this->client = new Client(
[
'auth' => [$apiKey, 'X'],
]
);
$this->client = new Client(array_merge($options, ['auth' => [$apiKey, 'X']]));

$this->setupResources();
}
Expand Down Expand Up @@ -300,18 +310,11 @@ private function performRequest($method, $url, $options)
{

try {
switch ($method) {
case 'GET':
return json_decode($this->client->get($url, $options)->getBody(), true);
case 'POST':
return json_decode($this->client->post($url, $options)->getBody(), true);
case 'PUT':
return json_decode($this->client->put($url, $options)->getBody(), true);
case 'DELETE':
return json_decode($this->client->delete($url, $options)->getBody(), true);
default:
return null;
}
$request = $this->client->request($method, $url, $options);

$this->headers = $request->getHeaders();
$this->results = json_decode($request->getBody(), true);
return $this;
} catch (RequestException $e) {
throw ApiException::create($e);
}
Expand Down
44 changes: 44 additions & 0 deletions src/Resources/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,48 @@ public function createWithAttachment(array $data)
{
return $this->api()->requestMultipart('POST', $this->endpoint(), $data);
}

/**
* Create a satisfaction rating
*
* @param int $id The ticket id
* @param array $data
* @return mixed|null
* @throws \Freshdesk\Exceptions\AccessDeniedException
* @throws \Freshdesk\Exceptions\ApiException
* @throws \Freshdesk\Exceptions\AuthenticationException
* @throws \Freshdesk\Exceptions\ConflictingStateException
* @throws \Freshdesk\Exceptions\NotFoundException
* @throws \Freshdesk\Exceptions\RateLimitExceededException
* @throws \Freshdesk\Exceptions\UnsupportedContentTypeException
* @throws \Freshdesk\Exceptions\MethodNotAllowedException
* @throws \Freshdesk\Exceptions\UnsupportedAcceptHeaderException
* @throws \Freshdesk\Exceptions\ValidationException
*/
public function survey($id, array $query = null)
{
return $this->api()->request('POST', $this->endpoint($id.'/satisfaction_ratings'), $query);
}

/**
* List all satisfaction ratings on ticket
*
* @param int $id The ticket id
* @param array $data
* @return mixed|null
* @throws \Freshdesk\Exceptions\AccessDeniedException
* @throws \Freshdesk\Exceptions\ApiException
* @throws \Freshdesk\Exceptions\AuthenticationException
* @throws \Freshdesk\Exceptions\ConflictingStateException
* @throws \Freshdesk\Exceptions\NotFoundException
* @throws \Freshdesk\Exceptions\RateLimitExceededException
* @throws \Freshdesk\Exceptions\UnsupportedContentTypeException
* @throws \Freshdesk\Exceptions\MethodNotAllowedException
* @throws \Freshdesk\Exceptions\UnsupportedAcceptHeaderException
* @throws \Freshdesk\Exceptions\ValidationException
*/
public function listSurvey($id, array $query = null)
{
return $this->api()->request('GET', $this->endpoint($id.'/satisfaction_ratings'), $query);
}
}