generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ControlD.php will make HTTP requests - ControlDFactory.php creates the client class - Bind the ControlD client class and build it with the factory in ControldServiceProvider.php. Used for dependency injection - Add tests for the factory - Use TestMiddleware.php to assert all methods - Define API endpoint and secret bearer token in .env and config - Installed guzzlehttp/guzzle to support the underlying Laravel HTTP client - Set up test coverage in run-tests.yml - Set minimum test coverage to 100 percent - Fix unused autoload path in composer.json
- Loading branch information
Showing
12 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CONTROL_D_API_URL= | ||
CONTROL_D_API_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ phpstan.neon | |
testbench.yaml | ||
vendor | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Api; | ||
|
||
use Illuminate\Http\Client\PendingRequest; | ||
|
||
class ControlD | ||
{ | ||
public function __construct(private PendingRequest $request) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Api; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Http\Client\PendingRequest; | ||
|
||
class ControlDFactory | ||
{ | ||
public function __construct(private PendingRequest $request, private Repository $config) | ||
{ | ||
} | ||
|
||
public function make(): ControlD | ||
{ | ||
$this->request | ||
->asJson() | ||
->acceptJson() | ||
->baseUrl($this->config->get('controld.url')) | ||
->withToken($this->config->get('controld.secret')) | ||
->retry(3, 250, new RetryCallback()); | ||
|
||
foreach ($this->config->get('controld.middleware') ?? [] as $middleware) { | ||
$this->request->withMiddleware(app($middleware)); | ||
} | ||
|
||
return new ControlD($this->request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Api; | ||
|
||
use Illuminate\Http\Client\ConnectionException; | ||
use Throwable; | ||
|
||
class RetryCallback | ||
{ | ||
public function __invoke(Throwable $exception): bool | ||
{ | ||
return $exception instanceof ConnectionException || $exception->getCode() >= 500; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Http\Client\PendingRequest; | ||
use Rapkis\Controld\Api\ControlDFactory; | ||
use Rapkis\Controld\Api\RetryCallback; | ||
use Rapkis\Controld\Tests\Api\TestMiddleware; | ||
|
||
it('creates an api client', function () { | ||
$config = app(Repository::class); | ||
$request = $this->createMock(PendingRequest::class); | ||
$factory = new ControlDFactory($request, $config); | ||
app()->bind(TestMiddleware::class, fn () => $this->createStub(TestMiddleware::class)); | ||
|
||
$config->set([ | ||
'controld' => [ | ||
'url' => 'example.com', | ||
'secret' => 'bearer_token', | ||
'middleware' => [TestMiddleware::class], | ||
], | ||
]); | ||
|
||
$request->expects($this->once())->method('asJson')->willReturnSelf(); | ||
$request->expects($this->once())->method('acceptJson')->willReturnSelf(); | ||
$request->expects($this->once())->method('baseUrl')->with('example.com')->willReturnSelf(); | ||
$request->expects($this->once())->method('withToken')->with('bearer_token')->willReturnSelf(); | ||
|
||
$request->expects($this->once())->method('withMiddleware') | ||
->with($this->createStub(TestMiddleware::class)) | ||
->willReturnSelf(); | ||
|
||
$request->expects($this->once())->method('retry') | ||
->with(3, 250, new RetryCallback()) | ||
->willReturnSelf(); | ||
|
||
$factory->make(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Http\Client\ConnectionException; | ||
use Rapkis\Controld\Api\RetryCallback; | ||
|
||
it('will retry for exception', function (Throwable $exception, bool $willRetry) { | ||
$retry = new RetryCallback(); | ||
|
||
expect($retry($exception))->toBe($willRetry); | ||
})->with([ | ||
[new Exception(), false], | ||
[new Exception('', 500), true], | ||
[new Exception('', 422), false], | ||
[new ConnectionException(), true], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Rapkis\Controld\Tests\Api; | ||
|
||
use Closure; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class TestMiddleware | ||
{ | ||
public function __invoke(callable $handler): Closure | ||
{ | ||
return function (RequestInterface $request, array $options = []) use ($handler) { | ||
$request->withHeader('test', 'test'); | ||
|
||
return $handler($request, $options); | ||
}; | ||
} | ||
} |