Skip to content

Commit

Permalink
Using PSR-18 (#17)
Browse files Browse the repository at this point in the history
* Using PSR-18

* Removing custom interface dependency

* Testing with bbaga/buildkite-php-guzzle-client:2.0-RC

* Upgrading to bbaga/buildkite-php-guzzle-client:2.0

* README update
  • Loading branch information
bbaga authored Jun 1, 2020
1 parent 9d44d04 commit f182a53
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ composer require bbaga/buildkite-php
```php
use bbaga\BuildkiteApi\Api\RestApi;

/** @var \bbaga\BuildkiteApi\Api\HttpClientInterface $client */
/** @var \Psr\Http\Client\ClientInterface $client */
$client = new MyHttpClient();

$api = new RestApi($client, 'MY_BUILDKITE_API_TOKEN');
```

`\bbaga\\BuildkiteApi\Api\HttpClientInterface` implementation is available in the [`bbaga/buildkite-php-guzzle-client`](https://github.com/bbaga/buildkite-php-guzzle-client) package.

`\bbaga\BuildkiteApi\Api\HttpClientInterface` is available in the [`bbaga/buildkite-php-http-interface`](https://github.com/bbaga/buildkite-php-http-interface) package.
`\Psr\Http\Client\ClientInterface` implementation is available in the [`bbaga/buildkite-php-guzzle-client`](https://github.com/bbaga/buildkite-php-guzzle-client) package.

### Interacting with Buildkite's REST API

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
],
"require": {
"ext-json": "*",
"bbaga/buildkite-php-http-interface": "^1.0",
"guzzlehttp/psr7": "^1.6"
"guzzlehttp/psr7": "^1.6",
"psr/http-client": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,8 +37,8 @@
"phpunit/phpunit": "^8.5",
"vimeo/psalm": "^3.11",
"psalm/plugin-phpunit": "^0.10.0",
"bbaga/buildkite-php-guzzle-client": "^1.0",
"symplify/easy-coding-standard": "^7.2"
"symplify/easy-coding-standard": "^7.2",
"bbaga/buildkite-php-guzzle-client": "^2.0"
},
"scripts": {
"ecs-fix": {
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Rest/Artifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getDownloadUrl(
$jobId,
$artifactId
);
$response = $this->api->get($uri, ['allow_redirects' => false]);
$response = $this->api->get($uri);

return $this->api->getResponseBody($response);
}
Expand Down
46 changes: 31 additions & 15 deletions src/Api/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use bbaga\BuildkiteApi\Api\Rest\Pipeline;
use bbaga\BuildkiteApi\Api\Rest\PipelineInterface;
use bbaga\BuildkiteApi\Api\Rest\User;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use function GuzzleHttp\Psr7\stream_for;
Expand All @@ -28,7 +30,7 @@
final class RestApi implements RestApiInterface
{
/**
* @var HttpClientInterface
* @var ClientInterface
*/
private $client;

Expand All @@ -45,11 +47,11 @@ final class RestApi implements RestApiInterface
public const BASE_URI = 'https://api.buildkite.com/v2/';

/**
* @param HttpClientInterface $client
* @param ClientInterface $client
* @param string $accessToken Buildkite API Access Token
* @param string $uri Buildkite API uri
*/
public function __construct(HttpClientInterface $client, string $accessToken, string $uri = self::BASE_URI)
public function __construct(ClientInterface $client, string $accessToken, string $uri = self::BASE_URI)
{
$this->client = $client;
$this->accessToken = $accessToken;
Expand All @@ -70,11 +72,25 @@ public function getResponseBody(ResponseInterface $response): array

public function get(string $resource, array $options = []): ResponseInterface
{
$request = $this->client->createRequest('GET', sprintf('%s%s', $this->uri, $resource));
$query = '';

return $this->client->send(
$this->addHeaders($request),
$options
if (isset($options['query'])) {
if (!\is_array($options['query'])) {
throw new \InvalidArgumentException('query must be an array');
}

$query = '?' . \http_build_query(
$options['query'],
'',
'&',
PHP_QUERY_RFC3986
);
}

$request = new Request('GET', sprintf('%s%s%s', $this->uri, $resource, $query));

return $this->client->sendRequest(
$this->addHeaders($request)
);
}

Expand All @@ -87,35 +103,35 @@ private function addHeaders(RequestInterface $request): RequestInterface
public function post(string $resource, array $body = []): ResponseInterface
{
$options = count($body) === 0 ? JSON_FORCE_OBJECT : 0;
$request = $this->client->createRequest('POST', sprintf('%s%s', $this->uri, $resource))
$request = (new Request('POST', sprintf('%s%s', $this->uri, $resource)))
->withBody(stream_for(json_encode($body, $options)));

return $this->client->send($this->addHeaders($request));
return $this->client->sendRequest($this->addHeaders($request));
}

public function patch(string $resource, array $body = []): ResponseInterface
{
$options = count($body) === 0 ? JSON_FORCE_OBJECT : 0;
$request = $this->client->createRequest('PATCH', sprintf('%s%s', $this->uri, $resource))
$request = (new Request('PATCH', sprintf('%s%s', $this->uri, $resource)))
->withBody(stream_for(json_encode($body, $options)));

return $this->client->send($this->addHeaders($request));
return $this->client->sendRequest($this->addHeaders($request));
}

public function put(string $resource, array $body = []): ResponseInterface
{
$options = count($body) === 0 ? JSON_FORCE_OBJECT : 0;
$request = $this->client->createRequest('PUT', sprintf('%s%s', $this->uri, $resource))
$request = (new Request('PUT', sprintf('%s%s', $this->uri, $resource)))
->withBody(stream_for(json_encode($body, $options)));

return $this->client->send($this->addHeaders($request));
return $this->client->sendRequest($this->addHeaders($request));
}

public function delete(string $resource): ResponseInterface
{
$request = $this->client->createRequest('DELETE', sprintf('%s%s', $this->uri, $resource));
$request = new Request('DELETE', sprintf('%s%s', $this->uri, $resource));

return $this->client->send($this->addHeaders($request));
return $this->client->sendRequest($this->addHeaders($request));
}

public function organization(): OrganizationInterface
Expand Down
3 changes: 3 additions & 0 deletions tests/Integration/Api/Rest/Fluent/BuildRelatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function testBuildRelatedFunctions(): void
$this->assertCount(1, $artifacts);
/** @var Artifact $artifact */
$artifact = $artifacts[0];

$this->assertStringStartsWith('http', $artifact->getDownloadUrl());

$artifact->delete();
$this->assertEquals('deleted', $artifact->getState());

Expand Down
19 changes: 5 additions & 14 deletions tests/Unit/Api/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace bbaga\BuildkiteApi\Tests\Unit\Api;

use bbaga\BuildkiteApi\Api\HttpClientInterface;
use bbaga\BuildkiteApi\Api\RestApi;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -19,7 +18,7 @@ final class RestApiTest extends TestCase
{
public function testGetResponseBody(): void
{
$client = $this->prophesize(HttpClientInterface::class)->reveal();
$client = $this->prophesize(ClientInterface::class)->reveal();
$api = new RestApi($client, 'token');
$response = $this->prophesize(ResponseInterface::class);
$response->getBody()->willReturn('{"foo": "bar"}');
Expand All @@ -32,24 +31,16 @@ public function testGet(): void
$testCase = $this;
$token = 'my-token';

$client = $this->prophesize(HttpClientInterface::class);
$client->createRequest('GET', RestApi::BASE_URI . 'some/uri')->willReturn(
new Request('GET', RestApi::BASE_URI . 'some/uri')
);
$client = $this->prophesize(ClientInterface::class);

$client->send(
Argument::type(RequestInterface::class),
Argument::type('array')
$client->sendRequest(
Argument::type(RequestInterface::class)
)->will(function (array $args) use ($testCase, $token): ResponseInterface {
/** @var RequestInterface $request */
$request = $args[0];
/** @var array $options */
$options = $args[1];
$authHeader = $request->getHeader('Authorization');

$testCase->assertEquals($authHeader[0], sprintf('Bearer %s', $token), 'Auth header set');
$testCase->assertArrayHasKey('test', $options, 'Option key set');
$testCase->assertEquals('dummy', $options['test'], 'Option value set');

$response = $testCase->prophesize(ResponseInterface::class);
$response->withStatus()->willReturn(200);
Expand Down

0 comments on commit f182a53

Please sign in to comment.