-
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.
Add
RequestTracker
and TrackingGuzzleClientFactory
When steps need to execute HTTP requests without the `HttpLoader` from the crawler package (for example when using some REST API SDK), developers are encouraged to utilize either a Guzzle Client instance generated by the `TrackingGuzzleClientFactory` or invoke the `trackHttpResponse()` or `trackHeadlessBrowserResponse()` methods of the `RequestTracker` manually after each request. This enables seamless tracking of requests within the crwl.io app.
- Loading branch information
Showing
25 changed files
with
576 additions
and
40 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
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
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,50 @@ | ||
<?php | ||
|
||
namespace Crwlr\CrwlExtensionUtils; | ||
|
||
use Closure; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class RequestTracker | ||
{ | ||
/** | ||
* @var Closure[] | ||
*/ | ||
private array $onHttpResponse = []; | ||
|
||
/** | ||
* @var Closure[] | ||
*/ | ||
private array $onHeadlessBrowserResponse = []; | ||
|
||
public function onHttpResponse(Closure $closure): self | ||
{ | ||
$this->onHttpResponse[] = $closure; | ||
|
||
return $this; | ||
} | ||
|
||
public function onHeadlessBrowserResponse(Closure $closure): self | ||
{ | ||
$this->onHeadlessBrowserResponse[] = $closure; | ||
|
||
return $this; | ||
} | ||
|
||
public function trackHttpResponse(?RequestInterface $request = null, ?ResponseInterface $response = null): void | ||
{ | ||
foreach ($this->onHttpResponse as $closure) { | ||
$closure->call($this, $request, $response); | ||
} | ||
} | ||
|
||
public function trackHeadlessBrowserResponse( | ||
?RequestInterface $request = null, | ||
?ResponseInterface $response = null | ||
): void { | ||
foreach ($this->onHeadlessBrowserResponse as $closure) { | ||
$closure->call($this, $request, $response); | ||
} | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace Crwlr\CrwlExtensionUtils; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class TrackingGuzzleClientFactory | ||
{ | ||
public function __construct(private readonly RequestTracker $requestTracker) {} | ||
|
||
/** | ||
* @param mixed[] $withOptions | ||
*/ | ||
public function getClient(array $withOptions = []): Client | ||
{ | ||
$stack = array_key_exists('handler', $withOptions) ? $withOptions['handler'] : HandlerStack::create(); | ||
|
||
$stack->push(Middleware::mapResponse(function (ResponseInterface $response) { | ||
$this->requestTracker->trackHttpResponse(response: $response); | ||
|
||
return $response; | ||
})); | ||
|
||
$withOptions['handler'] = $stack; | ||
|
||
return new Client($withOptions); | ||
} | ||
} |
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,21 @@ | ||
providers: | ||
# - Workbench\App\Providers\WorkbenchServiceProvider | ||
|
||
migrations: | ||
- workbench/database/migrations | ||
|
||
seeders: | ||
- Workbench\Database\Seeders\DatabaseSeeder | ||
|
||
workbench: | ||
start: '/' | ||
install: true | ||
discovers: | ||
web: true | ||
api: false | ||
commands: false | ||
components: false | ||
views: false | ||
build: [] | ||
assets: [] | ||
sync: [] |
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
Oops, something went wrong.