-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c344242
Showing
22 changed files
with
1,390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# OliviaHttpService | ||
|
||
OliviaHttpService é um serviço de cliente HTTP simples para realizar solicitações HTTP a uma API específica. | ||
|
||
## Requisitos | ||
|
||
- PHP 7.2 ou superior | ||
|
||
# Contribuição | ||
|
||
Contribuições são bem-vindas! Se você encontrou um bug, tem alguma sugestão ou deseja implementar uma nova funcionalidade, abra uma issue. | ||
|
||
# Licença | ||
Este projeto está licenciado sob a Licença MIT. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "elzobrito/olivia-app", | ||
"description": "HttpService Olivia", | ||
"authors": [ | ||
{ | ||
"name": "elzobrito", | ||
"email": "elzo.filho@etec.sp.gov.br" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"HttpServiceSrc\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^7.2.5|^8.0" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace HttpService; | ||
|
||
use HttpServiceSrc\HttpClient\ServicoHttpClient; | ||
use HttpServiceSrc\service\ServicoService; | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
class Index | ||
{ | ||
public function __construct() | ||
{ | ||
// Cria uma instância do ServicoHttpClient passando a base URL | ||
$httpClient = new ServicoHttpClient('https://dummyjson.com', 'Content-Type: application/json\r\n', '1.1'); | ||
|
||
// Cria uma instância do ServicoService passando o HttpClient | ||
$servicoService = new ServicoService($httpClient); | ||
$result = $servicoService->getSearchWithSlash('products/1'); // Corrigido: passando uma string como argumento | ||
print_r(json_decode($result)); | ||
} | ||
} | ||
new Index(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
|
||
namespace HttpService; | ||
|
||
use HttpServiceSrc\HttpClient\ServicoHttpClient; | ||
use HttpServiceSrc\service\ServicoService; | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
class Index | ||
{ | ||
public function __construct() | ||
{ | ||
// Cria uma instância do ServicoHttpClient passando a base URL | ||
$httpClient = new ServicoHttpClient('https://dummyjson.com', 'Content-Type: application/json\r\n'); | ||
|
||
// Cria uma instância do ServicoService passando o HttpClient | ||
$servicoService = new ServicoService($httpClient); | ||
$result = $servicoService->getSearchWithSlash('products',1); // Corrigido: passando uma string como argumento | ||
print_r(json_decode($result)); | ||
} | ||
} | ||
new Index(); | ||
|
||
|
||
namespace HttpServiceSrc\service; | ||
|
||
use HttpServiceSrc\HttpClient\HttpClientInterface; | ||
|
||
class ServicoService | ||
{ | ||
private $httpClient; | ||
|
||
public function __construct(HttpClientInterface $httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
public function getSearchWithSlash($query, $params = null) | ||
{ | ||
$uri = '/' .$query . '/' . $params; | ||
|
||
return $this->httpClient->get($uri); | ||
} | ||
|
||
public function getSearch($query, $params = null) | ||
{ | ||
$endpoint = '/' . $query; | ||
|
||
$queryParams = []; | ||
|
||
if ($params) { | ||
$queryParams = array_merge($queryParams, $params); | ||
} | ||
|
||
return $this->httpClient->get($endpoint, $queryParams); | ||
} | ||
} | ||
|
||
|
||
namespace HttpServiceSrc\HttpClient; | ||
|
||
use HttpServiceSrc\Exception\HttpClientException; | ||
|
||
abstract class AbstractHttpClient implements HttpClientInterface | ||
{ | ||
protected $defaultHeaders; | ||
|
||
public function __construct($handler = null) | ||
{ | ||
$this->defaultHeaders = $handler ?? array('Content-Type: application/x-www-form-urlencoded'); | ||
} | ||
|
||
abstract public function get($endpoint, $params = null); | ||
|
||
protected function getDefaultContextOptions($method, $parametros = null) | ||
{ | ||
return array( | ||
'http' => | ||
array( | ||
'method' => $method, | ||
'header' => $this->defaultHeaders, | ||
'content' => $parametros ?? '' | ||
) | ||
); | ||
} | ||
|
||
protected function handleResponse($response, $context) | ||
{ | ||
if ($response === false) { | ||
$error = error_get_last(); | ||
throw new HttpClientException('Failed to retrieve data from the server. Error: ' . $error['message']); | ||
} | ||
|
||
return $response; | ||
} | ||
protected function isResponseSuccessful($responseHeaders) | ||
{ | ||
if (!empty($responseHeaders)) { | ||
$statusCode = $this->getStatusCodeFromHeaders($responseHeaders); | ||
return $statusCode >= 200 && $statusCode < 300; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function getStatusCodeFromHeaders($headers) | ||
{ | ||
preg_match('/^HTTP\/\d\.\d\s+(\d+)/', $headers[0], $matches); | ||
return (int) $matches[1]; | ||
} | ||
} | ||
namespace HttpServiceSrc\HttpClient; | ||
interface HttpClientInterface | ||
{ | ||
public function get($endpoint, $params = null); | ||
} | ||
|
||
namespace HttpServiceSrc\HttpClient; | ||
|
||
use InvalidArgumentException; | ||
|
||
class ServicoHttpClient extends AbstractHttpClient | ||
{ | ||
private $baseUrl; | ||
|
||
public function __construct($baseUrl,$handler = null) | ||
{ | ||
parent::__construct($handler); | ||
$this->baseUrl = $baseUrl; | ||
} | ||
|
||
public function get($endpoint, $params = null) | ||
{ | ||
$url = $this->baseUrl . $endpoint; | ||
|
||
// Validação da URL | ||
if (!filter_var($url, FILTER_VALIDATE_URL)) { | ||
throw new InvalidArgumentException("Invalid URL: $url"); | ||
} | ||
|
||
$options = $this->getDefaultContextOptions('GET'); | ||
$context = stream_context_create($options); | ||
$response = file_get_contents($url, false, $context); | ||
|
||
return $this->handleResponse($response, $context); | ||
} | ||
|
||
} | ||
namespace HttpServiceSrc\Exception; | ||
|
||
use Exception; | ||
|
||
class HttpClientException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace HttpServiceSrc\Exception; | ||
|
||
use Exception; | ||
|
||
class HttpClientException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace HttpServiceSrc\HttpClient; | ||
|
||
use HttpServiceSrc\Exception\HttpClientException; | ||
|
||
abstract class AbstractHttpClient implements HttpClientInterface | ||
{ | ||
protected $defaultHeaders; | ||
protected $protocol_version; | ||
|
||
public function __construct($handler = null,$protocol_version) | ||
{ | ||
$this->defaultHeaders = $handler ?? array('Content-Type: application/x-www-form-urlencoded'); | ||
$this->protocol_version = $protocol_version; | ||
} | ||
|
||
abstract public function get($endpoint, $params = null); | ||
|
||
protected function getDefaultContextOptions($method, $parametros = null) | ||
{ | ||
return array( | ||
'http' => | ||
array( | ||
'method' => $method, | ||
'header' => $this->defaultHeaders, | ||
'content' => $parametros ?? '', | ||
'protocol_version' => $this->protocol_version, | ||
) | ||
); | ||
} | ||
|
||
protected function handleResponse($response, $context) | ||
{ | ||
if ($response === false) { | ||
$error = error_get_last(); | ||
throw new HttpClientException('Failed to retrieve data from the server. Error: ' . $error['message']); | ||
} | ||
|
||
return $response; | ||
} | ||
protected function isResponseSuccessful($responseHeaders) | ||
{ | ||
if (!empty($responseHeaders)) { | ||
$statusCode = $this->getStatusCodeFromHeaders($responseHeaders); | ||
return $statusCode >= 200 && $statusCode < 300; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function getStatusCodeFromHeaders($headers) | ||
{ | ||
preg_match('/^HTTP\/\d\.\d\s+(\d+)/', $headers[0], $matches); | ||
return (int) $matches[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
namespace HttpServiceSrc\HttpClient; | ||
interface HttpClientInterface | ||
{ | ||
public function get($endpoint, $params = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace HttpServiceSrc\HttpClient; | ||
|
||
use InvalidArgumentException; | ||
|
||
class ServicoHttpClient extends AbstractHttpClient | ||
{ | ||
private $baseUrl; | ||
|
||
public function __construct($baseUrl, $handler = null, $protocol_version = null) | ||
{ | ||
parent::__construct($handler, $protocol_version); | ||
$this->baseUrl = $baseUrl; | ||
} | ||
|
||
public function get($endpoint, $params = null) | ||
{ | ||
$url = $this->baseUrl . $endpoint; | ||
|
||
// Validação da URL | ||
if (!filter_var($url, FILTER_VALIDATE_URL)) { | ||
throw new InvalidArgumentException("Invalid URL: $url"); | ||
} | ||
|
||
$options = $this->getDefaultContextOptions('GET'); | ||
$context = stream_context_create($options); | ||
$response = file_get_contents($url, false, $context); | ||
|
||
return $this->handleResponse($response, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace HttpServiceSrc\service; | ||
|
||
use HttpServiceSrc\HttpClient\HttpClientInterface; | ||
|
||
class ServicoService | ||
{ | ||
private $httpClient; | ||
|
||
public function __construct(HttpClientInterface $httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
public function getSearchWithSlash($query, $params = null) | ||
{ | ||
$uri = '/' .$query . '/' . $params; | ||
|
||
return $this->httpClient->get($uri); | ||
} | ||
|
||
public function getSearch($query, $params = null) | ||
{ | ||
$endpoint = '/' . $query; | ||
|
||
$queryParams = []; | ||
|
||
if ($params) { | ||
$queryParams = array_merge($queryParams, $params); | ||
} | ||
|
||
return $this->httpClient->get($endpoint, $queryParams); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// autoload.php @generated by Composer | ||
|
||
require_once __DIR__ . '/composer/autoload_real.php'; | ||
|
||
return ComposerAutoloaderInitd8fdea5f824ff0ce43134e67dfcd9377::getLoader(); |
Oops, something went wrong.