From aa83a43116f3db80b95e89cb65a759af7aabc0b3 Mon Sep 17 00:00:00 2001 From: Mark Hameetman Date: Wed, 15 Jan 2020 08:10:50 +0100 Subject: [PATCH] First setup, retrieve and submit envelopes --- .gitignore | 2 + composer.json | 21 +++++++ src/Client.php | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Client.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f4acd3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..319c9ad --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "bureaupartners/multipost-api", + "description": "PHP API Client for Multi-Post", + "require": { + "php": ">=7.0", + "guzzlehttp/guzzle": "~6.0" + }, + "license": "MIT", + "authors": [{ + "name": "Mark Hameetman", + "email": "mark@bureau.partners" + }], + "autoload": { + "psr-4": { + "BureauPartners\\MultiPost\\": "src" + } + }, + "require-dev": { + "phpunit/phpunit": "^8" + } +} \ No newline at end of file diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..4d80e38 --- /dev/null +++ b/src/Client.php @@ -0,0 +1,161 @@ +hostname = $hostname; + } + if ($this->bearer_token == null) { + $this->requestBearerToken($email, $password); + } + $this->version = phpversion(); + $this->os = PHP_OS; + } + + public function isConnected() + { + if ($this->bearer_token !== null) { + return true; + } else { + return false; + } + } + + public function requestBearerToken($email, $password) + { + $body = [ + 'email' => $email, + 'password' => $password, + ]; + $response = $this->request(Client::HTTP_POST, '/login', $body); + if (!is_array($response)) { + return false; + } + $this->bearer_token = $response['access_token']; + return true; + } + + protected function getClient() + { + if (!$this->client) { + $this->client = new GuzzleClient([ + RequestOptions::VERIFY => true, + RequestOptions::TIMEOUT => 30, + ]); + } + return $this->client; + } + protected function request($method = null, $path = null, $body = null) + { + $client = $this->getClient(); + $options = [ + RequestOptions::HTTP_ERRORS => false, + RequestOptions::HEADERS => [ + 'User-Agent' => 'MultiPost Client (PHP Version: ' . $this->version . ', OS: ' . $this->os . ')', + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer ' . $this->bearer_token, + ], + ]; + + if (!empty($body)) { + $cleanParams = array_filter($body, function ($value) { + return $value !== null; + }); + switch ($method) { + case Client::HTTP_GET: + case Client::HTTP_HEAD: + case Client::HTTP_DELETE: + case Client::HTTP_OPTIONS: + $options[RequestOptions::QUERY] = $cleanParams; + break; + case Client::HTTP_PUT: + case Client::HTTP_POST: + case Client::HTTP_PATCH: + $options[RequestOptions::JSON] = $cleanParams; + break; + } + } + $response = $client->request($method, $this->hostname . $path, $options); + $response_code = $response->getStatusCode(); + if ($response_code == 200) { + return json_decode($response->getBody(), true); + } else { + return $response_code; + } + } + public function getEnvelopes($company_id) + { + return $this->request(Client::HTTP_GET, '/company/' . $company_id . '/envelopes'); + } + + public function createEnvelope($company_id, $name, $description, $envelope_details = Client::ENVELOPE_C5, $design_file_contents = null) + { + if (is_array($envelope_details)) { + $envelope = [ + 'name' => $name, + 'description' => $description, + ]; + $envelope = array_merge($envelope, $envelope_details); + } elseif ($envelope_details === Client::ENVELOPE_C5) { + $envelope = [ + 'name' => $name, + 'description' => $description, + 'width' => 229, + 'height' => 162, + 'weight' => 8, + 'max_pages' => 9, + 'window_width' => 110, + 'window_height' => 40, + 'window_left_margin' => 20, + 'window_top_margin' => 50, + ]; + } elseif ($envelope_details === Client::ENVELOPE_C4) { + $envelope = [ + 'name' => $name, + 'description' => $description, + 'width' => 229, + 'height' => 324, + 'weight' => 8, + 'max_pages' => 30, + 'window_width' => 110, + 'window_height' => 40, + 'window_left_margin' => 20, + 'window_top_margin' => 50, + ]; + } else { + return false; + } + if ($design_file_contents !== null) { + $envelope_design = [ + 'design_file_contents' => base64_encode($design_file_contents), + ]; + $envelope = array_merge($envelope, $envelope_design); + } + return $this->request(Client::HTTP_POST, '/company/' . $company_id . '/envelopes', $envelope); + } + +}