-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First setup, retrieve and submit envelopes
- Loading branch information
1 parent
7b862f2
commit aa83a43
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]" | ||
}], | ||
"autoload": { | ||
"psr-4": { | ||
"BureauPartners\\MultiPost\\": "src" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
namespace BureauPartners\MultiPost; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\RequestOptions; | ||
|
||
class Client | ||
{ | ||
protected $hostname = 'https://api.multipost.com/api'; | ||
protected $bearer_token = null; | ||
protected $client = null; | ||
protected $version = null; | ||
protected $os = null; | ||
|
||
const HTTP_GET = 'GET'; | ||
const HTTP_HEAD = 'HEAD'; | ||
const HTTP_DELETE = 'DELETE'; | ||
const HTTP_OPTIONS = 'OPTIONS'; | ||
const HTTP_PUT = 'PUT'; | ||
const HTTP_POST = 'POST'; | ||
const HTTP_PATCH = 'PATCH'; | ||
|
||
const ENVELOPE_C5 = 'C5'; | ||
const ENVELOPE_C4 = 'C4'; | ||
|
||
public function __construct($email, $password, $hostname = null) | ||
{ | ||
if ($hostname !== null) { | ||
$this->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); | ||
} | ||
|
||
} |