@@ -95,8 +95,8 @@ discard block |
||
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | - * @param $endpoint |
|
99 | - * @param $method |
|
98 | + * @param string $endpoint |
|
99 | + * @param string $method |
|
100 | 100 | * @param array $data |
101 | 101 | * @return mixed |
102 | 102 | * @throws \Exception |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | /** |
133 | 133 | * @param $uri |
134 | 134 | * @param string $method |
135 | - * @param array $headers |
|
135 | + * @param string[] $headers |
|
136 | 136 | * @param null $body |
137 | 137 | * @return B2Response |
138 | 138 | */ |
@@ -182,9 +182,12 @@ discard block |
||
182 | 182 | /** |
183 | 183 | * @param $data |
184 | 184 | * @param $sha1 |
185 | - * @param $fileName |
|
185 | + * @param string $fileName |
|
186 | 186 | * @param $url |
187 | 187 | * @param $token |
188 | + * @param string $fileData |
|
189 | + * @param string $fileDataSha1 |
|
190 | + * @param string $contentType |
|
188 | 191 | */ |
189 | 192 | public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
190 | 193 | { |
@@ -200,6 +203,7 @@ discard block |
||
200 | 203 | |
201 | 204 | /** |
202 | 205 | * @param $url |
206 | + * @param string $uri |
|
203 | 207 | */ |
204 | 208 | public function downloadFileByName($uri) |
205 | 209 | { |
@@ -8,221 +8,221 @@ |
||
8 | 8 | class B2Client |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $accountId; |
|
15 | - |
|
16 | - /** |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $applicationKey; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $apiUrl; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $authorizationToken; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $downloadUrl; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var CurlRequest |
|
38 | - */ |
|
39 | - protected $CurlRequest; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var Files |
|
43 | - */ |
|
44 | - public $Files; |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * B2Client constructor. |
|
49 | - * @param string $accountId |
|
50 | - * @param string $applicationKey |
|
51 | - */ |
|
52 | - public function __construct($accountId, $applicationKey, CurlRequest $curlRequest = null) |
|
53 | - { |
|
54 | - |
|
55 | - if (!$curlRequest) { |
|
56 | - $this->CurlRequest = new CurlRequest(); |
|
57 | - } else { |
|
58 | - $this->CurlRequest = $curlRequest; |
|
59 | - } |
|
60 | - |
|
61 | - $this->accountId = $accountId; |
|
62 | - $this->applicationKey = $applicationKey; |
|
63 | - $this->Files = new Files($this); |
|
64 | - |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param array $result |
|
69 | - */ |
|
70 | - public function setToken($result) |
|
71 | - { |
|
72 | - $this->authorizationToken = $result['authorizationToken']; |
|
73 | - $this->apiUrl = $result['apiUrl']; |
|
74 | - $this->downloadUrl = $result['downloadUrl']; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * |
|
79 | - */ |
|
80 | - public function requestToken() |
|
81 | - { |
|
82 | - |
|
83 | - $response = $this->curl('https://api.backblaze.com/b2api/v1/b2_authorize_account', 'GET', [ |
|
84 | - $this->buildBasicAuthHeader() |
|
85 | - ]); |
|
86 | - |
|
87 | - $data = $response->getJsonData(); |
|
88 | - if ($response->getStatusCode() === 200) { |
|
89 | - $this->setToken($data); |
|
90 | - return true; |
|
91 | - } else { |
|
92 | - throw new \RuntimeException('Failed to get token: ' . $data['message']); |
|
93 | - } |
|
94 | - |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param $endpoint |
|
99 | - * @param $method |
|
100 | - * @param array $data |
|
101 | - * @return mixed |
|
102 | - * @throws \Exception |
|
103 | - */ |
|
104 | - public function call($endpoint, $method, $data = []) |
|
105 | - { |
|
106 | - |
|
107 | - if (empty($this->authorizationToken)) { |
|
108 | - throw new \Exception('You must set or generate a token'); |
|
109 | - } |
|
110 | - |
|
111 | - $headers = [ |
|
112 | - $this->buildTokenAuthHeader() |
|
113 | - ]; |
|
114 | - |
|
115 | - $headers[] = 'Content-Type: application/json'; |
|
116 | - $headers[] = "Accept: application/json"; |
|
117 | - $body = json_encode($data); |
|
118 | - |
|
119 | - $response = $this->curl($this->apiUrl . '/b2api/v1/' . $endpoint, $method, $headers, $body); |
|
120 | - |
|
121 | - if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
|
122 | - return $response->getJsonData(); |
|
123 | - } |
|
124 | - |
|
125 | - if ($response->getStatusCode() >= 400) { |
|
126 | - $data = $response->getJsonData(); |
|
127 | - throw new \RuntimeException('Error ' . $response->getStatusCode() . ' - ' . $data['message']); |
|
128 | - } |
|
129 | - |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param $uri |
|
134 | - * @param string $method |
|
135 | - * @param array $headers |
|
136 | - * @param null $body |
|
137 | - * @return B2Response |
|
138 | - */ |
|
139 | - public function curl($uri, $method = 'GET', $headers = [], $body = null) |
|
140 | - { |
|
141 | - |
|
142 | - $response = new B2Response(); |
|
143 | - |
|
144 | - $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
145 | - $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); |
|
146 | - $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
147 | - $this->CurlRequest->setOption(CURLOPT_POST, 1); |
|
148 | - $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
|
149 | - $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
150 | - $this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, |
|
151 | - function ($curl, $header) use ($response) { |
|
152 | - $response->addHeader($header); |
|
153 | - return strlen($header); |
|
154 | - }); |
|
155 | - |
|
156 | - $resp = $this->CurlRequest->execute(); |
|
157 | - if ($this->CurlRequest->getErrorNo() !== 0) { |
|
158 | - throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
159 | - } else { |
|
160 | - $response->setData($resp); |
|
161 | - $response->setStatusCode($this->CurlRequest->getInfo(CURLINFO_HTTP_CODE)); |
|
162 | - return $response; |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - public function buildBasicAuthHeader() |
|
170 | - { |
|
171 | - return 'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->applicationKey); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - public function buildTokenAuthHeader() |
|
178 | - { |
|
179 | - return 'Authorization: ' . $this->authorizationToken; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @param $data |
|
184 | - * @param $sha1 |
|
185 | - * @param $fileName |
|
186 | - * @param $url |
|
187 | - * @param $token |
|
188 | - */ |
|
189 | - public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
|
190 | - { |
|
191 | - $headers = []; |
|
192 | - $headers[] = "Authorization: " . $uploadToken; |
|
193 | - $headers[] = "X-Bz-File-Name: " . $fileName; |
|
194 | - $headers[] = "Content-Type: " . $contentType; |
|
195 | - $headers[] = "X-Bz-Content-Sha1: " . $fileDataSha1; |
|
196 | - |
|
197 | - $response = $this->curl($uploadUrl, 'POST', $headers, $fileData); |
|
198 | - return $response->getJsonData(); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @param $url |
|
203 | - */ |
|
204 | - public function downloadFileByName($uri) |
|
205 | - { |
|
206 | - |
|
207 | - $uri = $this->downloadUrl . "/file/" . $uri; |
|
208 | - $headers = [ |
|
209 | - $this->buildTokenAuthHeader() |
|
210 | - ]; |
|
211 | - |
|
212 | - $response = $this->curl($uri, 'GET', $headers); |
|
213 | - if ($response->getStatusCode() === 200) { |
|
214 | - return $response->getData(); |
|
215 | - } else { |
|
216 | - throw new \RuntimeException('Download failed. ' . $response->getStatusCode()); |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - public function getDownloadUrl() |
|
224 | - { |
|
225 | - return $this->downloadUrl; |
|
226 | - } |
|
11 | + /** |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $accountId; |
|
15 | + |
|
16 | + /** |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $applicationKey; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $apiUrl; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $authorizationToken; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $downloadUrl; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var CurlRequest |
|
38 | + */ |
|
39 | + protected $CurlRequest; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var Files |
|
43 | + */ |
|
44 | + public $Files; |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * B2Client constructor. |
|
49 | + * @param string $accountId |
|
50 | + * @param string $applicationKey |
|
51 | + */ |
|
52 | + public function __construct($accountId, $applicationKey, CurlRequest $curlRequest = null) |
|
53 | + { |
|
54 | + |
|
55 | + if (!$curlRequest) { |
|
56 | + $this->CurlRequest = new CurlRequest(); |
|
57 | + } else { |
|
58 | + $this->CurlRequest = $curlRequest; |
|
59 | + } |
|
60 | + |
|
61 | + $this->accountId = $accountId; |
|
62 | + $this->applicationKey = $applicationKey; |
|
63 | + $this->Files = new Files($this); |
|
64 | + |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param array $result |
|
69 | + */ |
|
70 | + public function setToken($result) |
|
71 | + { |
|
72 | + $this->authorizationToken = $result['authorizationToken']; |
|
73 | + $this->apiUrl = $result['apiUrl']; |
|
74 | + $this->downloadUrl = $result['downloadUrl']; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * |
|
79 | + */ |
|
80 | + public function requestToken() |
|
81 | + { |
|
82 | + |
|
83 | + $response = $this->curl('https://api.backblaze.com/b2api/v1/b2_authorize_account', 'GET', [ |
|
84 | + $this->buildBasicAuthHeader() |
|
85 | + ]); |
|
86 | + |
|
87 | + $data = $response->getJsonData(); |
|
88 | + if ($response->getStatusCode() === 200) { |
|
89 | + $this->setToken($data); |
|
90 | + return true; |
|
91 | + } else { |
|
92 | + throw new \RuntimeException('Failed to get token: ' . $data['message']); |
|
93 | + } |
|
94 | + |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param $endpoint |
|
99 | + * @param $method |
|
100 | + * @param array $data |
|
101 | + * @return mixed |
|
102 | + * @throws \Exception |
|
103 | + */ |
|
104 | + public function call($endpoint, $method, $data = []) |
|
105 | + { |
|
106 | + |
|
107 | + if (empty($this->authorizationToken)) { |
|
108 | + throw new \Exception('You must set or generate a token'); |
|
109 | + } |
|
110 | + |
|
111 | + $headers = [ |
|
112 | + $this->buildTokenAuthHeader() |
|
113 | + ]; |
|
114 | + |
|
115 | + $headers[] = 'Content-Type: application/json'; |
|
116 | + $headers[] = "Accept: application/json"; |
|
117 | + $body = json_encode($data); |
|
118 | + |
|
119 | + $response = $this->curl($this->apiUrl . '/b2api/v1/' . $endpoint, $method, $headers, $body); |
|
120 | + |
|
121 | + if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
|
122 | + return $response->getJsonData(); |
|
123 | + } |
|
124 | + |
|
125 | + if ($response->getStatusCode() >= 400) { |
|
126 | + $data = $response->getJsonData(); |
|
127 | + throw new \RuntimeException('Error ' . $response->getStatusCode() . ' - ' . $data['message']); |
|
128 | + } |
|
129 | + |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param $uri |
|
134 | + * @param string $method |
|
135 | + * @param array $headers |
|
136 | + * @param null $body |
|
137 | + * @return B2Response |
|
138 | + */ |
|
139 | + public function curl($uri, $method = 'GET', $headers = [], $body = null) |
|
140 | + { |
|
141 | + |
|
142 | + $response = new B2Response(); |
|
143 | + |
|
144 | + $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
145 | + $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); |
|
146 | + $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
147 | + $this->CurlRequest->setOption(CURLOPT_POST, 1); |
|
148 | + $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
|
149 | + $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
150 | + $this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, |
|
151 | + function ($curl, $header) use ($response) { |
|
152 | + $response->addHeader($header); |
|
153 | + return strlen($header); |
|
154 | + }); |
|
155 | + |
|
156 | + $resp = $this->CurlRequest->execute(); |
|
157 | + if ($this->CurlRequest->getErrorNo() !== 0) { |
|
158 | + throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
159 | + } else { |
|
160 | + $response->setData($resp); |
|
161 | + $response->setStatusCode($this->CurlRequest->getInfo(CURLINFO_HTTP_CODE)); |
|
162 | + return $response; |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + public function buildBasicAuthHeader() |
|
170 | + { |
|
171 | + return 'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->applicationKey); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + public function buildTokenAuthHeader() |
|
178 | + { |
|
179 | + return 'Authorization: ' . $this->authorizationToken; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @param $data |
|
184 | + * @param $sha1 |
|
185 | + * @param $fileName |
|
186 | + * @param $url |
|
187 | + * @param $token |
|
188 | + */ |
|
189 | + public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
|
190 | + { |
|
191 | + $headers = []; |
|
192 | + $headers[] = "Authorization: " . $uploadToken; |
|
193 | + $headers[] = "X-Bz-File-Name: " . $fileName; |
|
194 | + $headers[] = "Content-Type: " . $contentType; |
|
195 | + $headers[] = "X-Bz-Content-Sha1: " . $fileDataSha1; |
|
196 | + |
|
197 | + $response = $this->curl($uploadUrl, 'POST', $headers, $fileData); |
|
198 | + return $response->getJsonData(); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @param $url |
|
203 | + */ |
|
204 | + public function downloadFileByName($uri) |
|
205 | + { |
|
206 | + |
|
207 | + $uri = $this->downloadUrl . "/file/" . $uri; |
|
208 | + $headers = [ |
|
209 | + $this->buildTokenAuthHeader() |
|
210 | + ]; |
|
211 | + |
|
212 | + $response = $this->curl($uri, 'GET', $headers); |
|
213 | + if ($response->getStatusCode() === 200) { |
|
214 | + return $response->getData(); |
|
215 | + } else { |
|
216 | + throw new \RuntimeException('Download failed. ' . $response->getStatusCode()); |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + public function getDownloadUrl() |
|
224 | + { |
|
225 | + return $this->downloadUrl; |
|
226 | + } |
|
227 | 227 | |
228 | 228 | } |
229 | 229 | \ No newline at end of file |
@@ -148,7 +148,7 @@ |
||
148 | 148 | $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
149 | 149 | $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
150 | 150 | $this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, |
151 | - function ($curl, $header) use ($response) { |
|
151 | + function($curl, $header) use ($response) { |
|
152 | 152 | $response->addHeader($header); |
153 | 153 | return strlen($header); |
154 | 154 | }); |
@@ -8,50 +8,50 @@ |
||
8 | 8 | class B2Response |
9 | 9 | { |
10 | 10 | |
11 | - private $data = null; |
|
12 | - |
|
13 | - private $statusCode = 0; |
|
14 | - |
|
15 | - private $headers = []; |
|
16 | - |
|
17 | - public function __construct() |
|
18 | - { |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * @param $header |
|
23 | - */ |
|
24 | - public function addHeader($header) |
|
25 | - { |
|
26 | - $this->headers[] = $header; |
|
27 | - } |
|
28 | - |
|
29 | - public function setData($data) |
|
30 | - { |
|
31 | - $this->data = $data; |
|
32 | - } |
|
33 | - |
|
34 | - public function setStatusCode($statusCode) |
|
35 | - { |
|
36 | - $this->statusCode = $statusCode; |
|
37 | - } |
|
38 | - |
|
39 | - public function getStatusCode() |
|
40 | - { |
|
41 | - return $this->statusCode; |
|
42 | - } |
|
43 | - |
|
44 | - public function getJsonData() |
|
45 | - { |
|
46 | - if ($this->data) { |
|
47 | - return json_decode($this->data, true); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function getData() |
|
52 | - { |
|
53 | - return $this->data; |
|
54 | - } |
|
11 | + private $data = null; |
|
12 | + |
|
13 | + private $statusCode = 0; |
|
14 | + |
|
15 | + private $headers = []; |
|
16 | + |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param $header |
|
23 | + */ |
|
24 | + public function addHeader($header) |
|
25 | + { |
|
26 | + $this->headers[] = $header; |
|
27 | + } |
|
28 | + |
|
29 | + public function setData($data) |
|
30 | + { |
|
31 | + $this->data = $data; |
|
32 | + } |
|
33 | + |
|
34 | + public function setStatusCode($statusCode) |
|
35 | + { |
|
36 | + $this->statusCode = $statusCode; |
|
37 | + } |
|
38 | + |
|
39 | + public function getStatusCode() |
|
40 | + { |
|
41 | + return $this->statusCode; |
|
42 | + } |
|
43 | + |
|
44 | + public function getJsonData() |
|
45 | + { |
|
46 | + if ($this->data) { |
|
47 | + return json_decode($this->data, true); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function getData() |
|
52 | + { |
|
53 | + return $this->data; |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |
57 | 57 |