Skip to content

Commit d64b07b

Browse files
committed
chore: some refactoring
1 parent 1de0e62 commit d64b07b

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

src/Api.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Http\Client\Common\Plugin\ContentLengthPlugin;
88
use Http\Client\Common\Plugin\ContentTypePlugin;
99
use Http\Client\Common\Plugin\LoggerPlugin;
10-
use Http\Client\Exception;
1110
use Http\Message\Authentication;
1211
use ProgrammatorDev\Api\Builder\CacheBuilder;
1312
use ProgrammatorDev\Api\Builder\ClientBuilder;
@@ -17,6 +16,7 @@
1716
use ProgrammatorDev\Api\Event\ResponseContentsEvent;
1817
use ProgrammatorDev\Api\Exception\ConfigException;
1918
use ProgrammatorDev\Api\Helper\StringHelperTrait;
19+
use Psr\Http\Client\ClientExceptionInterface as ClientException;
2020
use Psr\Http\Message\RequestInterface;
2121
use Psr\Http\Message\StreamInterface;
2222
use 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

Comments
 (0)