|
2 | 2 |
|
3 | 3 | use Buzz\Client\ClientInterface; |
4 | 4 | use Buzz\Listener\ListenerInterface; |
| 5 | +use Buzz\Message\Form\FormUpload; |
5 | 6 |
|
6 | 7 | use Gitlab\Exception\ErrorException; |
7 | 8 | use Gitlab\Exception\RuntimeException; |
8 | 9 | use Gitlab\HttpClient\Listener\ErrorListener; |
9 | 10 | use Gitlab\HttpClient\Message\Request; |
10 | 11 | use Gitlab\HttpClient\Message\Response; |
| 12 | +use Gitlab\HttpClient\Message\FormRequest; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * Performs requests on Gitlab API. API documentation should be self-explanatory. |
@@ -112,9 +114,9 @@ public function get($path, array $parameters = array(), array $headers = array() |
112 | 114 | /** |
113 | 115 | * {@inheritDoc} |
114 | 116 | */ |
115 | | - public function post($path, array $parameters = array(), array $headers = array()) |
| 117 | + public function post($path, array $parameters = array(), array $headers = array(), array $files = array()) |
116 | 118 | { |
117 | | - return $this->request($path, $parameters, 'POST', $headers); |
| 119 | + return $this->request($path, $parameters, 'POST', $headers, $files); |
118 | 120 | } |
119 | 121 |
|
120 | 122 | /** |
@@ -144,13 +146,11 @@ public function put($path, array $parameters = array(), array $headers = array() |
144 | 146 | /** |
145 | 147 | * {@inheritDoc} |
146 | 148 | */ |
147 | | - public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) |
| 149 | + public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array(), array $files = array()) |
148 | 150 | { |
149 | 151 | $path = trim($this->baseUrl.$path, '/'); |
150 | 152 |
|
151 | | - $request = $this->createRequest($httpMethod, $path); |
152 | | - $request->addHeaders($headers); |
153 | | - $request->setContent(http_build_query($parameters)); |
| 153 | + $request = $this->createRequest($httpMethod, $path, $parameters, $headers, $files); |
154 | 154 |
|
155 | 155 | $hasListeners = 0 < count($this->listeners); |
156 | 156 | if ($hasListeners) { |
@@ -200,13 +200,31 @@ public function getLastResponse() |
200 | 200 | /** |
201 | 201 | * @param string $httpMethod |
202 | 202 | * @param string $url |
203 | | - * @return Request |
| 203 | + * @param array $parameters |
| 204 | + * @param array $headers |
| 205 | + * @param array $files |
| 206 | + * |
| 207 | + * @return FormRequest|Request |
204 | 208 | */ |
205 | | - private function createRequest($httpMethod, $url) |
| 209 | + private function createRequest($httpMethod, $url, array $parameters, array $headers, array $files) |
206 | 210 | { |
207 | | - $request = new Request($httpMethod); |
| 211 | + if (empty($files)) { |
| 212 | + $request = new Request($httpMethod); |
| 213 | + $request->setContent(http_build_query($parameters)); |
| 214 | + } else { |
| 215 | + $request = new FormRequest($httpMethod); |
| 216 | + foreach ($parameters as $name => $value) { |
| 217 | + $request->setField($name, $value); |
| 218 | + } |
| 219 | + |
| 220 | + foreach ($files as $name => $file) { |
| 221 | + $upload = new FormUpload($file); |
| 222 | + $request->setField($name, $upload); |
| 223 | + } |
| 224 | + } |
208 | 225 | $request->setHeaders($this->headers); |
209 | 226 | $request->fromUrl($url); |
| 227 | + $request->addHeaders($headers); |
210 | 228 |
|
211 | 229 | return $request; |
212 | 230 | } |
|
0 commit comments