Skip to content

Commit b088659

Browse files
authored
Merge pull request #4 from Ticketpark/feature/base-url-overwritting
Allow to overwrite base url and provide Guzzle config
2 parents bd25325 + 51bd961 commit b088659

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

lib/ApiClient/Http/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ final class Client implements ClientInterface
1515
{
1616
private readonly GuzzleClient $guzzle;
1717

18-
public function __construct()
18+
public function __construct(array $guzzleConfig = [])
1919
{
20-
$this->guzzle = new GuzzleClient();
20+
$this->guzzle = new GuzzleClient($guzzleConfig);
2121
}
2222

2323
public function head(string $url, array $headers): Response

lib/ApiClient/TicketparkApiClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class TicketparkApiClient
2323

2424
public function __construct(
2525
private readonly string $apiKey,
26-
private readonly string $apiSecret
26+
private readonly string $apiSecret,
27+
private readonly string $baseUrl = self::ROOT_URL,
2728
) {
2829
}
2930

@@ -147,7 +148,7 @@ private function getUrl(string $path, array $parameters = []): string
147148
$params = '?' . http_build_query($parameters);
148149
}
149150

150-
return self::ROOT_URL . $path . $params;
151+
return $this->baseUrl . $path . $params;
151152
}
152153

153154
private function getValidAccessToken(): string

test/ApiClient/TicketparkApiClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,29 @@ public function testGenerateTokensThrowsExceptionOnUnexpectedResponse()
224224
$this->apiClient->setUserCredentials('username', 'secret');
225225
$this->apiClient->generateTokens();
226226
}
227+
228+
public function testGetWithOverwrittenBaseUrl()
229+
{
230+
$httpClient = $this->prophet->prophesize(ClientInterface::class);
231+
$httpClient->get(
232+
'https://overwritten.ticketpark.ch/path?foo=bar',
233+
[
234+
'Content-Type' => 'application/json',
235+
'Accept' => 'application/json',
236+
'Authorization' => 'Bearer some-token'
237+
]
238+
)
239+
->willReturn(new Response(200, '', []))
240+
->shouldBeCalledOnce();
241+
242+
$apiClient = new TicketparkApiClient(
243+
'apiKey',
244+
'apiSecret',
245+
'https://overwritten.ticketpark.ch'
246+
);
247+
248+
$apiClient->setClient($httpClient->reveal());
249+
$apiClient->setAccessToken('some-token');
250+
$apiClient->get('/path', ['foo' => 'bar']);
251+
}
227252
}

0 commit comments

Comments
 (0)