Skip to content

Commit

Permalink
add capability to offload images to optimole
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed May 14, 2024
1 parent 6ad4cf7 commit 2a090c3
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 2 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
},
"require": {
"php": ">=7.4",
"ext-json": "*",
"symfony/polyfill-php80": "^1.29"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"guzzlehttp/guzzle": "^7.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.6"
},
"suggest": {
"guzzlehttp/guzzle": "Use the Guzzle HTTP client to make requests to the API"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/BadResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class BadResponseException extends RuntimeException
{
}
18 changes: 18 additions & 0 deletions src/Exception/DashboardApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class DashboardApiException extends BadResponseException
{
}
18 changes: 18 additions & 0 deletions src/Exception/InvalidDashboardApiResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class InvalidDashboardApiResponseException extends DashboardApiException
{
}
18 changes: 18 additions & 0 deletions src/Exception/InvalidUploadApiResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class InvalidUploadApiResponseException extends UploadApiException
{
}
18 changes: 18 additions & 0 deletions src/Exception/UploadApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class UploadApiException extends BadResponseException
{
}
18 changes: 18 additions & 0 deletions src/Exception/UploadFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class UploadFailedException extends RuntimeException
{
}
18 changes: 18 additions & 0 deletions src/Exception/UploadLimitReachedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Exception;

class UploadLimitReachedException extends UploadApiException
{
}
22 changes: 22 additions & 0 deletions src/Http/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Http;

interface ClientInterface
{
/**
* Sends an HTTP request to the Optimole API and returns the JSON decoded body.
*/
public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array;
}
88 changes: 88 additions & 0 deletions src/Http/GuzzleClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Http;

use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Optimole\Sdk\Exception\BadResponseException;
use Optimole\Sdk\Exception\InvalidArgumentException;
use Optimole\Sdk\Exception\RuntimeException;
use Optimole\Sdk\Optimole;

class GuzzleClient implements ClientInterface
{
/**
* The Guzzle HTTP client.
*/
private GuzzleClientInterface $client;

/**
* Constructor.
*/
public function __construct(GuzzleClientInterface $client)
{
$this->client = $client;
}

/**
* {@inheritdoc}
*/
public function sendRequest(string $method, string $url, $body = null, array $headers = []): ?array
{
try {
$response = $this->client->send($this->createRequest($method, $url, $body, $headers), ['verify' => false]);
} catch (GuzzleBadResponseException $exception) {
throw new BadResponseException($exception->getMessage(), $exception->getCode(), $exception);
} catch (GuzzleException $exception) {
throw new RuntimeException($exception->getMessage(), $exception->getCode(), $exception);
}

$body = (string) $response->getBody();

if (empty($body)) {
return null;
}

$body = (array) json_decode($body, true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new BadResponseException(sprintf('Unable to decode JSON response: %s', json_last_error_msg()));
}

return $body;
}

/**
* Create a request object.
*/
private function createRequest(string $method, string $url, $body = null, array $headers = []): Request
{
if (is_array($body)) {
$body = json_encode($body);
}

if (null !== $body && !is_string($body)) {
throw new InvalidArgumentException('"body" must be a string or an array');
}

$headers = array_merge($headers, [
'User-Agent' => sprintf('optimole-sdk-php/%s', Optimole::VERSION),
]);
$method = strtolower($method);

return new Request($method, $url, $headers, $body);
}
}
Loading

0 comments on commit 2a090c3

Please sign in to comment.