Skip to content

Commit

Permalink
feat: Add HttpResultInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
il-masaru-yamagishi committed Oct 18, 2023
1 parent a0405d3 commit 0dd2eea
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Console/Commands/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->writeln(\sprintf('Start execution at %s', \date('Y-m-d H:i:s')));

foreach ($executor->execute($this->cancelToken) as $_) {
// Do nothing
// TODO: add progress
}

$io->writeln(
Expand Down
24 changes: 24 additions & 0 deletions src/Contracts/HttpResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @license MIT
*/

declare(strict_types=1);

namespace Heavyrain\Contracts;

use ArrayAccess;
use IteratorAggregate;
use JsonSerializable;
use Stringable;

/**
* HTTP profiling result
* for reduce memory and reference leaks, all data is stored in array
* @template-extends ArrayAccess<'request'|'response'|'requestException'|'uncaughtException', ?array<string, array<string, mixed>>>
* @template-extends IteratorAggregate<'request'|'response'|'requestException'|'uncaughtException', ?array<string, array<string, mixed>>>
*/
interface HttpResultInterface extends ArrayAccess, JsonSerializable, Stringable, IteratorAggregate
{
}
2 changes: 1 addition & 1 deletion src/Contracts/ReporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ReporterInterface
/**
* Reports HTTP profiling results
*
* @param array $results
* @param HttpResultInterface[] $results
* @return void
*/
public function report(array $results): void;
Expand Down
6 changes: 4 additions & 2 deletions src/HttpClient/AmphpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ private function handle(RequestInterface $request): ResponseInterface
try {
$ampResponse = $this->client->request($ampRequest, $this->cancellation);

$psrResponse = $this->toPsrResponse($ampResponse);

// Profiles with HTTP events.
$this->profiler->profile($ampRequest, $ampResponse);

return $psrResponse;
} catch (\Throwable $exception) {
// Profile exception during request
$this->profiler->profileException($ampRequest, $exception);

throw new RequestException('failed to fetch response', previous: $exception);
}

return $this->toPsrResponse($ampResponse);
}

/**
Expand Down
28 changes: 15 additions & 13 deletions src/HttpClient/HttpResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@

use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use ArrayAccess;
use JsonSerializable;
use Stringable;
use ArrayIterator;
use Heavyrain\Contracts\HttpResultInterface;
use Throwable;
use Traversable;

/**
* HTTP profiling result
* for reduce memory and reference leaks, all data is stored in array
* @template-implements ArrayAccess<string, array>
*/
final class HttpResult implements ArrayAccess, JsonSerializable, Stringable
final class HttpResult implements HttpResultInterface
{
public const START_KEY = 'start';
public const CONNECT_KEY = 'connect';
Expand All @@ -47,25 +46,28 @@ public function __construct(

public function offsetExists(mixed $offset): bool
{
return \in_array($offset, ['request', 'response', 'request_exception', 'uncaught_exception'], true);
return \array_key_exists($offset, $this->result);
}

public function offsetGet(mixed $offset): mixed
{
if (!\array_key_exists($offset, $this->result)) {
throw new \RuntimeException('invalid offset. supported: request, response, request_exception, uncaught_exception');
throw new \RuntimeException('invalid offset. supported: request, response, requestException, uncaughtException');
}
return $this->result[$offset];
}

public function offsetSet(mixed $offset, mixed $value): void
{
throw new \LogicException('cannot set result after created');
public function offsetSet(mixed $offset, mixed $value): void {
throw new \LogicException('HttpResult is immutable');
}

public function offsetUnset(mixed $offset): void {
throw new \LogicException('HttpResult is immutable');
}

public function offsetUnset(mixed $offset): void
public function getIterator(): Traversable
{
throw new \LogicException('cannot unset result after created');
return new ArrayIterator($this->result);
}

public function jsonSerialize(): mixed
Expand Down Expand Up @@ -173,7 +175,7 @@ private static function convertException(Throwable $exception): array
'class' => \get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'previousMessage' => $exception->getPrevious() ? $exception->getPrevious()->getMessage() : null,
'previousMessage' => $exception->getPrevious()?->getMessage(),
];
}
}
1 change: 0 additions & 1 deletion src/Reporters/TableReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __construct(private readonly SymfonyStyle $io)
public function report(array $results): void
{
$rows = [];
/** @var \Heavyrain\HttpClient\HttpResult $result */
foreach ($results as $result) {
$rows[] = [
'request' => \json_encode($result['request']),
Expand Down

0 comments on commit 0dd2eea

Please sign in to comment.