Skip to content

Commit

Permalink
Merge pull request #2 from 77web/fix-requester
Browse files Browse the repository at this point in the history
enabled handy usage for Client with readme
  • Loading branch information
77web authored Nov 12, 2023
2 parents 3499d05 + b00bedd commit d7358ac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# simple php client for sendgrid marketing campaign apis

## installation

```shell
composer require linkage/sendgrid-marketing-campaign-api-client
```

## usage

```php

$apiKey = 'get your api key from sendgrid admin screen';

$sendgridClient = new \Linkage\SendgridMarketingCampaignApiClient\Client(
new \Linkage\SendgridMarketingCampaignApiClient\SendgridApiRequester($apiKey),
);
try {
$sendgridClient->createContactList(
new \Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListRequest('my new contact list'),
);
} catch (\Linkage\SendgridMarketingCampaignApiClient\SendgridApiClientException $e) {
// handle client error
} catch (\Linkage\SendgridMarketingCampaignApiClient\SendgridApiServerException $e) {
// handle server error
}
```
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
readonly class Client implements ClientInterface
{
public function __construct(
private HttpRequester $requester,
private SendgridApiRequester $requester,
) {
}

Expand Down
41 changes: 38 additions & 3 deletions src/HttpRequester.php → src/SendgridApiRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,58 @@

namespace Linkage\SendgridMarketingCampaignApiClient;

use GuzzleHttp\ClientInterface as HttpClient;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use Symfony\Component\Serializer\SerializerInterface;

readonly class HttpRequester
readonly class SendgridApiRequester
{
private HttpClientInterface $httpClient;
private SerializerInterface $serializer;

public function __construct(
private HttpClient $httpClient,
string $apiKey,
HttpClientInterface|null $httpClient = null,
SerializerInterface|null $serializer = null,
) {
$this->httpClient = $httpClient ?? new HttpClient([
'base_uri' => 'https://api.sendgrid.com/v3',
'headers' => [
'Authorization' => sprintf('Bearer %s', $apiKey),
],
]);
$this->serializer = $serializer ?? (new SerializerFactory())->create();
}

/**
* @template T of object
*
* @param class-string<T> $responseClass
*
* @return T
*
* @throws SendgridApiClientException
* @throws SendgridApiServerException
*/
public function get(string $path, string $responseClass)
{
try {
$response = $this->httpClient->request('GET', $path);
} catch (ClientException $e) {
throw new SendgridApiClientException($e->getResponse()->getBody()->getContents(), previous: $e);
} catch (ServerException|GuzzleException $e) {
throw new SendgridApiServerException($e->getMessage(), code: $e->getCode(), previous: $e);
}

$result = $this->serializer->deserialize($response->getBody()->getContents(), $responseClass, 'json');
\assert(\is_object($result) && $result instanceof $responseClass);

return $result;
}

/**
* @template T of object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
use GuzzleHttp\Psr7\Response;
use Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListRequest;
use Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListResponse;
use Linkage\SendgridMarketingCampaignApiClient\HttpRequester;
use Linkage\SendgridMarketingCampaignApiClient\SendgridApiClientException;
use Linkage\SendgridMarketingCampaignApiClient\SendgridApiRequester;
use Linkage\SendgridMarketingCampaignApiClient\SendgridApiServerException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class HttpRequesterTest extends TestCase
class SendgridApiRequesterTest extends TestCase
{
private MockObject&ClientInterface $guzzleClientMock;

Expand Down Expand Up @@ -81,9 +81,10 @@ public function testPostServerError(): void
);
}

private function getSUT(): HttpRequester
private function getSUT(): SendgridApiRequester
{
return new HttpRequester(
return new SendgridApiRequester(
'test',
$this->guzzleClientMock,
);
}
Expand Down

0 comments on commit d7358ac

Please sign in to comment.