77use Http \Client \Common \Plugin \ContentLengthPlugin ;
88use Http \Client \Common \Plugin \ContentTypePlugin ;
99use Http \Client \Common \Plugin \LoggerPlugin ;
10- use Http \Client \Exception ;
1110use Http \Message \Authentication ;
1211use ProgrammatorDev \Api \Builder \CacheBuilder ;
1312use ProgrammatorDev \Api \Builder \ClientBuilder ;
1716use ProgrammatorDev \Api \Event \ResponseContentsEvent ;
1817use ProgrammatorDev \Api \Exception \ConfigException ;
1918use ProgrammatorDev \Api \Helper \StringHelperTrait ;
19+ use Psr \Http \Client \ClientExceptionInterface as ClientException ;
2020use Psr \Http \Message \RequestInterface ;
2121use Psr \Http \Message \StreamInterface ;
2222use Symfony \Component \EventDispatcher \EventDispatcher ;
@@ -52,8 +52,8 @@ public function __construct()
5252 }
5353
5454 /**
55- * @throws ConfigException
56- * @throws Exception
55+ * @throws ConfigException If a base URL has not been set.
56+ * @throws ClientException
5757 */
5858 protected function request (
5959 string $ method ,
@@ -75,6 +75,21 @@ protected function request(
7575 $ headers = array_merge ($ this ->headerDefaults , $ headers );
7676 }
7777
78+ $ this ->configurePlugins ();
79+
80+ $ uri = $ this ->buildUri ($ path , $ query );
81+ $ request = $ this ->buildRequest ($ method , $ uri , $ headers , $ body );
82+ $ response = $ this ->clientBuilder ->getClient ()->sendRequest ($ request );
83+
84+ $ this ->eventDispatcher ->dispatch (new PostRequestEvent ($ request , $ response ));
85+
86+ $ contents = $ response ->getBody ()->getContents ();
87+
88+ return $ this ->eventDispatcher ->dispatch (new ResponseContentsEvent ($ contents ))->getContents ();
89+ }
90+
91+ private function configurePlugins (): void
92+ {
7893 // help servers understand the content
7994 $ this ->clientBuilder ->addPlugin (new ContentTypePlugin (), 40 );
8095 $ this ->clientBuilder ->addPlugin (new ContentLengthPlugin (), 32 );
@@ -120,17 +135,6 @@ protected function request(
120135 8
121136 );
122137 }
123-
124- $ uri = $ this ->buildUri ($ path , $ query );
125- $ request = $ this ->createRequest ($ method , $ uri , $ headers , $ body );
126-
127- $ response = $ this ->clientBuilder ->getClient ()->sendRequest ($ request );
128-
129- $ this ->eventDispatcher ->dispatch (new PostRequestEvent ($ request , $ response ));
130-
131- $ contents = $ response ->getBody ()->getContents ();
132-
133- return $ this ->eventDispatcher ->dispatch (new ResponseContentsEvent ($ contents ))->getContents ();
134138 }
135139
136140 protected function getBaseUrl (): ?string
@@ -269,7 +273,7 @@ private function buildUri(string $path, array $query = []): string
269273 return $ uri ;
270274 }
271275
272- private function createRequest (
276+ private function buildRequest (
273277 string $ method ,
274278 string $ uri ,
275279 array $ headers = [],
0 commit comments