From 2ae33cabf6ddd7b9e073d92ac1ed8b8869d52f7a Mon Sep 17 00:00:00 2001 From: Marin Hekman Date: Fri, 20 Dec 2019 23:06:17 +0100 Subject: [PATCH 1/7] * added new request event callback to Client, such that a user can influence a new request * added new setters to Request: setConnectTimeoutMs(), setTimeoutMs(), setSslVerifyPeer() and setProxy() --- lib/Client.php | 22 ++++++++++- lib/Request.php | 103 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index d52ad76..b56d03c 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -82,6 +82,13 @@ class Client { */ public $language; + /** + * Language + * + * @var \Closure + */ + private $newRequestEventCallback; + /** * Create a new API client * @@ -104,7 +111,11 @@ public function __construct($accountcode, $accesskey, $secretkey) { * @return Request */ public function newRequest($method, $url) { - return new Request($this, $method, $url); + $request = new Request($this, $method, $url); + if (isset($this->newRequestEventCallback)) { + ($this->newRequestEventCallback)($request); + } + return $request; } /** @@ -115,4 +126,13 @@ public function newRequest($method, $url) { public function setLanguage($lang) { $this->language = $lang; } + + /** + * Set new request event callback + * + * @param \Closure $newRequestEventCallback + */ + public function setNewRequestEventCallback(\Closure $newRequestEventCallback) { + $this->newRequestEventCallback = $newRequestEventCallback; + } } diff --git a/lib/Request.php b/lib/Request.php index dcb91e1..54e1415 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -83,6 +83,34 @@ class Request { */ private $responseHeaders; + /** + * The number of milliseconds to wait while trying to connect + * + * @var int + */ + private $connectTimeoutMs; + + /** + * The maximum number of milliseconds to allow the request to execute + * + * @var int + */ + private $timeoutMs; + + /** + * The proxy to tunnel requests through + * + * @var string + */ + private $proxy; + + /** + * FALSE to stop cURL from verifying the peer's certificate. + * + * @var bool + */ + private $sslVerifyPeer; + /** * Create a new API request. * @@ -149,11 +177,28 @@ private function prepare() { curl_setopt($c, CURLOPT_CUSTOMREQUEST, $this->method); curl_setopt($c, CURLOPT_HEADERFUNCTION, array($this, 'handleHeader')); + if (isset($this->sslVerifyPeer)) { + curl_setopt($c, CURLOPT_SSL_VERIFYPEER, $this->sslVerifyPeer); + } if (isset($_SERVER["TM_TRAVIS"])) { // Travis has a broken CA cert bundle, ignore errors there curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE); } + if (isset($this->connectTimeoutMs)) { + curl_setopt($c, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeoutMs); + } + if (isset($this->timeoutMs)) { + curl_setopt($c, CURLOPT_TIMEOUT_MS, $this->timeoutMs); + } + if (!empty($this->proxy)) { + list($proxy, $proxyPort) = explode(':', $this->proxy); + curl_setopt($c, CURLOPT_PROXY, $proxy); + if (ctype_digit($proxyPort)) { + curl_setopt($c, CURLOPT_PROXY, $proxyPort); + } + } + if ($this->client->language) { $headers[] = "Accept-Language: " . $this->client->language; } @@ -276,9 +321,16 @@ public static function checkError($c, $output, array $headers = array()) { if ($info["http_code"] == 429) { $backoff = isset($headers["retry-after"]) ? $headers["retry-after"] : 0; throw new RateLimitException($backoff); - } else if ($info["http_code"] != 200) { - if ($info["http_code"] == 0) { + } + else { + if ($info["http_code"] == 200) { + // time-outs have a 200 code incl. an error, so we should check it $output = curl_error($c); + + // No error, so return + if (empty($output)) { + return; + } } throw new ClientException($info["http_code"], $output); } @@ -291,4 +343,51 @@ public function handleHeader($curl, $header) { } return strlen($header); } + + /** + * Set number of milliseconds to wait while trying to connect + * Use 0 to wait indefinitely. + * + * @param int $connectTimeoutMs + * + */ + public function setConnectTimeoutMs(int $connectTimeoutMs) + { + $this->connectTimeoutMs = $connectTimeoutMs; + } + + /** + * Set maximum time the request is allowed to take in milliseconds + * Use 0 to wait indefinitely. + * + * @param int $timeoutMs + * + */ + public function setTimeoutMs(int $timeoutMs) + { + $this->timeoutMs = $timeoutMs; + } + + /** + * The proxy to tunnel requests through + * Use : seperator to to add a port + * + * @param string $proxy + * + */ + public function setProxy(string $proxy) + { + $this->proxy = $proxy; + } + + /** + * FALSE to stop cURL from verifying the peer's certificate. + * + * @param bool $sslVerifyPeer + * + */ + public function setSslVerifyPeer(bool $sslVerifyPeer) + { + $this->sslVerifyPeer = $sslVerifyPeer; + } } From fdbfe61a47d41ae2a0e33cf86393f7a00b541fd7 Mon Sep 17 00:00:00 2001 From: Marin Hekman Date: Fri, 20 Dec 2019 23:37:38 +0100 Subject: [PATCH 2/7] fixed whitespace style coding issues --- lib/Request.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Request.php b/lib/Request.php index 54e1415..3660d8b 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -195,7 +195,7 @@ private function prepare() { list($proxy, $proxyPort) = explode(':', $this->proxy); curl_setopt($c, CURLOPT_PROXY, $proxy); if (ctype_digit($proxyPort)) { - curl_setopt($c, CURLOPT_PROXY, $proxyPort); + curl_setopt($c, CURLOPT_PROXY, $proxyPort); } } @@ -323,14 +323,14 @@ public static function checkError($c, $output, array $headers = array()) { throw new RateLimitException($backoff); } else { - if ($info["http_code"] == 200) { + if ($info["http_code"] == 200) { // time-outs have a 200 code incl. an error, so we should check it $output = curl_error($c); - // No error, so return - if (empty($output)) { - return; - } + // No error, so return + if (empty($output)) { + return; + } } throw new ClientException($info["http_code"], $output); } From b0ae10e79405bcbc23ace45aed92993645d47cb8 Mon Sep 17 00:00:00 2001 From: Marin Hekman Date: Fri, 20 Dec 2019 23:48:35 +0100 Subject: [PATCH 3/7] * fixed phpdoc description for variable $newRequestEventCallback * fixed more code styling issue --- lib/Client.php | 2 +- lib/Request.php | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index b56d03c..53a96f1 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -83,7 +83,7 @@ class Client { public $language; /** - * Language + * Callback called on every new request created * * @var \Closure */ diff --git a/lib/Request.php b/lib/Request.php index 3660d8b..8e4a319 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -351,8 +351,7 @@ public function handleHeader($curl, $header) { * @param int $connectTimeoutMs * */ - public function setConnectTimeoutMs(int $connectTimeoutMs) - { + public function setConnectTimeoutMs(int $connectTimeoutMs) { $this->connectTimeoutMs = $connectTimeoutMs; } @@ -363,8 +362,7 @@ public function setConnectTimeoutMs(int $connectTimeoutMs) * @param int $timeoutMs * */ - public function setTimeoutMs(int $timeoutMs) - { + public function setTimeoutMs(int $timeoutMs) { $this->timeoutMs = $timeoutMs; } @@ -375,8 +373,7 @@ public function setTimeoutMs(int $timeoutMs) * @param string $proxy * */ - public function setProxy(string $proxy) - { + public function setProxy(string $proxy) { $this->proxy = $proxy; } @@ -386,8 +383,7 @@ public function setProxy(string $proxy) * @param bool $sslVerifyPeer * */ - public function setSslVerifyPeer(bool $sslVerifyPeer) - { + public function setSslVerifyPeer(bool $sslVerifyPeer) { $this->sslVerifyPeer = $sslVerifyPeer; } } From 2248e74bb4bb7f0e131f6b651b09c5382561e371 Mon Sep 17 00:00:00 2001 From: Marin Hekman Date: Sat, 21 Dec 2019 00:00:02 +0100 Subject: [PATCH 4/7] proxy port was set on CURLOPT_PROXY instead of CURLOPT_PROXYPORT --- lib/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Request.php b/lib/Request.php index 8e4a319..d49500a 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -195,7 +195,7 @@ private function prepare() { list($proxy, $proxyPort) = explode(':', $this->proxy); curl_setopt($c, CURLOPT_PROXY, $proxy); if (ctype_digit($proxyPort)) { - curl_setopt($c, CURLOPT_PROXY, $proxyPort); + curl_setopt($c, CURLOPT_PROXYPORT, $proxyPort); } } From 3285b97428ff5c916ee28f3df33d2616f48ce9f8 Mon Sep 17 00:00:00 2001 From: Rik van der Kemp Date: Mon, 24 May 2021 17:27:07 +0200 Subject: [PATCH 5/7] fix: add missing propery isarchived to product property --- lib/Model/ProductProperty.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Model/ProductProperty.php b/lib/Model/ProductProperty.php index 0ae662c..5a60e44 100644 --- a/lib/Model/ProductProperty.php +++ b/lib/Model/ProductProperty.php @@ -79,6 +79,11 @@ public function __construct(array $data = array()) { */ public $values; + /** + * @var string + */ + public $isarchived; + /** * Unpack ProductProperty from JSON. * @@ -96,6 +101,7 @@ public static function fromJson($obj) { "description" => isset($obj->description) ? $obj->description : null, "key" => isset($obj->key) ? $obj->key : null, "values" => isset($obj->values) ? Json::unpackArray("KeyValueItem", $obj->values) : null, + "isarchived" => $obj->isarchived ?? null )); } @@ -118,6 +124,9 @@ public function jsonSerialize() { if (!is_null($this->values)) { $result["values"] = $this->values; } + if (!is_null($this->isarchived)) { + $result["isarchived"] = $this->isarchived; + } return $result; } From 4859a12fb461aa42e66831a23997ea2ceafbfc7b Mon Sep 17 00:00:00 2001 From: kov-lucas Date: Thu, 28 Oct 2021 13:08:25 +0200 Subject: [PATCH 6/7] allow null for birthdate --- lib/Model/Contact.php | 2 +- lib/Request.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Model/Contact.php b/lib/Model/Contact.php index 12f2535..fe9621a 100644 --- a/lib/Model/Contact.php +++ b/lib/Model/Contact.php @@ -301,7 +301,7 @@ public function jsonSerialize() { $result["addresses"] = $this->addresses; } if (!is_null($this->birthdate)) { - $result["birthdate"] = Json::packTimestamp($this->birthdate); + $result["birthdate"] = !empty($this->birthdate) ? Json::packTimestamp($this->birthdate) : null; } if (!is_null($this->company)) { $result["company"] = strval($this->company); diff --git a/lib/Request.php b/lib/Request.php index dcb91e1..8c21bab 100644 --- a/lib/Request.php +++ b/lib/Request.php @@ -163,9 +163,7 @@ private function prepare() { $headers[] = "Expect:"; // issue with cURL when doing big size POSTs https://stackoverflow.com/questions/14158675/how-can-i-stop-curl-from-using-100-continue if ($this->bodycontenttype == "json") { foreach ($this->body as $key => $value) { - if (!is_null($value)) { - $body[$key] = $value; - } + $body[$key] = $value; } $body = json_encode($body); $headers[] = "Content-Type: application/json"; From 8bec33ea3b6c7d0e754a6616f33e74d80934004d Mon Sep 17 00:00:00 2001 From: manjula-pep Date: Tue, 8 Feb 2022 15:39:08 +0100 Subject: [PATCH 7/7] added totalmaxtickets to the result --- lib/Model/Event.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Model/Event.php b/lib/Model/Event.php index 847ae1a..e24ed15 100644 --- a/lib/Model/Event.php +++ b/lib/Model/Event.php @@ -487,6 +487,7 @@ public static function fromJson($obj) { "webremark" => isset($obj->webremark) ? $obj->webremark : null, "createdts" => isset($obj->createdts) ? Json::unpackTimestamp($obj->createdts) : null, "lastupdatets" => isset($obj->lastupdatets) ? Json::unpackTimestamp($obj->lastupdatets) : null, + "totalmaxtickets" => isset($obj->totalmaxtickets) ? $obj->totalmaxtickets : null, )); $result->custom_fields = array();