-
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.
Merge pull request #4 from telebugs/guzzle
Make it possible to send exceptions
- Loading branch information
Showing
10 changed files
with
237 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Telebugs; | ||
|
||
use GuzzleHttp\Promise\PromiseInterface; | ||
|
||
class Promise | ||
{ | ||
private $promise; | ||
|
||
public function __construct(PromiseInterface $promise) | ||
{ | ||
$this->promise = $promise; | ||
} | ||
|
||
public function then(...$args): Promise | ||
{ | ||
return new Promise($this->promise->then(...$args)); | ||
} | ||
|
||
public function otherwise(...$args): Promise | ||
{ | ||
return new Promise($this->promise->otherwise(...$args)); | ||
} | ||
|
||
public function wait(...$args): mixed | ||
{ | ||
return $this->promise->wait(...$args); | ||
} | ||
|
||
public function getState(): string | ||
{ | ||
return $this->promise->getState(); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace Telebugs; | ||
|
||
use GuzzleHttp; | ||
|
||
use Telebugs\Promise; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Sender | ||
{ | ||
private const CONTENT_TYPE = 'application/json'; | ||
|
||
private const USER_AGENT = 'telebugs-php/' . Reporter::VERSION . ' (php/' . PHP_VERSION . ')'; | ||
|
||
private $config; | ||
|
||
private $client; | ||
private $authorization; | ||
|
||
public function __construct() | ||
{ | ||
$this->config = Config::getInstance(); | ||
|
||
if ($this->config->getHttpClient() !== null) { | ||
$this->client = $this->config->getHttpClient(); | ||
} else { | ||
$this->client = new GuzzleHttp\Client(); | ||
} | ||
|
||
$this->authorization = 'Bearer ' . $this->config->getApiKey(); | ||
} | ||
|
||
public function send(string $data): Promise | ||
{ | ||
$guzzlePromise = $this->client->postAsync($this->config->getApiURL(), [ | ||
'body' => $data, | ||
'headers' => [ | ||
'Content-Type' => self::CONTENT_TYPE, | ||
'User-Agent' => self::USER_AGENT, | ||
'Authorization' => $this->authorization | ||
] | ||
]); | ||
|
||
$promise = new Promise($guzzlePromise); | ||
return $promise->then(function (ResponseInterface $res) { | ||
return json_decode($res->getBody()->getContents(), true); | ||
}); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Telebugs\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
use Telebugs\Promise; | ||
|
||
class PromiseTest extends TestCase | ||
{ | ||
public function testThen(): void | ||
{ | ||
$promise = new Promise(new \GuzzleHttp\Promise\FulfilledPromise(1)); | ||
$newPromise = $promise->then(function ($value) { | ||
return $value + 1; | ||
}); | ||
|
||
$this->assertEquals(2, $newPromise->wait()); | ||
} | ||
|
||
public function testOtherwise(): void | ||
{ | ||
$promise = new Promise(new \GuzzleHttp\Promise\RejectedPromise(1)); | ||
$newPromise = $promise->otherwise(function ($value) { | ||
return $value + 1; | ||
}); | ||
|
||
$this->assertEquals(2, $newPromise->wait()); | ||
} | ||
|
||
public function testWait(): void | ||
{ | ||
$promise = new Promise(new \GuzzleHttp\Promise\FulfilledPromise(1)); | ||
|
||
$this->assertEquals(1, $promise->wait()); | ||
} | ||
|
||
public function testGetState(): void | ||
{ | ||
$promise = new Promise(new \GuzzleHttp\Promise\FulfilledPromise(1)); | ||
|
||
$this->assertEquals("fulfilled", $promise->getState()); | ||
} | ||
} |
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