Skip to content

Commit 4e74fae

Browse files
committed
psr17
1 parent 1a9386b commit 4e74fae

6 files changed

Lines changed: 72 additions & 68 deletions

File tree

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
php: ["8.1", "8.0", "7.4", "7.3", "7.2"]
17+
php: ["8.2",8.1", "8.0", "7.4"]
1818

1919
steps:
2020
- uses: actions/checkout@v2

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@ Install
1414
To install the PHP SDK you will need to be using [Composer]([https://getcomposer.org/)
1515
in your project.
1616

17-
Install the SDK alongside Guzzle 7:
17+
Install the SDK alongside Guzzle:
1818

1919
```bash
20-
composer require cloudconvert/cloudconvert-php php-http/guzzle7-adapter
20+
composer require cloudconvert/cloudconvert-php guzzlehttp/guzzle
2121
```
2222

23-
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.
24-
25-
If you want to use Guzzle 6 instead, use:
26-
27-
```bash
28-
composer require cloudconvert/cloudconvert-php php-http/guzzle6-adapter
29-
```
23+
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).
3024

3125

3226

composer.json

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
99
}
1010
],
1111
"require": {
12-
"php": "^7.2|^8.0",
13-
"psr/http-message": "^1.0",
14-
"php-http/client-implementation": "^1.0 || ^2.0",
15-
"php-http/httplug": "^1.0 || ^2.0",
16-
"php-http/message-factory": "^1.0",
17-
"php-http/discovery": "^1.0",
18-
"symfony/options-resolver": "^4.2 || ^5.0 || ^6.0",
19-
"netresearch/jsonmapper": "^4.0",
20-
"php-http/multipart-stream-builder": "^1.0",
12+
"php": "^7.4|^8.0",
13+
"ext-json": "*",
14+
"php-http/httplug": "^2.4",
2115
"php-http/client-common": "^2.0",
22-
"php-http/message": "^1.7",
23-
"guzzlehttp/psr7": "^1.0 || ^2.0"
16+
"php-http/discovery": "^1.17",
17+
"php-http/multipart-stream-builder": "^1.3",
18+
"psr/http-message": "^1.1 || ^2.0",
19+
"psr/http-factory-implementation": "^1.0",
20+
"psr/http-client-implementation": "^1.0",
21+
"symfony/options-resolver": "^4.2 || ^5.0 || ^6.0",
22+
"netresearch/jsonmapper": "^4.0"
2423
},
2524
"require-dev": {
2625
"php-http/mock-client": "^1.0",
27-
"guzzlehttp/guzzle": "^7.0",
28-
"phpunit/phpunit": "^7.0 || ^9.3"
26+
"phpunit/phpunit": "^9.3",
27+
"guzzlehttp/guzzle": "^7.0"
2928
},
3029
"autoload": {
3130
"psr-4": {
@@ -41,5 +40,10 @@
4140
"tests": [
4241
"vendor/bin/phpunit --verbose"
4342
]
43+
},
44+
"config": {
45+
"allow-plugins": {
46+
"php-http/discovery": false
47+
}
4448
}
4549
}

src/CloudConvert.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@
1010
use CloudConvert\Resources\TasksResource;
1111
use CloudConvert\Resources\UsersResource;
1212
use CloudConvert\Transport\HttpTransport;
13-
use Http\Client\HttpClient;
1413
use Psr\Http\Client\ClientInterface;
1514
use Symfony\Component\OptionsResolver\OptionsResolver;
1615

