Skip to content

Commit

Permalink
feat: upgraded composer library and handle response interface generally
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanparasteh committed Oct 16, 2022
1 parent a3fb840 commit 214383a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.DS_Store/
.DS_Store

.idea/
.vscode/

vendor/

composer.lock
composer.lock
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"require": {
"php": ">=8.0.0",
"react/http": "^1.6.0",
"clue/socks-react": "^1.3.0",
"react/http": "^1.8.0",
"clue/socks-react": "^1.4.0",
"react/promise-timer": "^1.9.0"
},
"require-dev": {
Expand Down
31 changes: 23 additions & 8 deletions src/AsyncRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,29 @@ private function request(string $url, array $params = [], array $headers = [], s

// Added Request Timeout
return timeout($req, $this->timeout)->then(
function (ResponseInterface $response) use ($canResponseDecode) {
function ($response) use ($canResponseDecode) {
if($response instanceof ResponseInterface){
return [
'result' => true,
'code' => $response->getStatusCode(),
'headers' => $response->getHeaders(),
'body' => $canResponseDecode
? json_decode($response->getBody()->getContents(), true)
: $response->getBody()->getContents()
];
}

if($response instanceof ResponseException){
return [
'result' => false,
'code' => $response->getCode(),
'body' => $response->getTraceAsString()
];
}

return [
'result' => true,
'code' => $response->getStatusCode(),
'headers' => $response->getHeaders(),
'body' => $canResponseDecode
? json_decode($response->getBody()->getContents(), true)
: $response->getBody()->getContents()
'result' => false,
'code' => 100_000,
];
},
function ($error) use ($canResponseDecode) {
Expand All @@ -159,4 +174,4 @@ function ($error) use ($canResponseDecode) {
}
);
}
}
}

0 comments on commit 214383a

Please sign in to comment.