Skip to content

Commit

Permalink
Merge pull request #106 from lion-packages/new
Browse files Browse the repository at this point in the history
Refactoring fetch helper
  • Loading branch information
GabrielPalac authored Dec 24, 2024
2 parents a5f4481 + 1b97a8a commit 491c0e2
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 60 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEVELOPMENT_ENVIRONMENT="dev"
################################################################################
################# SERVER INFORMATION ------------------------- #################
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SHELL ["/bin/bash", "--login", "-i", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash \
&& source /home/lion/.bashrc \
&& nvm install 20 \
&& npm install -g npm@10
&& npm install -g npm@11

# Install OhMyZsh
RUN sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
Expand Down
6 changes: 4 additions & 2 deletions lion
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ declare(strict_types=1);

define('LION_START', microtime(true));

define('IS_INDEX', false);

/**
* -----------------------------------------------------------------------------
* Register The Auto Loader
Expand All @@ -22,6 +20,10 @@ use Lion\Bundle\Commands\CommandHandler;
use Lion\Database\Driver;
use Lion\Files\Store;

define('IS_INDEX', false);

define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT'));

/**
* -----------------------------------------------------------------------------
* Register environment variable loader automatically
Expand Down
6 changes: 4 additions & 2 deletions npm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ declare(strict_types=1);

define('LION_START', microtime(true));

define('IS_INDEX', false);

/**
* -----------------------------------------------------------------------------
* Register The Auto Loader
Expand All @@ -22,6 +20,10 @@ use Lion\Bundle\Commands\CommandHandler;
use Lion\Database\Driver;
use Lion\Files\Store;

define('IS_INDEX', false);

define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT'));

/**
* -----------------------------------------------------------------------------
* Register environment variable loader automatically
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

define('LION_START', microtime(true));

define('IS_INDEX', false);
define('IS_INDEX', true);

define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT'));

/**
* -----------------------------------------------------------------------------
Expand Down
15 changes: 10 additions & 5 deletions src/LionBundle/Commands/Lion/Route/PostmanCollectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Exception\GuzzleException;
use Lion\Bundle\Helpers\Commands\ClassFactory;
use Lion\Bundle\Helpers\Commands\PostmanCollection;
use Lion\Bundle\Helpers\Http\Fetch;
use Lion\Bundle\Helpers\Http\Routes;
use Lion\Command\Command;
use Lion\Files\Store;
Expand Down Expand Up @@ -187,11 +188,15 @@ private function fetchRoutes(): void
$this->jsonName = $this->str->of(Carbon::now()->format('Y_m_d'))->concat('_lion_collection')->lower()->get();

$this->routes = json_decode(
fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])->getBody()->getContents(),
fetch(
new Fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
);

Expand Down
15 changes: 10 additions & 5 deletions src/LionBundle/Commands/Lion/Route/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DI\Attribute\Inject;
use GuzzleHttp\Exception\GuzzleException;
use Lion\Bundle\Helpers\Http\Fetch;
use Lion\Bundle\Helpers\Http\Routes;
use Lion\Command\Command;
use Lion\Helpers\Arr;
Expand Down Expand Up @@ -211,11 +212,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function fetchRoutes(): void
{
$this->routes = json_decode(
fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])->getBody()->getContents(),
fetch(
new Fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
);

Expand Down
9 changes: 3 additions & 6 deletions src/LionBundle/Helpers/Bundle/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

declare(strict_types=1);

use GuzzleHttp\Client;
use Lion\Helpers\Arr;
use Lion\Helpers\Str;
use Lion\Request\Request;
use Lion\Request\Response;

/**
* [Object of the GuzzleHttp Client class]
*
* @var Client
* [Object with properties captured in an HTTP request]
*/
const client = new Client();
define('request', (new Request())->capture());

/**
* [Object with properties captured in an HTTP request]
*/
define('request', (new Request())->capture());
define('ssl', (new Request())->capture());

/**
* [Object of Response class to generate response objects]
Expand Down
18 changes: 11 additions & 7 deletions src/LionBundle/Helpers/Bundle/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Carbon\Carbon;
use Faker\Factory;
use Faker\Generator;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use Lion\Bundle\Enums\LogTypeEnum;
use Lion\Bundle\Helpers\Env;
use Lion\Bundle\Helpers\Fake;
use Lion\Bundle\Helpers\Http\Fetch;
use Lion\Files\Store;
use Lion\Request\Http;
use Lion\Request\Request;
Expand Down Expand Up @@ -56,20 +58,22 @@ function now(null|DateTimeZone|string $tz = null): Carbon

if (!function_exists('fetch')) {
/**
* Function to make HTTP requests with guzzlehttp
* Function to make HTTP requests with GuzzleHttp
*
* @param string $method [HTTP protocol]
* @param string $uri [URL to make the request]
* @param array $options [Options to send through the request, such as
* headers or parameters]
* @param Fetch $fetch [Defines parameters for consuming HTTP requests with
* GuzzleHttp]
*
* @return Response
*
* @throws GuzzleException
*/
function fetch(string $method, string $uri, array $options = []): Response
function fetch(Fetch $fetch): Response
{
return client->request($method, $uri, $options);
$client = new Client(
null === $fetch->getFetchConfiguration() ? [] : $fetch->getFetchConfiguration()->getConfiguration()
);

return $client->request($fetch->getHttpMethod(), $fetch->getUri(), $fetch->getOptions());
}
}

