Skip to content

Commit 98f86d8

Browse files
committed
Upload a file in a project
1 parent 489bc48 commit 98f86d8

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
5757
* @param string $path
5858
* @param array $parameters
5959
* @param array $requestHeaders
60+
* @param array $files
6061
* @return mixed
6162
*/
62-
protected function post($path, array $parameters = array(), $requestHeaders = array())
63+
protected function post($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
6364
{
64-
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
65+
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders, $files);
6566

6667
return $response->getContent();
6768
}

lib/Gitlab/Api/Projects.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,14 @@ public function removeVariable($project_id, $key)
473473
{
474474
return $this->delete($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)));
475475
}
476+
477+
/**
478+
* @param int $project_id
479+
* @param string $file
480+
* @return mixed
481+
*/
482+
public function uploadFile($project_id, $file)
483+
{
484+
return $this->post($this->getProjectPath($project_id, 'uploads'), array(), array(), array('file' => $file));
485+
}
476486
}

lib/Gitlab/HttpClient/HttpClient.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
use Buzz\Client\ClientInterface;
44
use Buzz\Listener\ListenerInterface;
5+
use Buzz\Message\Form\FormUpload;
56

67
use Gitlab\Exception\ErrorException;
78
use Gitlab\Exception\RuntimeException;
89
use Gitlab\HttpClient\Listener\ErrorListener;
910
use Gitlab\HttpClient\Message\Request;
1011
use Gitlab\HttpClient\Message\Response;
12+
use Gitlab\HttpClient\Message\FormRequest;
1113

1214
/**
1315
* 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()
112114
/**
113115
* {@inheritDoc}
114116
*/
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())
116118
{
117-
return $this->request($path, $parameters, 'POST', $headers);
119+
return $this->request($path, $parameters, 'POST', $headers, $files);
118120
}
119121

120122
/**
@@ -144,13 +146,11 @@ public function put($path, array $parameters = array(), array $headers = array()
144146
/**
145147
* {@inheritDoc}
146148
*/
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())
148150
{
149151
$path = trim($this->baseUrl.$path, '/');
150152

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);
154154

155155
$hasListeners = 0 < count($this->listeners);
156156
if ($hasListeners) {
@@ -200,13 +200,31 @@ public function getLastResponse()
200200
/**
201201
* @param string $httpMethod
202202
* @param string $url
203-
* @return Request
203+
* @param array $parameters
204+
* @param array $headers
205+
* @param array $files
206+
*
207+
* @return FormRequest|Request
204208
*/
205-
private function createRequest($httpMethod, $url)
209+
private function createRequest($httpMethod, $url, array $parameters, array $headers, array $files)
206210
{
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+
}
208225
$request->setHeaders($this->headers);
209226
$request->fromUrl($url);
227+
$request->addHeaders($headers);
210228

211229
return $request;
212230
}

lib/Gitlab/HttpClient/HttpClientInterface.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ public function get($path, array $parameters = array(), array $headers = array()
2424
/**
2525
* Send a POST request
2626
*
27-
* @param string $path Request path
28-
* @param array $parameters POST Parameters
29-
* @param array $headers Reconfigure the request headers for this call only
27+
* @param string $path Request path
28+
* @param array $parameters POST Parameters
29+
* @param array $headers Reconfigure the request headers for this call only
30+
* @param array $files Files paths of files to upload
3031
*
3132
* @return array Data
3233
*/
33-
public function post($path, array $parameters = array(), array $headers = array());
34+
public function post($path, array $parameters = array(), array $headers = array(), array $files = array());
3435

3536
/**
3637
* Send a PATCH request
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php namespace Gitlab\HttpClient\Message;
2+
3+
use Buzz\Message\Form\FormRequest as BaseFormRequest;
4+
5+
class FormRequest extends BaseFormRequest
6+
{
7+
8+
}

test/Gitlab/Tests/Api/AbstractApiTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public function get($path, array $parameters = array(), $requestHeaders = array(
146146
/**
147147
* {@inheritDoc}
148148
*/
149-
public function post($path, array $parameters = array(), $requestHeaders = array())
149+
public function post($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
150150
{
151-
return parent::post($path, $parameters, $requestHeaders);
151+
return parent::post($path, $parameters, $requestHeaders, $files);
152152
}
153153

154154
/**

0 commit comments

Comments
 (0)