Skip to content

Commit 1af748d

Browse files
authored
Make sure AwsClient is useful (#184)
* Make sure AwsClient is useful * Dont extend AbstractClient * Rebase
1 parent f7ecf59 commit 1af748d

File tree

1 file changed

+83
-21
lines changed

1 file changed

+83
-21
lines changed

AwsClient.php

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,81 @@
55
namespace AsyncAws\Core;
66

77
use AsyncAws\CloudFormation\CloudFormationClient;
8+
use AsyncAws\Core\Credentials\CacheProvider;
9+
use AsyncAws\Core\Credentials\ChainProvider;
10+
use AsyncAws\Core\Credentials\ConfigurationProvider;
11+
use AsyncAws\Core\Credentials\CredentialProvider;
12+
use AsyncAws\Core\Credentials\IniFileProvider;
13+
use AsyncAws\Core\Credentials\InstanceProvider;
14+
use AsyncAws\Core\Exception\InvalidArgument;
815
use AsyncAws\Core\Exception\MissingDependency;
9-
use AsyncAws\Core\Exception\RuntimeException;
1016
use AsyncAws\Core\Sts\StsClient;
17+
use AsyncAws\Lambda\LambdaClient;
1118
use AsyncAws\S3\S3Client;
1219
use AsyncAws\Ses\SesClient;
20+
use AsyncAws\Sns\SnsClient;
1321
use AsyncAws\Sqs\SqsClient;
22+
use Psr\Log\LoggerInterface;
23+
use Psr\Log\NullLogger;
24+
use Symfony\Component\HttpClient\HttpClient;
25+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1426

1527
/**
1628
* Base API client that instantiate other API classes if needed.
1729
*
1830
* @author Tobias Nyholm <[email protected]>
1931
*/
20-
class AwsClient extends AbstractApi
32+
class AwsClient
2133
{
2234
/**
2335
* @var array
2436
*/
2537
private $serviceCache;
2638

39+
/**
40+
* @var HttpClientInterface
41+
*/
42+
private $httpClient;
43+
44+
/**
45+
* @var Configuration
46+
*/
47+
private $configuration;
48+
49+
/**
50+
* @var CredentialProvider
51+
*/
52+
private $credentialProvider;
53+
54+
/**
55+
* @var LoggerInterface|null
56+
*/
57+
private $logger;
58+
59+
/**
60+
* @param Configuration|array $configuration
61+
*/
62+
public function __construct($configuration = [], ?CredentialProvider $credentialProvider = null, ?HttpClientInterface $httpClient = null, ?LoggerInterface $logger = null)
63+
{
64+
if (\is_array($configuration)) {
65+
$configuration = Configuration::create($configuration);
66+
} elseif (!$configuration instanceof Configuration) {
67+
throw new InvalidArgument(sprintf('Second argument to "%s::__construct()" must be an array or an instance of "%s"', __CLASS__, Configuration::class));
68+
}
69+
70+
$this->httpClient = $httpClient ?? HttpClient::create();
71+
$this->logger = $logger ?? new NullLogger();
72+
$this->configuration = $configuration;
73+
$this->credentialProvider = $credentialProvider ?? new CacheProvider(new ChainProvider([
74+
new ConfigurationProvider(),
75+
new IniFileProvider($this->logger),
76+
new InstanceProvider($this->httpClient, $this->logger),
77+
]));
78+
}
79+
2780
public function cloudFormation(): CloudFormationClient
2881
{
29-
if (!class_exists(S3Client::class)) {
82+
if (!class_exists(CloudFormationClient::class)) {
3083
throw MissingDependency::create('async-aws/cloud-formation', 'CloudFormation');
3184
}
3285

@@ -37,14 +90,14 @@ public function cloudFormation(): CloudFormationClient
3790
return $this->serviceCache[__METHOD__];
3891
}
3992

40-
public function sts(): StsClient
93+
public function lambda(): LambdaClient
4194
{
42-
if (!class_exists(StsClient::class)) {
43-
throw MissingDependency::create('async-aws/core', 'STS');
95+
if (!class_exists(LambdaClient::class)) {
96+
throw MissingDependency::create('async-aws/lambda', 'Lambda');
4497
}
4598

4699
if (!isset($this->serviceCache[__METHOD__])) {
47-
$this->serviceCache[__METHOD__] = new StsClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
100+
$this->serviceCache[__METHOD__] = new LambdaClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
48101
}
49102

50103
return $this->serviceCache[__METHOD__];
@@ -76,33 +129,42 @@ public function ses(): SesClient
76129
return $this->serviceCache[__METHOD__];
77130
}
78131

79-
public function sqs(): SqsClient
132+
public function sns(): SnsClient
80133
{
81-
if (!class_exists(SqsClient::class)) {
82-
throw MissingDependency::create('async-aws/sqs', 'SQS');
134+
if (!class_exists(SnsClient::class)) {
135+
throw MissingDependency::create('async-aws/sns', 'SNS');
83136
}
84137

85138
if (!isset($this->serviceCache[__METHOD__])) {
86-
$this->serviceCache[__METHOD__] = new SqsClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
139+
$this->serviceCache[__METHOD__] = new SnsClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
87140
}
88141

89142
return $this->serviceCache[__METHOD__];
90143
}
91144

92-
protected function getServiceCode(): string
145+
public function sts(): StsClient
93146
{
94-
// This will never work on the base API. .
95-
throw new RuntimeException(sprintf('The $endpoint parameter is required on "%s::request()".', __CLASS__));
96-
}
147+
if (!class_exists(StsClient::class)) {
148+
throw MissingDependency::create('async-aws/core', 'STS');
149+
}
97150

98-
protected function getSignatureScopeName(): string
99-
{
100-
// This will never work on the base API. .
101-
throw new RuntimeException(\sprintf('The signing name is required on "%s".', __CLASS__));
151+
if (!isset($this->serviceCache[__METHOD__])) {
152+
$this->serviceCache[__METHOD__] = new StsClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
153+
}
154+
155+
return $this->serviceCache[__METHOD__];
102156
}
103157

104-
protected function getSignatureVersion(): string
158+
public function sqs(): SqsClient
105159
{
106-
return 'v4';
160+
if (!class_exists(SqsClient::class)) {
161+
throw MissingDependency::create('async-aws/sqs', 'SQS');
162+
}
163+
164+
if (!isset($this->serviceCache[__METHOD__])) {
165+
$this->serviceCache[__METHOD__] = new SqsClient($this->configuration, $this->credentialProvider, $this->httpClient, $this->logger);
166+
}
167+
168+
return $this->serviceCache[__METHOD__];
107169
}
108170
}

0 commit comments

Comments
 (0)