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/) 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..ba920a2 --- /dev/null +++ b/lib/Productsup/Platform/Process.php @@ -0,0 +1,45 @@ +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 + * @param boolean $full + * @return array + */ + public function toArray($full = true) { + $data = array( + 'id' => $this->action_id, + 'action' => $this->action, + ); + + if ($full) { + $data['site_id'] = $this->site_id; + } + + return $data; + } +} 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..438c4e2 --- /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(false); + $request->url .= '/'.$model->site_id; + $data = $this->executeRequest($request); + + if (isset($data['success'])) { + return $data['success']; + } + + return false; + } +}