forked from flydreamers/shipwire-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipwireConnector.php
More file actions
168 lines (151 loc) · 4.4 KB
/
ShipwireConnector.php
File metadata and controls
168 lines (151 loc) · 4.4 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
<?php
namespace flydreamers\shipwire;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
class ShipwireConnector //extends Shipwire
{
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';
private function __construct()
{
}
/**
* Generates the connection instance for Shipwire
*
* @param $username
* @param $password
* @param string $environment
* @param string $version
* @throws Exception
*/
public static function init($username, $password, $environment = null)
{
self::$authorizationCode = base64_encode($username . ':' . $password);
if (null !== $environment) {
self::$environment = $environment;
}
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;
}
/**
* @var Client
*/
private $_client;
/**
* Gets guzzle client to manage URL Connections
*
* @return Client
*/
private function getClient()
{
if (!isset($this->client)) {
if (!isset(self::$authorizationCode)) {
throw new \Exception('Invalid authorization code');
}
$this->client = new Client(
[
'base_url' => self::getEndpointUrl(),
]
);
}
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
*
* @throws Exception
*/
public function api($resource, $params = [], $method = "GET", $body = null, $onlyResource = false)
{
$client = self::getClient();
$request = $client->createRequest($method, '/api/v3/' . $resource, [
'headers' => [
'User-Agent' => 'flydreamers-shipwireapi/1.0',
'Accept' => 'application/json',
'Authorization' => 'Basic ' . self::$authorizationCode
],
'body'=>$body
]);
if (count($params) > 0) {
$request->setQuery($params);
}
if ($body!==null){
$request->addHeader('content-type', 'application/json');
}
try {
$response = $client->send($request);
$data = $response->json();
if ($data['status'] >= 300) {
throw new ShipwireConnectionException($data['message'], $data['status']);
}
return $onlyResource?$data['resource']:$data;
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = $e->getResponse()->json();
switch ($response['status']) {
case 401:
throw new \flydreamers\shipwire\exceptions\InvalidAuthorizationException($response['message'], $response['status']);
break;
case 400:
throw new \flydreamers\shipwire\exceptions\InvalidRequestException($response['message'], $response['status']);
break;
}
throw new \flydreamers\shipwire\exceptions\ShipwireConnectionException($response['message'], $response['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;
}
}