Skip to content

Commit

Permalink
psr17
Browse files Browse the repository at this point in the history
  • Loading branch information
josiasmontag committed May 22, 2023
1 parent 1a9386b commit 4e74fae
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["8.1", "8.0", "7.4", "7.3", "7.2"]
php: ["8.2",8.1", "8.0", "7.4"]

steps:
- uses: actions/checkout@v2
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ Install
To install the PHP SDK you will need to be using [Composer]([https://getcomposer.org/)
in your project.

Install the SDK alongside Guzzle 7:
Install the SDK alongside Guzzle:

```bash
composer require cloudconvert/cloudconvert-php php-http/guzzle7-adapter
composer require cloudconvert/cloudconvert-php guzzlehttp/guzzle
```

This package is not tied to any specific HTTP client. Instead, it uses [Httplug](https://github.com/php-http/httplug) to let users choose whichever HTTP client they want to use.

If you want to use Guzzle 6 instead, use:

```bash
composer require cloudconvert/cloudconvert-php php-http/guzzle6-adapter
```
This package is not tied to any specific HTTP client by using [PSR-7](https://www.php-fig.org/psr/psr-7/), [PSR-17](https://www.php-fig.org/psr/psr-17/), [PSR-18](https://www.php-fig.org/psr/psr-18/), and [HTTPlug](https://httplug.io/). Therefore, you will also need to install packages that provide [`psr/http-client-implementation`](https://packagist.org/providers/psr/http-client-implementation) and [`psr/http-factory-implementation`](https://packagist.org/providers/psr/http-factory-implementation) (for example Guzzle).



Expand Down
30 changes: 17 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
}
],
"require": {
"php": "^7.2|^8.0",
"psr/http-message": "^1.0",
"php-http/client-implementation": "^1.0 || ^2.0",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message-factory": "^1.0",
"php-http/discovery": "^1.0",
"symfony/options-resolver": "^4.2 || ^5.0 || ^6.0",
"netresearch/jsonmapper": "^4.0",
"php-http/multipart-stream-builder": "^1.0",
"php": "^7.4|^8.0",
"ext-json": "*",
"php-http/httplug": "^2.4",
"php-http/client-common": "^2.0",
"php-http/message": "^1.7",
"guzzlehttp/psr7": "^1.0 || ^2.0"
"php-http/discovery": "^1.17",
"php-http/multipart-stream-builder": "^1.3",
"psr/http-message": "^1.1 || ^2.0",
"psr/http-factory-implementation": "^1.0",
"psr/http-client-implementation": "^1.0",
"symfony/options-resolver": "^4.2 || ^5.0 || ^6.0",
"netresearch/jsonmapper": "^4.0"
},
"require-dev": {
"php-http/mock-client": "^1.0",
"guzzlehttp/guzzle": "^7.0",
"phpunit/phpunit": "^7.0 || ^9.3"
"phpunit/phpunit": "^9.3",
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -41,5 +40,10 @@
"tests": [
"vendor/bin/phpunit --verbose"
]
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
7 changes: 3 additions & 4 deletions src/CloudConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@
use CloudConvert\Resources\TasksResource;
use CloudConvert\Resources\UsersResource;
use CloudConvert\Transport\HttpTransport;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CloudConvert
{

const VERSION = '3.3.0';
const VERSION = '3.4.0';

/**
* @var array
*/
protected $options;
/**
* @var HttpClient
* @var HttpTransport
*/
protected $httpTransport;
/**
Expand Down Expand Up @@ -58,7 +57,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('sandbox', 'boolean');

$resolver->setDefined('http_client');
$resolver->setAllowedTypes('http_client', [HttpClient::class, ClientInterface::class]);
$resolver->setAllowedTypes('http_client', [ClientInterface::class]);

}

Expand Down
85 changes: 47 additions & 38 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Http\Message\StreamFactory;
use Http\Message\UriFactory;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriFactoryInterface;


class HttpTransport
{

protected $options;
protected $httpClient;
protected $messageFactory;


/**
Expand All @@ -48,9 +43,9 @@ public function __construct($options)
/**
* Creates a new instance of the HTTP client.
*
* @return HttpClient
* @return PluginClient
*/
protected function createHttpClientInstance(): HttpClient
protected function createHttpClientInstance(): PluginClient
{

$httpClient = $this->options['http_client'] ?? Psr18ClientDiscovery::find();
Expand Down Expand Up @@ -82,35 +77,35 @@ public function getSyncBaseUri(): string
}

/**
* @return HttpClient
* @return PluginClient
*/
public function getHttpClient(): HttpClient
public function getHttpClient(): PluginClient
{
return $this->httpClient;
}

/**
* @return MessageFactory
* @return RequestFactoryInterface
*/
public function getMessageFactory(): MessageFactory
public function getRequestFactory(): RequestFactoryInterface
{
return $this->options['message_factory'] ?? MessageFactoryDiscovery::find();
return $this->options['request_factory'] ?? Psr17FactoryDiscovery::findRequestFactory();
}

/**
* @return UriFactory
* @return UriFactoryInterface
*/
public function getUriFactory(): UriFactory
public function getUriFactory(): UriFactoryInterface
{
return $this->options['uri_factory'] ?? UriFactoryDiscovery::find();
return $this->options['uri_factory'] ?? Psr17FactoryDiscovery::findUriFactory();
}

/**
* @return StreamFactory
* @return StreamFactoryInterface
*/
public function getStreamFactory(): StreamFactory
public function getStreamFactory(): StreamFactoryInterface
{
return $this->options['stream_factory'] ?? StreamFactoryDiscovery::find();
return $this->options['stream_factory'] ?? Psr17FactoryDiscovery::findStreamFactory();
}

/**
Expand All @@ -127,7 +122,7 @@ public function get(string $path, array $query = []): ResponseInterface
}


return $this->sendRequest($this->getMessageFactory()->createRequest('GET', $path, [
return $this->sendRequest($this->getRequestFactory()->createRequest('GET', $path, [
'accept-encoding' => 'application/json'
]));
}
Expand All @@ -140,10 +135,20 @@ public function get(string $path, array $query = []): ResponseInterface
*/
public function download(string $url)
{
return $this->sendRequest($this->getMessageFactory()->createRequest('GET', $url), false)->getBody();
return $this->sendRequest($this->getRequestFactory()->createRequest('GET', $url), false)->getBody();
}


/**
* @param array<string, mixed>|string $body
*/
protected function buildBody($body): StreamInterface
{
$stringBody = is_array($body) ? json_encode($body, JSON_THROW_ON_ERROR) : $body;

return $this->getStreamFactory()->createStream($stringBody);
}

/**
* @param $path
* @param $body
Expand All @@ -152,10 +157,12 @@ public function download(string $url)
*/
public function post(string $path, array $body): ResponseInterface
{
return $this->sendRequest($this->getMessageFactory()->createRequest('POST', $path, [
'content-type' => 'application/json',
'accept-encoding' => 'application/json'
], json_encode($body)));
return $this->sendRequest(
$this->getRequestFactory()->createRequest('POST', $path)
->withHeader('content-type', 'application/json')
->withHeader('accept-encoding', 'application/json')
->withBody($this->buildBody($body))
);
}

/**
Expand All @@ -166,10 +173,12 @@ public function post(string $path, array $body): ResponseInterface
*/
public function put(string $path, array $body): ResponseInterface
{
return $this->sendRequest($this->getMessageFactory()->createRequest('PUT', $path, [
'content-type' => 'application/json',
'accept-encoding' => 'application/json'
], json_encode($body)));
return $this->sendRequest(
$this->getRequestFactory()->createRequest('POST', $path)
->withHeader('content-type', 'application/json')
->withHeader('accept-encoding', 'application/json')
->withBody($this->buildBody($body))
);
}

/**
Expand All @@ -179,7 +188,7 @@ public function put(string $path, array $body): ResponseInterface
*/
public function delete(string $path): ResponseInterface
{
return $this->sendRequest($this->getMessageFactory()->createRequest('DELETE', $path, [
return $this->sendRequest($this->getRequestFactory()->createRequest('DELETE', $path, [
'accept-encoding' => 'application/json'
]));
}
Expand Down Expand Up @@ -208,12 +217,12 @@ public function upload($path, $file, string $fileName = null, array $additionalP
$multipartStream = $builder->build();
$boundary = $builder->getBoundary();

$request = $this->getMessageFactory()->createRequest(
$request = $this->getRequestFactory()->createRequest(
'POST',
$path,
['Content-Type' => 'multipart/form-data; boundary="' . $boundary . '"'],
$multipartStream
);
$path
)
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $boundary . '"')
->withBody($multipartStream);

return $this->sendRequest($request, false);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Unit/JobResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
namespace CloudConvert\Tests\Unit;


use CloudConvert\Models\ConvertTask;
use CloudConvert\Models\ExportUrlTask;
use CloudConvert\Models\ImportUrlTask;

use CloudConvert\Models\Job;
use CloudConvert\Models\Task;

Expand Down

0 comments on commit 4e74fae

Please sign in to comment.