Expand Down
103 changes: 103 additions & 0 deletions src/LionBundle/Helpers/Http/Fetch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Lion\Bundle\Helpers\Http;

/**
* Defines parameters for consuming HTTP requests with GuzzleHttp
*
* @property string $httpMethod [HTTP protocol]
* @property string $uri [URL to make the request]
* @property array<string, mixed> $options [Options to send through the request,
* such as headers or parameters]
* @property FetchConfiguration|null $fetchConfiguration [Defines the
* configuration data for making an HTTP request]
*
* @package Lion\Bundle\Helpers\Http
*/
class Fetch
{
/**
* @var FetchConfiguration|null $fetchConfiguration [Defines the
* configuration data for making an HTTP request]
*/
private ?FetchConfiguration $fetchConfiguration = null;

/**
* Class Constructor
*
* @param string $httpMethod [HTTP protocol]
* @param string $uri [URL to make the request]
* @param array<string, mixed> $options [Options to send through the request, such as
* headers or parameters]
*/
public function __construct(
private readonly string $httpMethod,
private readonly string $uri,
private readonly array $options = []
) {}

/**
* Returns an HTTP configuration object
*
* @return FetchConfiguration|null
*
* @internal
*/
public function getFetchConfiguration(): ?FetchConfiguration
{
return $this->fetchConfiguration;
}

/**
* Adds an HTTP configuration object
*
* @param FetchConfiguration $fetchConfiguration [Defines the configuration
* data for making an HTTP request]
*
* @return Fetch
*/
public function setFetchConfiguration(FetchConfiguration $fetchConfiguration): Fetch
{
$this->fetchConfiguration = $fetchConfiguration;

return $this;
}

/**
* Returns the HTTP method of the HTTP request
*
* @return string
*
* @internal
*/
public function getHttpMethod(): string
{
return $this->httpMethod;
}

/**
* Returns the URI of the HTTP request
*
* @return string
*
* @internal
*/
public function getUri(): string
{
return $this->uri;
}

/**
* Returns the data from the HTTP request
*
* @return array<string, mixed>
*
* @internal
*/
public function getOptions(): array
{
return $this->options;
}
}
36 changes: 36 additions & 0 deletions src/LionBundle/Helpers/Http/FetchConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Lion\Bundle\Helpers\Http;

/**
* Defines the configuration data for making an HTTP request
*
* @property array<string, string> $configuration [Configuration data]
*
* @package Lion\Bundle\Helpers\Http
*/
class FetchConfiguration
{
/**
* Class Constructor
*
* @param array<string, string> $configuration [Configuration data]
*/
public function __construct(
private readonly array $configuration = []
) {}

/**
* Returns configuration data
*
* @return array<string, string>
*
* @internal
*/
public function getConfiguration(): array
{
return $this->configuration;
}
}
8 changes: 0 additions & 8 deletions tests/Helpers/Bundle/ConstantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tests\Helpers\Bundle;

use GuzzleHttp\Client;
use Lion\Helpers\Arr;
use Lion\Helpers\Str;
use Lion\Request\Response;
Expand All @@ -13,13 +12,6 @@

class ConstantsTest extends Test
{
#[Testing]
public function clientConstant(): void
{
$this->assertTrue(defined('client'));
$this->assertInstanceOf(Client::class, client);
}

#[Testing]
public function requestConstant(): void
{
Expand Down
5 changes: 4 additions & 1 deletion tests/Helpers/Bundle/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Faker\Generator;
use GuzzleHttp\Exception\GuzzleException;
use Lion\Bundle\Enums\LogTypeEnum;
use Lion\Bundle\Helpers\Http\Fetch;
use Lion\Request\Http;
use Lion\Request\Status;
use Lion\Security\AES;
Expand Down Expand Up @@ -86,7 +87,9 @@ public function testNow(): void
*/
public function testFetch(): void
{
$fetchResponse = fetch(Http::GET, env('SERVER_URL'))->getBody()->getContents();
$fetchResponse = fetch(new Fetch(Http::GET, env('SERVER_URL')))
->getBody()
->getContents();

$response = json_decode($fetchResponse, true);

Expand Down
Loading

0 comments on commit 491c0e2

Please sign in to comment.