Skip to content

Commit

Permalink
Merge branch 'release/1.0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstein committed Feb 1, 2022
2 parents 273a104 + a073059 commit f0c54e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Cloudflare Changelog

## 1.0.3.1 - 2022-01-31
### Fixed
- Fixed a bug where only the zone cache purge would log a `200`-status failure response. (Now applies to all other API requests.)

## 1.0.3 - 2022-01-29
### Fixed
- Unsuccessful, `200`-status API responses will log returned messages instead of throwing exceptions. ([#44](https://github.com/workingconcept/cloudflare-craft-plugin/issues/44))
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "workingconcept/craft-cloudflare",
"description": "Purge Cloudflare caches from Craft.",
"type": "craft-plugin",
"version": "1.0.3",
"version": "1.0.3.1",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudflare-craft-plugin",
"version": "1.0.3",
"version": "1.0.3.1",
"description": "![Cloudflare](resources/hero.svg)",
"main": "index.js",
"scripts": {
Expand Down
63 changes: 34 additions & 29 deletions src/services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Api extends Component
private $_connectionErrors = [];

/**
* Get a configured Guzzle client if we have an API key and email. Otherwise
* returns null.
* Get a configured Guzzle client if we have an API key and email.
* Otherwise, returns `null`.
*
* @return Client|null
*/
Expand Down Expand Up @@ -86,25 +86,18 @@ public function verifyConnection(): bool

try {
$response = $this->getClient()->get($testUri);
$responseContents = Json::decode(
$response->getBody()->getContents(),
false
);

// should be 200 response containing `"success": true`
$success = $response->getStatusCode() === 200
&& $responseContents->success;
$responseBody = Json::decode($response->getBody(), false);

if ($success) {
if ($this->_isSuccessfulResponse($response, $responseBody)) {
return true;
}

if (isset($responseContents->errors)) {
$this->_connectionErrors = $responseContents->errors;
if (isset($responseBody->errors)) {
$this->_connectionErrors = $responseBody->errors;

Craft::info(sprintf(
'Connection test failed: %s',
Json::encode($responseContents->errors)
Json::encode($responseBody->errors)
), 'cloudflare');
} else {
Craft::info('Connection test failed.', 'cloudflare');
Expand Down Expand Up @@ -170,8 +163,8 @@ public function getZones(): ?array
$this->responseItems = [];

$currentPage = 0;
$totalPages = 1; // temporary
$perPage = 50;
$totalPages = 1; // temporary
$perPage = 50;

while ($currentPage < $totalPages) {
$currentPage++;
Expand Down Expand Up @@ -213,11 +206,12 @@ public function getZoneById(string $zoneId): ?object
$zoneId
));

if ( ! $response->getStatusCode() === 200)
{
$responseBody = Json::decode($response->getBody(), false);

if (! $this->_isSuccessfulResponse($response, $responseBody)) {
Craft::info(sprintf(
'Request failed: %s',
$response->getBody()
$responseBody->errors
), 'cloudflare');

return null;
Expand All @@ -238,8 +232,7 @@ public function getZoneById(string $zoneId): ?object
'cloudflare'
);

return Json::decode($response->getBody(), false)
->result;
return $responseBody->result;
}

/**
Expand All @@ -265,15 +258,15 @@ public function purgeZoneCache()

$responseBody = Json::decode($response->getBody(), false);

if ($response->getStatusCode() !== 200 || $responseBody->success === false) {
if (! $this->_isSuccessfulResponse($response, $responseBody)) {
Craft::info(sprintf(
'Zone purge request failed: %s',
Json::encode($responseBody)
$responseBody->errors
), 'cloudflare');

return (object) [
'success' => false,
'message' => $response->getBody()->getContents(),
'message' => $responseBody,
'result' => []
];
}
Expand Down Expand Up @@ -323,15 +316,15 @@ public function purgeUrls(array $urls = [])

$responseBody = Json::decode($response->getBody(), false);

if ( ! $response->getStatusCode() === 200) {
if (! $this->_isSuccessfulResponse($response, $responseBody)) {
Craft::info(sprintf(
'Request failed: %s',
Json::encode($responseBody)
Json::encode($responseBody->errors)
), 'cloudflare');

return (object) [
'success' => false,
'message' => $response->getBody()->getContents(),
'message' => $responseBody,
'result' => []
];
}
Expand Down Expand Up @@ -361,16 +354,28 @@ public function getApiBaseUrl(): string
return self::API_BASE_URL;
}

/**
* Returns `true` if the provided response status and data indicate success.
*
* @param $response
* @param $responseBody
* @return bool
*/
private function _isSuccessfulResponse($response, $responseBody): bool
{
return $response->getStatusCode() === 200 && $responseBody->success === true;
}

/**
* Quietly handle an exception from the Cloudflare API.
*
* @param mixed $exception (ClientException or RequestException)
* @param string $action human-friendly description of the attempted action
* @param array $urls related URLs (if relevant)
*
* @return \stdClass with populated `result` property array
* @return object with populated `result` property array
*/
private function _handleApiException($exception, string $action, $urls = []): \stdClass
private function _handleApiException($exception, string $action, $urls = []): object
{
if ($responseBody = Json::decode($exception->getResponse()->getBody(), false)) {
$message = "${action} failed.\n";
Expand Down

0 comments on commit f0c54e4

Please sign in to comment.