Skip to content

Commit

Permalink
refactor: fetch helper parameters are modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleon4 committed Dec 24, 2024
1 parent 4caeccf commit 1b97a8a
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 59 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
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
8 changes: 8 additions & 0 deletions src/LionBundle/Helpers/Http/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct(
* Returns an HTTP configuration object
*
* @return FetchConfiguration|null
*
* @internal
*/
public function getFetchConfiguration(): ?FetchConfiguration
{
Expand All @@ -67,6 +69,8 @@ public function setFetchConfiguration(FetchConfiguration $fetchConfiguration): F
* Returns the HTTP method of the HTTP request
*
* @return string
*
* @internal
*/
public function getHttpMethod(): string
{
Expand All @@ -77,6 +81,8 @@ public function getHttpMethod(): string
* Returns the URI of the HTTP request
*
* @return string
*
* @internal
*/
public function getUri(): string
{
Expand All @@ -87,6 +93,8 @@ public function getUri(): string
* Returns the data from the HTTP request
*
* @return array<string, mixed>
*
* @internal
*/
public function getOptions(): array
{
Expand Down
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
49 changes: 29 additions & 20 deletions tests/Helpers/Commands/PostmanCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Exception\GuzzleException;
use Lion\Bundle\Helpers\Commands\PostmanCollection;
use Lion\Bundle\Helpers\Http\Fetch;
use Lion\Dependency\Injection\Container;
use Lion\Files\Store;
use Lion\Request\Http;
Expand Down Expand Up @@ -196,11 +197,13 @@ public function addRequest(string $name, string $route, string $method, array $p
public function addRoutes(): void
{
$routes = json_decode(
fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])
fetch(
new Fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
Expand All @@ -227,11 +230,13 @@ public function addRoutes(): void
public function createCollection(): void
{
$routes = json_decode(
fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])
fetch(
new Fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
Expand Down Expand Up @@ -267,11 +272,13 @@ public function createCollection(): void
public function getRoutes(): void
{
$routes = json_decode(
fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])
fetch(
new Fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
Expand Down Expand Up @@ -302,11 +309,13 @@ public function getRoutes(): void
public function getItems(): void
{
$routes = json_decode(
fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH']
]
])
fetch(
new Fetch(Http::GET, (self::HOST . '/route-list'), [
'headers' => [
'Lion-Auth' => $_ENV['SERVER_HASH'],
],
])
)
->getBody()
->getContents(),
true
Expand Down
6 changes: 4 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

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

define('IS_INDEX', false);

/**
* -----------------------------------------------------------------------------
* Register The Auto Loader
Expand All @@ -20,6 +18,10 @@
use Dotenv\Dotenv;
use Lion\Files\Store;

define('IS_INDEX', false);

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

/**
* -----------------------------------------------------------------------------
* Register environment variable loader automatically
Expand Down

0 comments on commit 1b97a8a

Please sign in to comment.