Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit b69e179

Browse files
committed
added async tasks for requests
1 parent 7a9cbaf commit b69e179

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Tasks;
4+
5+
use Exception;
6+
use FortniteApi\Components\HttpClient;
7+
use FortniteApi\Components\Objects\CreatorCode;
8+
use FortniteApi\FortniteApiError;
9+
use Psr\Http\Message\ResponseInterface as Response;
10+
11+
class CreatorCodeArrayTask extends Awaitable
12+
{
13+
/**
14+
* Awaits the response and returns the parsed body.
15+
*
16+
* @return null|CreatorCode[]|array
17+
*/
18+
public function await()
19+
{
20+
FortniteApiError::clearLastError();
21+
22+
try {
23+
/** @var Response $response */
24+
$response = parent::await();
25+
26+
if (empty($response)) {
27+
return null;
28+
}
29+
30+
$statusCode = $response->getStatusCode();
31+
32+
if (!HttpClient::isSuccess($statusCode)) {
33+
FortniteApiError::setLastError("Request failed.", $response);
34+
35+
return null;
36+
}
37+
38+
$body = $response->getBody();
39+
40+
if (empty($body)) {
41+
return CreatorCode::createObjectArray(null);
42+
}
43+
44+
$text = (string)$body;
45+
46+
return CreatorCode::createObjectArray($text);
47+
} catch (Exception $ex) {
48+
FortniteApiError::setLastError($ex->getMessage());
49+
50+
return null;
51+
}
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Tasks;
4+
5+
use Exception;
6+
use FortniteApi\Components\HttpClient;
7+
use FortniteApi\Components\Objects\CreatorCode;
8+
use FortniteApi\FortniteApiError;
9+
use Psr\Http\Message\ResponseInterface as Response;
10+
11+
class CreatorCodeTask extends Awaitable
12+
{
13+
/**
14+
* Awaits the response and returns the parsed body.
15+
*
16+
* @return null|CreatorCode
17+
*/
18+
public function await()
19+
{
20+
FortniteApiError::clearLastError();
21+
22+
try {
23+
/** @var Response $response */
24+
$response = parent::await();
25+
26+
if (empty($response)) {
27+
return null;
28+
}
29+
30+
$statusCode = $response->getStatusCode();
31+
32+
if (!HttpClient::isSuccess($statusCode)) {
33+
FortniteApiError::setLastError("Request failed.", $response);
34+
35+
return null;
36+
}
37+
38+
$body = $response->getBody();
39+
40+
if (empty($body)) {
41+
return CreatorCode::createObject(null);
42+
}
43+
44+
$text = (string)$body;
45+
46+
return CreatorCode::createObject($text);
47+
} catch (Exception $ex) {
48+
FortniteApiError::setLastError($ex->getMessage());
49+
50+
return null;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)