Skip to content

Commit

Permalink
Allow empty baseUri, fix base_uri setting in Symfony Bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
brzuchal committed Nov 27, 2023
1 parent 2a60871 commit 05ee033
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
18 changes: 16 additions & 2 deletions src/DefaultRestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,49 @@ public function __construct(
) {
}

/** @throws ExpectedBaseUri */
public function get(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec
{
return $this->createHeadersSpec('GET', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function head(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec
{
return $this->createHeadersSpec('HEAD', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function post(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec
{
return $this->createBodyHeadersSpec('POST', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function put(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec
{
return $this->createBodyHeadersSpec('PUT', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function patch(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec
{
return $this->createBodyHeadersSpec('PATCH', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function delete(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec
{
return $this->createHeadersSpec('DELETE', $this->createUriFactory($uri, $uriVariables));
}

/** @throws ExpectedBaseUri */
public function options(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec
{
return $this->createHeadersSpec('OPTIONS', $this->createUriFactory($uri, $uriVariables));
}

/** @param non-empty-string $method */
protected function createHeadersSpec(string $method, UriFactory $uriFactory): RequestHeadersSpec
{
return new RequestHeadersSpec(
Expand All @@ -80,6 +88,7 @@ protected function createHeadersSpec(string $method, UriFactory $uriFactory): Re
);
}

/** @param non-empty-string $method */
protected function createBodyHeadersSpec(string $method, UriFactory $uriFactory): RequestBodyHeadersSpec
{
return new RequestBodyHeadersSpec(
Expand All @@ -92,10 +101,15 @@ protected function createBodyHeadersSpec(string $method, UriFactory $uriFactory)
);
}

/** @throws ExpectedBaseUri */
protected function createUriFactory(string|null $uri, array|null $uriVariables): UriFactory
{
if ($this->baseUrl) {
if (str_ends_with($this->baseUrl, '/') && str_starts_with($uri, '/')) {
if ($this->baseUrl === null && $uri === null) {
throw new ExpectedBaseUri('Not passing Uri if no baseUri defined on client is not allowed');
}

if (! empty($this->baseUrl)) {
if (str_ends_with($this->baseUrl, '/') && $uri && str_starts_with($uri, '/')) {
$uri = ltrim($uri, '/');
}

Expand Down
2 changes: 1 addition & 1 deletion src/DefaultRestClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(
) {
}

public function baseUrl(string $url): static
public function baseUrl(string|null $url): static
{
$this->baseUrl = $url;

Expand Down
11 changes: 11 additions & 0 deletions src/ExpectedBaseUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Brzuchal\RestClient;

use Exception;

final class ExpectedBaseUri extends Exception
{
}
3 changes: 2 additions & 1 deletion src/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
*/
final class RestClient
{
/** @param non-empty-string|null $baseUri */
public static function create(
string $baseUri,
string|null $baseUri,
HttpClientInterface|null $httpClient = null,
SerializerInterface|null $serializer = null,
): RestClientInterface {
Expand Down
4 changes: 2 additions & 2 deletions src/RestClientBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface RestClientBuilderInterface
/**
* Specify a base URI for all requests using the URI template.
*
* @param non-empty-string $url
* @param non-empty-string|null $url
*
* @return $this
*/
public function baseUrl(string $url): static;
public function baseUrl(string|null $url): static;

/**
* Specify a base URI template list of variables.
Expand Down

0 comments on commit 05ee033

Please sign in to comment.