Skip to content

Commit

Permalink
Merge pull request #132 from smsapi/uri-with-port
Browse files Browse the repository at this point in the history
Uri with port
  • Loading branch information
maciejlew authored Mar 27, 2024
2 parents 188cd39 + 6d4b620 commit 2711086
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 29 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- service url scheme and host validation
- service url with port support

### Changed
- PSR-18 HTTP client related exceptions namespace moved

## [3.0.10] - 2024-01-16
### Added
- PHP-8.3 support
Expand Down
21 changes: 4 additions & 17 deletions src/Curl/Exception/ClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@

namespace Smsapi\Client\Curl\Exception;

use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Smsapi\Client\Infrastructure\HttpClient\ClientException as HttpClientException;

/**
* @api
* @deprecated
* @see HttpClientException
*/
class ClientException extends Exception implements ClientExceptionInterface
class ClientException extends HttpClientException
{
private $request;

public static function withRequest(string $message, RequestInterface $request): self
{
$exception = new self($message);
$exception->request = $request;

return $exception;
}

public function getRequest(): RequestInterface
{
return $this->request;
}
}
6 changes: 4 additions & 2 deletions src/Curl/Exception/NetworkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Smsapi\Client\Curl\Exception;

use Psr\Http\Client\NetworkExceptionInterface;
use Smsapi\Client\Infrastructure\HttpClient\NetworkException as HttpClientNetworkException;

/**
* @api
* @deprecated
* @see HttpClientNetworkException
*/
class NetworkException extends ClientException implements NetworkExceptionInterface
class NetworkException extends HttpClientNetworkException
{

}
6 changes: 4 additions & 2 deletions src/Curl/Exception/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Smsapi\Client\Curl\Exception;

use Psr\Http\Client\RequestExceptionInterface;
use Smsapi\Client\Infrastructure\HttpClient\RequestException as HttpClientRequestException;

/**
* @api
* @deprecated
* @see HttpClientRequestException
*/
class RequestException extends ClientException implements RequestExceptionInterface
class RequestException extends HttpClientRequestException
{

}
12 changes: 9 additions & 3 deletions src/Curl/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Smsapi\Client\Curl\Exception\NetworkException;
use Smsapi\Client\Curl\Exception\RequestException;
use Smsapi\Client\Infrastructure\HttpClient\NetworkException;
use Smsapi\Client\Infrastructure\HttpClient\RequestException;

/**
* @internal
Expand All @@ -34,7 +34,13 @@ public function sendRequest(RequestInterface $request): ResponseInterface

private function prepareRequestHttpClient(RequestInterface $request)
{
$url = sprintf("%s://%s%s", $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getRequestTarget());
$url = strtr("{scheme}://{host}{port}{path}", [
'{scheme}' => $request->getUri()->getScheme(),
'{host}' => $request->getUri()->getHost(),
'{port}' => $request->getUri()->getPort() ? ':' . $request->getUri()->getPort() : '',
'{path}' => $request->getRequestTarget()
]);

$httpClient = curl_init($url);

if ($httpClient === false) {
Expand Down
30 changes: 30 additions & 0 deletions src/Infrastructure/HttpClient/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Smsapi\Client\Infrastructure\HttpClient;

use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;

/**
* @api
*/
class ClientException extends Exception implements ClientExceptionInterface
{
private $request;

public static function withRequest(string $message, RequestInterface $request): self
{
$exception = new static($message);
$exception->request = $request;

return $exception;
}

public function getRequest(): RequestInterface
{
return $this->request;
}
}
7 changes: 7 additions & 0 deletions src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Smsapi\Client\Infrastructure\HttpClient\RequestException;

/**
* @internal
Expand All @@ -33,14 +34,20 @@ private function prependBaseUri(RequestInterface $request): RequestInterface
{
$uri = $request->getUri();

if (!filter_var($this->baseUri, FILTER_VALIDATE_URL)) {
throw RequestException::withRequest("Invalid Base URI", $request);
}

$baseUriParts = parse_url($this->baseUri);

$scheme = $baseUriParts['scheme'] ?? '';
$host = $baseUriParts['host'] ?? '';
$port = $baseUriParts['port'] ?? null;
$basePath = $baseUriParts['path'] ?? '';
$basePath = rtrim($basePath, '/');

$uri = $uri->withPath($basePath . '/' . $uri->getPath());
$uri = $uri->withPort($port);
$uri = $uri->withHost($host);
$uri = $uri->withScheme($scheme);

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

declare(strict_types=1);

namespace Smsapi\Client\Infrastructure\HttpClient;

use Psr\Http\Client\NetworkExceptionInterface;

/**
* @api
*/
class NetworkException extends ClientException implements NetworkExceptionInterface
{

}
15 changes: 15 additions & 0 deletions src/Infrastructure/HttpClient/RequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Smsapi\Client\Infrastructure\HttpClient;

use Psr\Http\Client\RequestExceptionInterface;

/**
* @api
*/
class RequestException extends ClientException implements RequestExceptionInterface
{

}
2 changes: 1 addition & 1 deletion src/SmsapiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
interface SmsapiClient extends LoggerAwareInterface
{
const VERSION = '3.0.10';
const VERSION = 'Unreleased';

public function smsapiPlService(string $apiToken): SmsapiPlService;

Expand Down
27 changes: 27 additions & 0 deletions tests/Helper/HttpClient/HttpClientRequestSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Smsapi\Client\Tests\Helper\HttpClient;

use GuzzleHttp\Psr7\Response;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class HttpClientRequestSpy implements ClientInterface
{
private $lastSentRequest;

public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->lastSentRequest = $request;

return new Response();
}

public function getLastSentRequest(): RequestInterface
{
return $this->lastSentRequest;
}
}
4 changes: 0 additions & 4 deletions tests/SmsapiClientIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public static function prepare()

$apiUri = Config::get('API URI');

if (!filter_var($apiUri, FILTER_VALIDATE_URL)) {
throw new RuntimeException('Invalid API URI');
}

$smsapiHttpClient = new SmsapiHttpClient();

$serviceName = Config::get('Service name');
Expand Down
Loading

0 comments on commit 2711086

Please sign in to comment.