33namespace MailerLiteApi \Common ;
44
55use Http \Client \HttpClient ;
6- use Http \Client \ Curl \ Client as CurlClient ;
7-
8- use GuzzleHttp \ Psr7 \ Request ;
9- use MailerLiteApi \ Common \ ApiConstants ;
6+ use Http \Discovery \ Psr17FactoryDiscovery ;
7+ use Http \ Discovery \ Psr18ClientDiscovery ;
8+ use Psr \ Http \ Client \ ClientInterface ;
9+ use Psr \ Http \ Message \ RequestFactoryInterface ;
1010use Psr \Http \Message \ResponseInterface ;
11-
12- use Http \Message \MessageFactory \GuzzleMessageFactory ;
13- use Http \Message \StreamFactory \GuzzleStreamFactory ;
11+ use Psr \Http \Message \StreamFactoryInterface ;
1412
1513class RestClient {
1614
@@ -20,10 +18,14 @@ class RestClient {
2018
2119 public $ baseUrl ;
2220
21+ public $ requestFactory ;
22+
23+ public $ streamFactory ;
24+
2325 /**
24- * @param string $baseUrl
25- * @param string $apiKey
26- * @param HttpClient $httpClient
26+ * @param string $baseUrl
27+ * @param string $apiKey
28+ * @param \Http\Client\ HttpClient|null $httpClient
2729 */
2830 public function __construct ($ baseUrl , $ apiKey , HttpClient $ httpClient = null )
2931 {
@@ -90,11 +92,25 @@ public function delete($endpointUri)
9092 */
9193 protected function send ($ method , $ endpointUri , $ body = null , array $ headers = [])
9294 {
95+
9396 $ headers = array_merge ($ headers , self ::getDefaultHeaders ());
9497 $ endpointUrl = $ this ->baseUrl . $ endpointUri ;
9598
96- $ request = new Request ($ method , $ endpointUrl , $ headers , json_encode ($ body ));
97- $ response = $ this ->getHttpClient ()->sendRequest ($ request );
99+ $ request = $ this ->getRequestFactory ()->createRequest ($ method , $ endpointUrl );
100+
101+ if ($ body ) {
102+ $ stream = $ this ->getStreamFactory ()
103+ ->createStream (json_encode ($ body ));
104+ $ request = $ request ->withBody ($ stream );
105+ }
106+
107+ foreach ($ headers as $ name => $ value ) {
108+ $ request = $ request ->withAddedHeader ($ name , $ value );
109+ }
110+
111+ $ response = $ this ->getHttpClient ()->sendRequest (
112+ $ request
113+ );
98114
99115 return $ this ->handleResponse ($ response );
100116 }
@@ -117,30 +133,52 @@ protected function handleResponse(ResponseInterface $response)
117133 }
118134
119135 /**
120- * @return HttpClient
136+ * @return ClientInterface
121137 */
122138 protected function getHttpClient ()
123139 {
124140 if (is_null ($ this ->httpClient )) {
125- $ options = [
126- CURLOPT_CONNECTTIMEOUT => 10 ,
127- CURLOPT_SSL_VERIFYPEER => false
128- ];
129-
130- $ this ->httpClient = new CurlClient (new GuzzleMessageFactory (), new GuzzleStreamFactory (), $ options );
141+ $ this ->httpClient = Psr18ClientDiscovery::find ();
131142 }
132143
133144 return $ this ->httpClient ;
134145 }
135146
147+ /**
148+ * @return HttpClient
149+ */
150+ protected function getRequestFactory (): RequestFactoryInterface
151+ {
152+ if (null === $ this ->requestFactory ) {
153+ $ this ->requestFactory = Psr17FactoryDiscovery::findRequestFactory ();
154+ }
155+
156+ return $ this ->requestFactory ;
157+ }
158+
159+ private function getStreamFactory (): StreamFactoryInterface
160+ {
161+ if (null === $ this ->streamFactory ) {
162+ $ this ->streamFactory = Psr17FactoryDiscovery::findStreamFactory ();
163+ }
164+
165+ return $ this ->streamFactory ;
166+ }
167+
136168 /**
137169 * @return array
138170 */
139171 protected function getDefaultHeaders () {
140- return [
141- 'User-Agent ' => ApiConstants::SDK_USER_AGENT . '/ ' . ApiConstants::SDK_VERSION ,
142- 'X-MailerLite-ApiKey ' => $ this ->apiKey ,
143- 'Content-Type ' => 'application/json '
172+ $ headers = [
173+ 'User-Agent ' => ApiConstants::SDK_USER_AGENT .'/ ' .ApiConstants::SDK_VERSION ,
174+ 'Content-Type ' => 'application/json ' ,
144175 ];
176+
177+ // Only adding it when provided. Not required for RestClientTest
178+ if ($ this ->apiKey ) {
179+ $ headers ['X-MailerLite-ApiKey ' ] = $ this ->apiKey ;
180+ }
181+
182+ return $ headers ;
145183 }
146- }
184+ }
0 commit comments