|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Extending GuzzleHttp client |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Graviton\ImportExport\Service; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use GuzzleHttp\Psr7; |
|
9
|
|
|
use GuzzleHttp\Promise; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class HttpClient |
|
13
|
|
|
* Extends Guzzle client |
|
14
|
|
|
* |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class FileSender |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ClientInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ClientInterface $client real guzzle client |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(ClientInterface $client) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->client = $client; |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create and send an asynchronous HTTP request sending a file if needed. |
|
36
|
|
|
* |
|
37
|
|
|
* Use an absolute path to override the base path of the client, or a |
|
38
|
|
|
* relative path to append to the base path of the client. The URL can |
|
39
|
|
|
* contain the query string as well. Use an array to provide a URL |
|
40
|
|
|
* template and additional variables to use in the URL template expansion. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $method HTTP method |
|
43
|
|
|
* @param string|UriInterface $uri URI object or string. |
|
44
|
|
|
* @param array $options Request options to apply. |
|
45
|
|
|
* |
|
46
|
|
|
* @return PromiseInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function requestAsync($method, $uri, array $options = []) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->client->requestAsync($method, $uri, $this->checkFileUploadRequest($options)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create and send an HTTP request sending a file as needed. |
|
55
|
|
|
* |
|
56
|
|
|
* Use an absolute path to override the base path of the client, or a |
|
57
|
|
|
* relative path to append to the base path of the client. The URL can |
|
58
|
|
|
* contain the query string as well. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $method HTTP method |
|
61
|
|
|
* @param string|UriInterface $uri URI object or string. |
|
62
|
|
|
* @param array $options Request options to apply. |
|
63
|
|
|
* |
|
64
|
|
|
* @return ResponseInterface |
|
65
|
|
|
* @throws GuzzleException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function request($method, $uri, array $options = []) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->client->request($method, $uri, $this->checkFileUploadRequest($options)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $options Curl data options |
|
75
|
|
|
* @return array options |
|
76
|
|
|
*/ |
|
77
|
|
|
private function checkFileUploadRequest($options) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!array_key_exists('upload', $options)) { |
|
80
|
|
|
return $options; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$fileName = $options['upload']; |
|
84
|
|
|
unset($options['upload']); |
|
85
|
|
|
|
|
86
|
|
|
if (!$fileName) { |
|
87
|
|
|
return $options; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!array_key_exists('json', $options)) { |
|
91
|
|
|
throw new \RuntimeException("No json option passed"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// We send the data in URL |
|
95
|
|
|
$options['query'] = ['metadata' => json_encode($options['json'])]; |
|
96
|
|
|
|
|
97
|
|
|
// Guzzle modify header if we send json. |
|
98
|
|
|
unset($options['json']); |
|
99
|
|
|
|
|
100
|
|
|
// We send file only |
|
101
|
|
|
$options['body'] = file_get_contents($fileName); |
|
102
|
|
|
|
|
103
|
|
|
return $options; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|