Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple from Buzz #191

Merged
merged 1 commit into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@ Based on [php-github-api](https://github.com/m4tthumphrey/php-github-api) and co

Installation
------------
1. Install Composer

```bash
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
```
Via [composer](https://getcomposer.org)

2. Add the following to your require block in composer.json config.

> Note: be careful when using the `dev-master` tag as this may have unexpected results depending on your version of
Gitlab. See the Versioning section below for more information.

`php composer.phar require m4tthumphrey/php-gitlab-api:dev-master`
```bash
composer require m4tthumphrey/php-gitlab-api php-http/guzzle6-adapter
```

3. Include Composer's autoloader:
Why `php-http/guzzle6-adapter`? We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io).

```php
require_once dirname(__DIR__).'/vendor/autoload.php';
```
You can visit [HTTPlug for library users](http://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages.

Versioning
----------
Expand All @@ -41,8 +32,9 @@ General API Usage
-----------------

```php
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
$client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
$client = \Gitlab\Client::create('http://git.yourdomain.com')
->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN)
;

$project = $client->api('projects')->create('My Project', array(
'description' => 'This is a project',
Expand All @@ -57,8 +49,9 @@ Model Usage
You can also use the library in an object oriented manner:

```php
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
$client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
$client = \Gitlab\Client::create('http://git.yourdomain.com')
->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN)
;

# Creating a new project
$project = \Gitlab\Model\Project::create($client, 'My Project', array(
Expand Down
14 changes: 14 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# UPGRADE FROM 8.0 to 9.0

Since 9.0, lib no longer use buzz 0.7+, instead it has an HTTPlug abstraction layer.

## `Gitlab\Client` changes

* The constructor no longer allow to specify base url. Use `setUrl` or `Client::create` instead.
* The default url is set to `https://gitlab.com`.
* The `$options` constructor argument have been removed, the `getOption` and `setOption` methods have been removed.
See [documentation](doc/customize.md) to know how to customize the client timeout and how to use a custom user agent.
* The `setBaseUrl` and `getBaseUrl` methods have been removed. Use `setUrl` instead.
* The `clearHeaders` and `setHeaders` methods have been removed. See [documentation](doc/customize.md) to know how use custom headers.
* The `setHttpClient` method have been removed. Use a `Gitlab\HttpClient\Builder` instead.
* The `getHttpClient` method return type is changed to `Http\Client\Common\HttpMethodsClient`.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
],
"require": {
"php": "^5.6 || ^7.0",
"ext-curl": "*",
"ext-xml": "*",
"kriswallsmith/buzz": ">=0.7"
"php-http/client-common": "^1.5",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.2",
"php-http/httplug": "^1.1",
"php-http/multipart-stream-builder": "^1.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.2",
"php-http/guzzle6-adapter": "^1.0",
"php-http/mock-client": "^1.0",
"phpunit/phpunit": "~4.5"
},
"autoload": {
Expand Down
28 changes: 28 additions & 0 deletions doc/customize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Customize `php-gitlab-api`

### How to set custom headers (including `User-Agent`)?

By providing a `Gitlab\HttpClient\Builder` to the `Gitlab\Client` constructor, you can customize the HTTP client.

```php
$plugin = new Http\Client\Common\Plugin\HeaderSetPlugin([
'User-Agent' => 'Foobar',
]);

$builder = new Gitlab\HttpClient\Builder();
$builder->addPlugin($plugin);

$client = new Gitlab\Client($builder);
```
Read more about [HTTPlug plugins here](http://docs.php-http.org/en/latest/plugins/introduction.html#how-it-works).

### How to customize the HTTP client timeout?
As timeout configuration is not compatible with HTTP client abstraction, you have to create the `Gitlab\Client` with
an explicit HTTP client implementation.

```php
$httpClient = Http\Adapter\Guzzle6::createWithConfig([
'timeout' => 1.0
]);
$client = Gitlab\Client::createWithHttpClient($httpClient);
```
99 changes: 87 additions & 12 deletions lib/Gitlab/Api/AbstractApi.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php namespace Gitlab\Api;

use Gitlab\Client;
use Gitlab\HttpClient\Message\ResponseMediator;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Http\Message\StreamFactory;

/**
* Abstract class for Api classes
Expand All @@ -23,12 +27,19 @@ abstract class AbstractApi implements ApiInterface
*/
protected $client;

/**
* @var StreamFactory
*/
private $streamFactory;

/**
* @param Client $client
* @param StreamFactory|null $streamFactory
*/
public function __construct(Client $client)
public function __construct(Client $client, StreamFactory $streamFactory = null)
{
$this->client = $client;
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
}

/**
Expand All @@ -48,9 +59,11 @@ public function configure()
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
$path = $this->preparePath($path, $parameters);

return $response->getContent();
$response = $this->client->getHttpClient()->get($path, $requestHeaders);

return ResponseMediator::getContent($response);
}

/**
Expand All @@ -62,9 +75,35 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
*/
protected function post($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
{
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders, $files);

return $response->getContent();
$path = $this->preparePath($path);

$body = null;
if (empty($files) && !empty($parameters)) {
$body = $this->streamFactory->createStream(http_build_query($parameters));
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
} elseif (!empty($files)) {
$builder = new MultipartStreamBuilder($this->streamFactory);

foreach ($parameters as $name => $value) {
$builder->addResource($name, $value);
}

foreach ($files as $name => $file) {
$builder->addResource($name, fopen($file, 'r'), [
'headers' => [
'Content-Type' => $this->guessContentType($file),
],
'filename' => basename($file),
]);
}

$body = $builder->build();
$requestHeaders['Content-Type'] = 'multipart/form-data; boundary='.$builder->getBoundary();
}

$response = $this->client->getHttpClient()->post($path, $requestHeaders, $body);

return ResponseMediator::getContent($response);
}

/**
Expand All @@ -75,9 +114,13 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
*/
protected function patch($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
$path = $this->preparePath($path);

$body = empty($parameters) ? null : $this->streamFactory->createStream(http_build_query($parameters));

return $response->getContent();
$response = $this->client->getHttpClient()->patch($path, $requestHeaders, $body);

return ResponseMediator::getContent($response);
}

/**
Expand All @@ -88,9 +131,13 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
*/
protected function put($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
$path = $this->preparePath($path);

$body = empty($parameters) ? null : $this->streamFactory->createStream(http_build_query($parameters));

$response = $this->client->getHttpClient()->put($path, $requestHeaders, $body);

return $response->getContent();
return ResponseMediator::getContent($response);
}

/**
Expand All @@ -101,9 +148,13 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
*/
protected function delete($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
$path = $this->preparePath($path);

return $response->getContent();
$body = empty($parameters) ? null : $this->streamFactory->createStream(http_build_query($parameters));

$response = $this->client->getHttpClient()->delete($path, $requestHeaders, $body);

return ResponseMediator::getContent($response);
}

/**
Expand All @@ -126,4 +177,28 @@ protected function encodePath($path)

return str_replace('.', '%2E', $path);
}

private function preparePath($path, array $parameters = [])
{
if (count($parameters) > 0) {
$path .= '?'.http_build_query($parameters);
}

return $path;
}

/**
* @param $file
*
* @return string
*/
private function guessContentType($file)
{
if (!class_exists(\finfo::class, false)) {
return 'application/octet-stream';
}
$finfo = new \finfo(FILEINFO_MIME_TYPE);

return $finfo->file($file);
}
}
Loading