From 6556c5144c93c95998bd7560376d751244f2f62f Mon Sep 17 00:00:00 2001 From: a-komarev Date: Mon, 22 May 2017 12:13:06 +0300 Subject: [PATCH] Update to YouTrack PHP SDK stable version --- CHANGELOG.md | 4 +-- README.md | 42 +++++++++++++++++++---- composer.json | 2 +- src/Providers/YouTrackServiceProvider.php | 19 +++++++--- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f8ba4b..add525a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,6 @@ All notable changes to `laravel-youtrack-sdk` will be documented in this file. -## 1.0.0 - 2017-05-19 +## 1.0.0 - 2017-05-22 -- Initial release +- Initial release. diff --git a/README.md b/README.md index a6cd5da..1d91339 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Laravel wrapper for the [YouTrack PHP SDK](https://github.com/cybercog/youtrack- - Using contracts to keep high customization capabilities. - Multiple authorization strategies: Token, Cookie. - Following PHP Standard Recommendations: + - [PSR-1 (Basic Coding Standard)](http://www.php-fig.org/psr/psr-1/). - [PSR-2 (Coding Style Guide)](http://www.php-fig.org/psr/psr-2/). - [PSR-4 (Autoloading Standard)](http://www.php-fig.org/psr/psr-4/). - [PSR-7 (HTTP Message Interface)](http://www.php-fig.org/psr/psr-7/). @@ -121,6 +122,31 @@ $youtrack = app(\Cog\YouTrack\Rest\YouTrackClient::class); ### API requests +#### HTTP request + +```php +$method = 'POST'; // GET, POST, PUT, DELETE, PATCH or any custom ones +$response = $youtrack->request($method, '/issue', [ + 'project' => 'TEST', + 'summary' => 'New test issue', + 'description' => 'Test description', +]); +``` + +You can [customize requests created and transferred by a client using request options](http://docs.guzzlephp.org/en/latest/request-options.html). Request options control various aspects of a request including, headers, query string parameters, timeout settings, the body of a request, and much more. + +```php +$options = [ + 'debug' => true, + 'sink' => '/path/to/dump/file', +]; +$response = $youtrack->request('POST', '/issue', [ + 'project' => 'TEST', + 'summary' => 'New test issue', + 'description' => 'Test description', +], $options); +``` + #### HTTP GET request ```php @@ -142,7 +168,7 @@ $response = $youtrack->post('/issue', [ ```php $response = $youtrack->put('/issue/TEST-1', [ 'summary' => 'Updated summary', - 'description' => Updated description, + 'description' => 'Updated description', ]); ``` @@ -158,25 +184,29 @@ Each successful request to the API returns instance of `\Cog\YouTrack\Rest\Respo #### Get PSR HTTP response -PSR HTTP response could be accessed by calling `getResponse` method on API Response. +PSR HTTP response could be accessed by calling `httpResponse` method on API Response. ```php $youtrackResponse = $youtrack->get('/issue/TEST-1'); -$psrResponse = $youtrackResponse->getResponse(); +$psrResponse = $youtrackResponse->httpResponse(); ``` #### Get response Cookie +Returns `Set-Cookie` headers as string from the HTTP response. + ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$cookieString = $apiResponse->getCookie(); +$cookieString = $apiResponse->cookie(); ``` #### Get response Location +Returns `Location` header from the HTTP response. + ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$location = $apiResponse->getLocation(); +$location = $apiResponse->location(); ``` #### Transform response to array @@ -190,7 +220,7 @@ $location = $apiResponse->toArray(); ```php $apiResponse = $youtrack->get('/issue/TEST-1'); -$location = $apiResponse->getStatusCode(); +$location = $apiResponse->statusCode(); ``` ## Change log diff --git a/composer.json b/composer.json index f356768..07f86b6 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "docs": "https://github.com/cybercog/laravel-youtrack-sdk/wiki" }, "require": { - "cybercog/youtrack-php-sdk": "dev-master", + "cybercog/youtrack-php-sdk": "^1.0", "illuminate/support": "~5.1.20|~5.2.0|~5.3.0|~5.4.0", "php": "^7.1" }, diff --git a/src/Providers/YouTrackServiceProvider.php b/src/Providers/YouTrackServiceProvider.php index ff85753..5074e38 100644 --- a/src/Providers/YouTrackServiceProvider.php +++ b/src/Providers/YouTrackServiceProvider.php @@ -13,9 +13,11 @@ namespace Cog\Laravel\YouTrack\Providers; +use Cog\YouTrack\Rest\Authenticator\CookieAuthenticator; use Cog\YouTrack\Rest\Authorizer\Contracts\Authorizer as AuthorizerContract; use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; use Cog\YouTrack\Rest\Client\YouTrackClient; +use Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient; use GuzzleHttp\Client as HttpClient; use Illuminate\Contracts\Config\Repository as ConfigContract; use Illuminate\Support\ServiceProvider; @@ -49,11 +51,11 @@ public function register(): void $this->app->bind(ClientContract::class, function () { $config = $this->app->make(ConfigContract::class); - $http = new HttpClient([ + $httpClient = new GuzzleHttpClient(new HttpClient([ 'base_uri' => $config->get('youtrack.base_uri'), - ]); + ])); - return new YouTrackClient($http, $this->resolveAuthorizer($config)); + return new YouTrackClient($httpClient, $this->resolveAuthorizer($config)); }); } @@ -83,8 +85,15 @@ protected function bootConfig(): void */ protected function resolveAuthorizer(ConfigContract $config): AuthorizerContract { - $options = $config->get('youtrack.authorizers.' . $config->get('youtrack.authorizer')); + $authorizer = $config->get('youtrack.authorizer'); - return new $options['driver']($options); + $options = $config->get('youtrack.authorizers.' . $authorizer); + if ($authorizer == 'cookie') { + return new $options['driver']( + new CookieAuthenticator($options['username'], $options['password']) + ); + } + + return new $options['driver']($options['token']); } }