-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShipwireConnector.php
More file actions
188 lines (163 loc) · 4.98 KB
/
ShipwireConnector.php
File metadata and controls
188 lines (163 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace flydreamers\shipwire;
use flydreamers\shipwire\exceptions\InvalidAuthorizationException;
use flydreamers\shipwire\exceptions\InvalidRequestException;
use flydreamers\shipwire\exceptions\ShipwireConnectionException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
class ShipwireConnector
{
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
/**
* Environment method for integration. Possible values: 'live', 'sandbox'
* @var string
*/
static $environment = 'live';
/**
* Sandbox Base Url for Shipwire API
* @var string
*/
static $sandboxBaseUrl = 'https://api.beta.shipwire.com';
/**
* Live Base Url for Shipwire API
* @var string
*/
static $baseUrl = 'https://api.shipwire.com';
/**
* @var string
*/
static $authorizationCode;
/**
* @var string
*/
static $version = 'v3';
/**
* @var HandlerStack
*/
static $handlerStack;
private function __construct()
{
}
/**
* Generates the connection instance for Shipwire
*
* @param $username
* @param $password
* @param string $environment
* @param HandlerStack $handlerStack
*/
public static function init($username, $password, $environment = null, HandlerStack $handlerStack = null)
{
self::$authorizationCode = base64_encode($username . ':' . $password);
if (null !== $environment) {
self::$environment = $environment;
}
if (null !== $handlerStack) {
self::$handlerStack = $handlerStack;
}
self::$instance = null;
}
/**
* @var ShipwireConnector
*/
private static $instance = null;
/**
* @return ShipwireConnector
* @throws \Exception
*/
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self();
self::$instance->getClient();
}
return self::$instance;
}
/**
* Gets guzzle client to manage URL Connections
*
* @return Client
* @throws \Exception
*/
private function getClient()
{
if (!isset($this->client)) {
if (!isset(self::$authorizationCode)) {
throw new \Exception('Invalid authorization code');
}
$config = ['base_uri' => self::getEndpointUrl()];
if (isset(self::$handlerStack)) {
$config['handler'] = self::$handlerStack;
}
$this->client = new Client($config);
}
return $this->client;
}
/**
* Send an api request to Shipwire Endpoint
*
* @param string $resource function to be called
* @param array $params key value parameters
* @param string $method
* @param string $body
* @param bool $onlyResource
* @return mixed
* @throws InvalidAuthorizationException
* @throws InvalidRequestException
* @throws ShipwireConnectionException
* @throws \Exception
*/
public function api($resource, $params = [], $method = "GET", $body = null, $onlyResource = false)
{
$client = self::getClient();
try {
$headers = [
'User-Agent' => 'flydreamers-shipwireapi/1.0',
'Accept' => 'application/json',
'Authorization' => 'Basic ' . self::$authorizationCode
];
if ($body !== null) {
$headers['content-type'] = 'application/json';
}
$response = $client->request($method, '/api/v3/'.$resource, [
'headers' => $headers,
'query' => $params,
'body' => $body,
]);
$data = json_decode($response->getBody(), true);
if ($data['status'] >= 300) {
throw new ShipwireConnectionException($data['message'], $data['status']);
}
return $onlyResource?$data['resource']:$data;
} catch (RequestException $e) {
if ($responseBody = $e->getResponse()->getBody()) {
$data = json_decode($responseBody, true);
switch ($data['status']) {
case 401:
throw new InvalidAuthorizationException($data['message'], $data['status']);
break;
case 400:
throw new InvalidRequestException($data['message'], $data['status']);
break;
}
throw new ShipwireConnectionException($data['message'], $data['status']);
}
} catch (\Exception $exception) {
throw $exception;
}
}
/**
* Gets the endpoint URL based on
* @return string
*/
protected static function getEndpointUrl()
{
if (self::$environment == 'live') {
return self::$baseUrl;
}
return self::$sandboxBaseUrl;
}
}