Skip to content

Commit b8942a7

Browse files
committed
Rate limit api
1 parent 1ece9a8 commit b8942a7

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"require": {
1515
"php": "^8.0",
16-
"guzzlehttp/guzzle": "^7.3"
16+
"guzzlehttp/guzzle": "^7.3",
17+
"spatie/guzzle-rate-limiter-middleware": "^2.0"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": ">=9.0",

src/ConstantContact/Client.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function __construct(private string $clientAPIKey, private string $client
3535
// default to all scopes
3636
$this->scopes = \array_flip($this->validScopes);
3737
$this->host = $_SERVER['HTTP_HOST'] ?? '';
38+
$this->guzzleHandler = \GuzzleHttp\HandlerStack::create();
39+
$this->guzzleHandler->push(\Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware::perSecond(4));
3840
}
3941

4042
public function getBody() : string
@@ -49,7 +51,7 @@ public function next() : array
4951
return [];
5052
}
5153

52-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
54+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
5355
$response = $guzzle->request('GET', 'https://api.cc.email' . $this->next);
5456

5557
return $this->process($response);
@@ -177,12 +179,16 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ar
177179
try
178180
{
179181
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
180-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders([
181-
'Connection' => 'keep-alive',
182-
'Content-Length' => \strlen($json),
183-
'Accept-Encoding' => 'gzip, deflate',
184-
'Host' => $this->host,
185-
'Accept' => '*/*']),
182+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(
183+
[
184+
'Connection' => 'keep-alive',
185+
'Content-Length' => \strlen($json),
186+
'Accept-Encoding' => 'gzip, deflate',
187+
'Host' => $this->host,
188+
'Accept' => '*/*'
189+
]
190+
),
191+
'handler' => $this->guzzleHandler,
186192
'body' => $json, ]);
187193

188194
$response = $guzzle->request($method, $url);
@@ -202,7 +208,7 @@ public function delete(string $url) : bool
202208
{
203209
try
204210
{
205-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
211+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
206212
$response = $guzzle->request('DELETE', $url);
207213

208214
$this->process($response);
@@ -233,7 +239,7 @@ public function get(string $url, array $parameters) : array
233239
}
234240
}
235241

236-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders()]);
242+
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
237243
$response = $guzzle->request('GET', $url);
238244

239245
return $this->process($response);
@@ -253,6 +259,7 @@ public function post(string $url, array $parameters) : array
253259
{
254260
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
255261
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(),
262+
'handler' => $this->guzzleHandler,
256263
'body' => $json, ]);
257264
$response = $guzzle->request('POST', $url);
258265

0 commit comments

Comments
 (0)