From ef5827ae21b0c3c74a12eac4b8e1c5f3f43d0bce Mon Sep 17 00:00:00 2001 From: Mischa Gorinskat Date: Wed, 13 Sep 2017 12:56:45 +0200 Subject: [PATCH 1/4] Adds Process object --- examples/Service/Process.php | 55 ++++++++++++++++++++ lib/Productsup/Platform/Process.php | 38 ++++++++++++++ lib/Productsup/Platform/Site/Reference.php | 8 +++ lib/Productsup/Service/Process.php | 58 ++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 examples/Service/Process.php create mode 100644 lib/Productsup/Platform/Process.php create mode 100644 lib/Productsup/Service/Process.php diff --git a/examples/Service/Process.php b/examples/Service/Process.php new file mode 100644 index 0000000..dd14567 --- /dev/null +++ b/examples/Service/Process.php @@ -0,0 +1,55 @@ +id = 4558; +$Client->secret = 'b011f60ae834da8a8054d149b5ed5727'; + +/** + * Initialize the Sites Service where you can + * + * Create a new site (Sites->insert()) + * Delete a site (Sites->delete()) + * Get a list of sites (Sites->get()) + */ +$processCall = new Productsup\Service\Process($Client); + +/** + * to get a certain site you may pass a reference, + * how references are created is explained later + */ +$reference = new \Productsup\Platform\Site\Reference(); +$reference->setKey($reference::REFERENCE_SITE); +$reference->setValue(434456); // Site ID + +/** + * Triggering an action + * + * Valid actions are: + * - import: triggers an import + * - export-all: triggers all exports and channels + * - export: triggers an export (old style), action_id parameter with export id is required + * - channel: triggers a channel (new style), action_id parameter with channel is required + * - all: triggers an import and all exports and channels + */ +$processModel = new Process(); +$processModel->action = 'channel'; +$processModel->action_id = 51395; +$processModel->addReference($reference); +// This also works, but using a reference is preferred +// $processModel->site_id = $reference->getValue(); + +// The results reveals whether jenkins accepted the job or, but not does not say +// anything about if the job is already started or queued, in most cases +// it will run immediately +$result = $processCall->post($processModel); + +var_dump($result); diff --git a/lib/Productsup/Platform/Process.php b/lib/Productsup/Platform/Process.php new file mode 100644 index 0000000..5ed797e --- /dev/null +++ b/lib/Productsup/Platform/Process.php @@ -0,0 +1,38 @@ +getKey() != Reference::REFERENCE_SITE) { + throw new ClientException('Process only accepts site as a reference.'); + } + + $this->site_id = $reference->getValue(); + parent::addReference($reference); + } + + /** + * cast data to an array + * @return array + */ + public function toArray() { + return array( + 'id' => $this->action_id, + 'action' => $this->action, + 'site_id' => $this->site_id, + ); + } +} diff --git a/lib/Productsup/Platform/Site/Reference.php b/lib/Productsup/Platform/Site/Reference.php index c120824..9e6ac5e 100644 --- a/lib/Productsup/Platform/Site/Reference.php +++ b/lib/Productsup/Platform/Site/Reference.php @@ -66,4 +66,12 @@ public function setValue($value) public function getValue() { return $this->_value; } + + /** + * get the defined key + * @return string|null + */ + public function getKey() { + return $this->_key; + } } diff --git a/lib/Productsup/Service/Process.php b/lib/Productsup/Service/Process.php new file mode 100644 index 0000000..3be224a --- /dev/null +++ b/lib/Productsup/Service/Process.php @@ -0,0 +1,58 @@ +site_id) { + throw new ClientException('A site id is required for a process.'); + } + if (!in_array($model->action, $this->validActions)) { + throw new ClientException(sprintf( + 'Only the following actions are allowed: %s', + implode(', ', $this->validActions) + )); + } + if (in_array($model->action, $this->actionsRequiringId) && !$model->action_id) { + throw new ClientException('An export or channel id needs to be set for this action.'); + } + + $request = $this->getRequest(); + $request->method = Request::METHOD_POST; + $request->postBody = $model->toArray(); + $request->url .= '/'.$model->site_id; + $data = $this->executeRequest($request); + + if (isset($data['success'])) { + return $data['success']; + } + + return false; + } +} From a825fbe4d7dc5f3454e1edf025f00f7699a15eb5 Mon Sep 17 00:00:00 2001 From: Mischa Gorinskat Date: Wed, 13 Sep 2017 15:33:45 +0200 Subject: [PATCH 2/4] Adds documentation link to README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c11b8ea..7c54b22 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ require_once(__DIR__.'/Platform-API-Client/autoload.php'); * [PHP cURL](http://php.net/manual/en/curl.installation.php) # Basic usage -see the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service). +See the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service). + +# API Documentation + +See [the documentation page](http://api-docs.productsup.io/) From 39f7bdc0aef15e2d0dba0e7f6e3f0ea29365abf2 Mon Sep 17 00:00:00 2001 From: Mischa Gorinskat Date: Wed, 13 Sep 2017 15:34:09 +0200 Subject: [PATCH 3/4] ACC-1167: Makes new code PHP 5.3 compatible --- lib/Productsup/Service/Process.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Productsup/Service/Process.php b/lib/Productsup/Service/Process.php index 3be224a..297b8a7 100644 --- a/lib/Productsup/Service/Process.php +++ b/lib/Productsup/Service/Process.php @@ -9,10 +9,10 @@ class Process extends Service { protected $serviceName = 'process'; - protected $validActions = [ + protected $validActions = array( 'import', 'export', 'channel', 'all', 'export-all' - ]; - protected $actionsRequiringId = ['export', 'channel']; + ); + protected $actionsRequiringId = array('export', 'channel'); // Not applicable to process protected function getDataModel() From 52fd0a5112c1b6d215a83bdacaf85e05d6a899e2 Mon Sep 17 00:00:00 2001 From: Mischa Gorinskat Date: Wed, 13 Sep 2017 15:39:21 +0200 Subject: [PATCH 4/4] Minor fixes to Process Model --- lib/Productsup/Platform/Process.php | 13 ++++++++++--- lib/Productsup/Service/Process.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Productsup/Platform/Process.php b/lib/Productsup/Platform/Process.php index 5ed797e..ba920a2 100644 --- a/lib/Productsup/Platform/Process.php +++ b/lib/Productsup/Platform/Process.php @@ -14,6 +14,7 @@ class Process extends DataModel { * adds a reference to a site that can later be used as an identifier * note: this is only possible when creating a site or project * @param Reference $reference + * @throws ClientException When adding non site reference */ public function addReference(Reference $reference) { if ($reference->getKey() != Reference::REFERENCE_SITE) { @@ -26,13 +27,19 @@ public function addReference(Reference $reference) { /** * cast data to an array + * @param boolean $full * @return array */ - public function toArray() { - return array( + public function toArray($full = true) { + $data = array( 'id' => $this->action_id, 'action' => $this->action, - 'site_id' => $this->site_id, ); + + if ($full) { + $data['site_id'] = $this->site_id; + } + + return $data; } } diff --git a/lib/Productsup/Service/Process.php b/lib/Productsup/Service/Process.php index 297b8a7..438c4e2 100644 --- a/lib/Productsup/Service/Process.php +++ b/lib/Productsup/Service/Process.php @@ -45,7 +45,7 @@ public function post(ProcessModel $model) { $request = $this->getRequest(); $request->method = Request::METHOD_POST; - $request->postBody = $model->toArray(); + $request->postBody = $model->toArray(false); $request->url .= '/'.$model->site_id; $data = $this->executeRequest($request);