Skip to content

Commit

Permalink
Extract Http Client from Rest Client (#20)
Browse files Browse the repository at this point in the history
Extract HttpClient from Rest Client
  • Loading branch information
Pe Ell authored May 22, 2017
1 parent 2a44e24 commit baf7089
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 77 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ All notable changes to `youtrack-rest-php` will be documented in this file.
### Added

- `Authenticator` contract and `CookieAuthenticator` implementation.
- `HttpClient` contract and `GuzzleHttpClient` implementation.
- `isStatusCode` assert in `Response` contract.

### Updated

- `CookieAuthorizer` constructor accepts `Authenticator` instead of credentials.
- `TokenAuthorizer` constructor accepts string token instead of array.
- `Authorizer` delegates authentication to `Authenticator`.
- `Client` delegates HTTP requests to `HttpClient`.
- Changed namespace of `AuthenticationException`.
- `getHeaders` method was dropped from `Authorizer` contract.
- `Response` interface methods `getResponse`, `getStatusCode`, `getCookie`, `getLocation` were renamed to `httpResponse`, `statusCode`, `cookie`, `location` respectively.
- `User-Agent` header is more verbose.
- REST Client version is defined in `Client` contract instead of each concrete implementation.

## [2.0.1] - 2017-05-21

Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +99,44 @@ require_once '/path/to/your-project/vendor/autoload.php';
Starting with YouTrack 2017.1 release [authorization based on permanent tokens](https://www.jetbrains.com/help/youtrack/standalone/2017.2/Manage-Permanent-Token.html) is recommended as the main approach for the authorization in your REST API calls.

```php
// Instantiate HTTP Client
$http = new \GuzzleHttp\Client([
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com',
]);

// Instantiate YouTrack API HTTP Client
$httpClient = new \Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient($psrHttpClient);

// Instantiate YouTrack API Token Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer('YOUTRACK_API_TOKEN');

// Instantiate YouTrack API Client
$youtrack = new \Cog\YouTrack\Rest\YouTrackClient($http, $authorizer);
$youtrack = new \Cog\YouTrack\Rest\YouTrackClient($httpClient, $authorizer);
```

#### Cookie authorization

```php
// Instantiate HTTP Client
$http = new \GuzzleHttp\Client([
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com',
]);

// Instantiate YouTrack API HTTP Client
$httpClient = new \Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient($psrHttpClient);

// Instantiate YouTrack API Cookie Authenticator
$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator('YOUTRACK_USERNAME', 'YOUTRACK_PASSWORD');

// Instantiate YouTrack API Cookie Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator);

// Instantiate YouTrack API Client
$youtrack = new \Cog\YouTrack\Rest\YouTrackClient($http, $authorizer);
$youtrack = new \Cog\YouTrack\Rest\YouTrackClient($httpClient, $authorizer);
```

### API requests


#### HTTP GET request

```php
Expand All @@ -153,7 +158,7 @@ $response = $youtrack->post('/issue', [
```php
$response = $youtrack->put('/issue/TEST-1', [
'summary' => 'Updated summary',
'description' => Updated description,
'description' => 'Updated description',
]);
```

Expand Down
20 changes: 13 additions & 7 deletions examples/cookie-authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@
// Boot third party libraries
require_once __DIR__ . '/../vendor/autoload.php';

use Cog\YouTrack\Rest;

// Application configuration (replace with your YouTrack server values)
$apiBaseUri = 'https://write-youtrack-domain.here';
$apiUsername = 'YOUR_USERNAME';
$apiPassword = 'YOUR_PASSWORD';
$apiUsername = 'YOUTRACK_USERNAME';
$apiPassword = 'YOUTRACK_PASSWORD';

// Instantiate HTTP Client
$http = new \GuzzleHttp\Client([
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => $apiBaseUri,
'debug' => true,
]);

// Instantiate YouTrack API HTTP Client
$httpClient = new Rest\HttpClient\GuzzleHttpClient($psrHttpClient);

// Instantiate YouTrack API Cookie Authenticator
$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator($apiUsername, $apiPassword);
$authenticator = new Rest\Authenticator\CookieAuthenticator($apiUsername, $apiPassword);

// Instantiate YouTrack API Cookie Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator);
$authorizer = new Rest\Authorizer\CookieAuthorizer($authenticator);

// Instantiate YouTrack API Client
$client = new \Cog\YouTrack\Rest\Client\YouTrackClient($http, $authorizer);
$client = new Rest\Client\YouTrackClient($httpClient, $authorizer);

// Do request to the API
$response = $client->get('/admin/project');
Expand Down
16 changes: 11 additions & 5 deletions examples/token-authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@
// Boot third party libraries
require_once __DIR__ . '/../vendor/autoload.php';

use Cog\YouTrack\Rest;

// Application configuration (replace with your YouTrack server values)
$apiBaseUri = 'https://write-youtrack-domain.here';
$apiAuthToken = 'WRITE_YOUR_TOKEN_HERE';
$apiToken = 'YOUTRACK_PERMANENT_TOKEN';

// Instantiate HTTP Client
$http = new \GuzzleHttp\Client([
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => $apiBaseUri,
'debug' => true,
]);

// Instantiate YouTrack API HTTP Client
$httpClient = new Rest\HttpClient\GuzzleHttpClient($psrHttpClient);

// Instantiate YouTrack API Token Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer($apiAuthToken);
$authorizer = new Rest\Authorizer\TokenAuthorizer($apiToken);

// Instantiate YouTrack API Client
$client = new \Cog\YouTrack\Rest\Client\YouTrackClient($http, $authorizer);
$client = new Rest\Client\YouTrackClient($httpClient, $authorizer);

// Do request to the API
$response = $client->get('/admin/project');
Expand Down
4 changes: 2 additions & 2 deletions src/Authenticator/Exceptions/AuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Cog\YouTrack\Rest\Authenticator\Exceptions;

use Cog\YouTrack\Rest\Authorizer\Exceptions\AuthorizationException;
use Cog\YouTrack\Rest\Client\Exceptions\ClientException;

/**
* Class AuthenticationException.
*
* @package Cog\YouTrack\Rest\Authenticator\Exceptions
*/
class AuthenticationException extends AuthorizationException
class AuthenticationException extends ClientException
{
//
}
4 changes: 2 additions & 2 deletions src/Authorizer/Exceptions/AuthorizationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Cog\YouTrack\Rest\Authorizer\Exceptions;

use Exception;
use Cog\YouTrack\Rest\Client\Exceptions\ClientException;

/**
* Class AuthorizationException.
*
* @package Cog\YouTrack\Rest\Authorizer\Exceptions
*/
class AuthorizationException extends Exception
class AuthorizationException extends ClientException
{
//
}
42 changes: 32 additions & 10 deletions src/Client/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,76 @@
*/
interface Client
{
/**
* Version of YouTrack REST PHP client.
*/
const VERSION = '3.0.0';

/**
* Create and send an HTTP request.
*
* @param string $method
* @param string $uri
* @param array $formData
* @param array $options
* @return \Cog\YouTrack\Rest\Response\Contracts\Response
*
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
* @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException
* @throws \Cog\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function request(string $method, string $uri, array $formData = []) : ResponseContract;
public function request(string $method, string $uri, array $options = []) : ResponseContract;

/**
* Create and send an GET HTTP request.
*
* @param string $uri
* @param array $formData
* @param array $options
* @return \Cog\YouTrack\Rest\Response\Contracts\Response
*
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
* @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException
* @throws \Cog\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function get(string $uri, array $formData = []): ResponseContract;
public function get(string $uri, array $options = []): ResponseContract;

/**
* Create and send an POST HTTP request.
*
* @param string $uri
* @param array $formData
* @param array $options
* @return \Cog\YouTrack\Rest\Response\Contracts\Response
*
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
* @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException
* @throws \Cog\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function post(string $uri, array $formData = []): ResponseContract;
public function post(string $uri, array $options = []): ResponseContract;

/**
* Create and send an PUT HTTP request.
*
* @param string $uri
* @param array $formData
* @param array $options
* @return \Cog\YouTrack\Rest\Response\Contracts\Response
*
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
* @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException
* @throws \Cog\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function put(string $uri, array $formData = []): ResponseContract;
public function put(string $uri, array $options = []): ResponseContract;

/**
* Create and send an DELETE HTTP request.
*
* @param string $uri
* @param array $formData
* @param array $options
* @return \Cog\YouTrack\Rest\Response\Contracts\Response
*
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
* @throws \Cog\YouTrack\Rest\Authorizer\Exceptions\InvalidTokenException
* @throws \Cog\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function delete(string $uri, array $formData = []): ResponseContract;
public function delete(string $uri, array $options = []): ResponseContract;

/**
* Write header value.
Expand Down
26 changes: 26 additions & 0 deletions src/Client/Exceptions/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of YouTrack REST PHP.
*
* (c) Anton Komarev <a.komarev@cybercog.su>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cog\YouTrack\Rest\Client\Exceptions;

use Exception;

/**
* Class ClientException.
*
* @package Cog\YouTrack\Rest\Client\Exceptions
*/
class ClientException extends Exception
{
//
}
Loading

0 comments on commit baf7089

Please sign in to comment.