Skip to content

Commit

Permalink
Refactor how HTTP requests are made
Browse files Browse the repository at this point in the history
  • Loading branch information
Blitheness committed Mar 31, 2022
1 parent ce0d563 commit a90c3c0
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions app/services/PlayerDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,17 @@ public function __construct(string $world, ?string $directory = null)

public function processPlayerData()
{
$promises = [];

// Advancements
$advancementPaths = $this->_advancementsRepo->getAdvancementFilePaths();
foreach($advancementPaths as $path) {
$uuid = $this->getUuidFromPath($path);
$advancements = $this->_advancementsRepo->getAdvancementsForPlayer($uuid);
$payload = [
'player_uuid' => $uuid,
'data' => $advancements,
];
$promises[] = $this->_httpClient->requestAsync(
'PUT',
env('ADVANCEMENTS_ENDPOINT'),
[RequestOptions::JSON => $payload]
$this->makeJsonPutRequest(
env('ADVANCEMENTS_ENDPOINT'),
[
'player_uuid' => $uuid,
'data' => $advancements,
]
);
}

Expand All @@ -68,37 +64,38 @@ public function processPlayerData()
foreach($statisticsPaths as $path) {
$uuid = $this->getUuidFromPath($path);
$statistics = $this->_statisticsRepo->getStatisticsForPlayer($uuid);
$payload = [
'player_uuid' => $uuid,
'data' => $statistics,
];
$promises[] = $this->_httpClient->requestAsync(
'PUT',
$this->makeJsonPutRequest(
env('STATISTICS_ENDPOINT'),
[RequestOptions::JSON => $payload]
[
'player_uuid' => $uuid,
'data' => $statistics,
]
);
}
}

// Wait for API calls to complete
try
{
$promiseIterator = new EachPromise($promises, [
'concurrency' => 2,
'rejected' => function(RequestException $e) {
logger()->error('HTTP Request Failed', [
'method' => $e->getRequest()->getMethod(),
'target' => $e->getRequest()->getRequestTarget(),
'status_code' => $e->getResponse()->getStatusCode(),
'error_message' => $e->getMessage()
]);
}
]);
$promiseIterator->promise()->wait();
}
catch (Exception $e)
{
logger()->error('HTTP Client Error', [$e->getMessage()]);
}
private function makeJsonPutRequest(string $endpoint, array $payload): void
{
$this->_httpClient
->putAsync(
$endpoint,
[RequestOptions::JSON => $payload]
)
->then(
null,
function(RequestException $e) { $this->logFailedRequest($e); }
)
->wait();
}

private function logFailedRequest(RequestException $exception): void
{
logger()->error('HTTP Request Failed', [
'method' => $exception->getRequest()->getMethod(),
'target' => $exception->getRequest()->getRequestTarget(),
'status_code' => $exception->getResponse()->getStatusCode(),
'error_message' => $exception->getMessage()
]);
}

private function getUuidFromPath(string $path): string
Expand Down

0 comments on commit a90c3c0

Please sign in to comment.