1716
class CloudConvert
1817
{
1918

20-
const VERSION = '3.3.0';
19+
const VERSION = '3.4.0';
2120

2221
/**
2322
* @var array
2423
*/
2524
protected $options;
2625
/**
27-
* @var HttpClient
26+
* @var HttpTransport
2827
*/
2928
protected $httpTransport;
3029
/**
@@ -58,7 +57,7 @@ public function configureOptions(OptionsResolver $resolver): void
5857
$resolver->setAllowedTypes('sandbox', 'boolean');
5958

6059
$resolver->setDefined('http_client');
61-
$resolver->setAllowedTypes('http_client', [HttpClient::class, ClientInterface::class]);
60+
$resolver->setAllowedTypes('http_client', [ClientInterface::class]);
6261

6362
}
6463

src/Transport/HttpTransport.php

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,22 @@
1010
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
1111
use Http\Client\Common\Plugin\RedirectPlugin;
1212
use Http\Client\Common\PluginClient;
13-
use Http\Client\HttpClient;
14-
use Http\Discovery\HttpClientDiscovery;
15-
use Http\Discovery\MessageFactoryDiscovery;
13+
use Http\Discovery\Psr17FactoryDiscovery;
1614
use Http\Discovery\Psr18ClientDiscovery;
17-
use Http\Discovery\StreamFactoryDiscovery;
18-
use Http\Discovery\UriFactoryDiscovery;
19-
use Http\Message\MessageFactory;
2015
use Http\Message\MultipartStream\MultipartStreamBuilder;
21-
use Http\Message\StreamFactory;
22-
use Http\Message\UriFactory;
16+
use Psr\Http\Message\RequestFactoryInterface;
2317
use Psr\Http\Message\RequestInterface;
2418
use Psr\Http\Message\ResponseInterface;
19+
use Psr\Http\Message\StreamFactoryInterface;
2520
use Psr\Http\Message\StreamInterface;
21+
use Psr\Http\Message\UriFactoryInterface;
2622

2723

2824
class HttpTransport
2925
{
3026

3127
protected $options;
3228
protected $httpClient;
33-
protected $messageFactory;
3429

3530

3631
/**
@@ -48,9 +43,9 @@ public function __construct($options)
4843
/**
4944
* Creates a new instance of the HTTP client.
5045
*
51-
* @return HttpClient
46+
* @return PluginClient
5247
*/
53-
protected function createHttpClientInstance(): HttpClient
48+
protected function createHttpClientInstance(): PluginClient
5449
{
5550

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

8479
/**
85-
* @return HttpClient
80+
* @return PluginClient
8681
*/
87-
public function getHttpClient(): HttpClient
82+
public function getHttpClient(): PluginClient
8883
{
8984
return $this->httpClient;
9085
}
9186

9287
/**
93-
* @return MessageFactory
88+
* @return RequestFactoryInterface
9489
*/
95-
public function getMessageFactory(): MessageFactory
90+
public function getRequestFactory(): RequestFactoryInterface
9691
{
97-
return $this->options['message_factory'] ?? MessageFactoryDiscovery::find();
92+
return $this->options['request_factory'] ?? Psr17FactoryDiscovery::findRequestFactory();
9893
}
9994

10095
/**
101-
* @return UriFactory
96+
* @return UriFactoryInterface
10297
*/
103-
public function getUriFactory(): UriFactory
98+
public function getUriFactory(): UriFactoryInterface
10499
{
105-
return $this->options['uri_factory'] ?? UriFactoryDiscovery::find();
100+
return $this->options['uri_factory'] ?? Psr17FactoryDiscovery::findUriFactory();
106101
}
107102

108103
/**
109-
* @return StreamFactory
104+
* @return StreamFactoryInterface
110105
*/
111-
public function getStreamFactory(): StreamFactory
106+
public function getStreamFactory(): StreamFactoryInterface
112107
{
113-
return $this->options['stream_factory'] ?? StreamFactoryDiscovery::find();
108+
return $this->options['stream_factory'] ?? Psr17FactoryDiscovery::findStreamFactory();
114109
}
115110

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

129124

130-
return $this->sendRequest($this->getMessageFactory()->createRequest('GET', $path, [
125+
return $this->sendRequest($this->getRequestFactory()->createRequest('GET', $path, [
131126
'accept-encoding' => 'application/json'
132127
]));
133128
}
@@ -140,10 +135,20 @@ public function get(string $path, array $query = []): ResponseInterface
140135
*/
141136
public function download(string $url)
142137
{
143-
return $this->sendRequest($this->getMessageFactory()->createRequest('GET', $url), false)->getBody();
138+
return $this->sendRequest($this->getRequestFactory()->createRequest('GET', $url), false)->getBody();
144139
}
145140

146141

142+
/**
143+
* @param array<string, mixed>|string $body
144+
*/
145+
protected function buildBody($body): StreamInterface
146+
{
147+
$stringBody = is_array($body) ? json_encode($body, JSON_THROW_ON_ERROR) : $body;
148+
149+
return $this->getStreamFactory()->createStream($stringBody);
150+
}
151+
147152
/**
148153
* @param $path
149154
* @param $body
@@ -152,10 +157,12 @@ public function download(string $url)
152157
*/
153158
public function post(string $path, array $body): ResponseInterface
154159
{
155-
return $this->sendRequest($this->getMessageFactory()->createRequest('POST', $path, [
156-
'content-type' => 'application/json',
157-
'accept-encoding' => 'application/json'
158-
], json_encode($body)));
160+
return $this->sendRequest(
161+
$this->getRequestFactory()->createRequest('POST', $path)
162+
->withHeader('content-type', 'application/json')
163+
->withHeader('accept-encoding', 'application/json')
164+
->withBody($this->buildBody($body))
165+
);
159166
}
160167

161168
/**
@@ -166,10 +173,12 @@ public function post(string $path, array $body): ResponseInterface
166173
*/
167174
public function put(string $path, array $body): ResponseInterface
168175
{
169-
return $this->sendRequest($this->getMessageFactory()->createRequest('PUT', $path, [
170-
'content-type' => 'application/json',
171-
'accept-encoding' => 'application/json'
172-
], json_encode($body)));
176+
return $this->sendRequest(
177+
$this->getRequestFactory()->createRequest('POST', $path)
178+
->withHeader('content-type', 'application/json')
179+
->withHeader('accept-encoding', 'application/json')
180+
->withBody($this->buildBody($body))
181+
);
173182
}
174183

175184
/**
@@ -179,7 +188,7 @@ public function put(string $path, array $body): ResponseInterface
179188
*/
180189
public function delete(string $path): ResponseInterface
181190
{
182-
return $this->sendRequest($this->getMessageFactory()->createRequest('DELETE', $path, [
191+
return $this->sendRequest($this->getRequestFactory()->createRequest('DELETE', $path, [
183192
'accept-encoding' => 'application/json'
184193
]));
185194
}
@@ -208,12 +217,12 @@ public function upload($path, $file, string $fileName = null, array $additionalP
208217
$multipartStream = $builder->build();
209218
$boundary = $builder->getBoundary();
210219

211-
$request = $this->getMessageFactory()->createRequest(
220+
$request = $this->getRequestFactory()->createRequest(
212221
'POST',
213-
$path,
214-
['Content-Type' => 'multipart/form-data; boundary="' . $boundary . '"'],
215-
$multipartStream
216-
);
222+
$path
223+
)
224+
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $boundary . '"')
225+
->withBody($multipartStream);
217226

218227
return $this->sendRequest($request, false);
219228
}

tests/Unit/JobResourceTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
namespace CloudConvert\Tests\Unit;
55

66

7-
use CloudConvert\Models\ConvertTask;
8-
use CloudConvert\Models\ExportUrlTask;
9-
use CloudConvert\Models\ImportUrlTask;
7+
108
use CloudConvert\Models\Job;
119
use CloudConvert\Models\Task;
1210

0 commit comments

Comments
 (0)