Skip to content

upgrade to enso v7 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use LaravelEnso\Api\Contracts\Endpoint;
use LaravelEnso\Api\Enums\Calls;
use LaravelEnso\Api\Enums\Call;
use LaravelEnso\Api\Exceptions\Api as Exception;
use LaravelEnso\Api\Exceptions\Handler;
use LaravelEnso\Api\Models\Log;
Expand Down Expand Up @@ -62,13 +62,13 @@ abstract protected function endpoint(): Endpoint;
private function log(Response $response, string $duration): void
{
Log::create([
'user_id' => Auth::user()?->id,
'url' => $this->endpoint()->url(),
'route' => Route::currentRouteName(),
'method' => $this->endpoint()->method(),
'status' => $response->status(),
'try' => $this->api->tries(),
'type' => Calls::Outbound,
'user_id' => Auth::user()?->id,
'url' => $this->endpoint()->url(),
'route' => Route::currentRouteName(),
'method' => $this->endpoint()->method(),
'status' => $response->status(),
'try' => $this->api->tries(),
'type' => Call::Outbound->value,
'duration' => $duration,
]);
}
Expand Down
15 changes: 7 additions & 8 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use LaravelEnso\Api\Contracts\Retry;
use LaravelEnso\Api\Contracts\Timeout;
use LaravelEnso\Api\Contracts\UsesAuth;
use LaravelEnso\Api\Enums\Authorization;
use LaravelEnso\Api\Enums\Methods;
use LaravelEnso\Api\Enums\ResponseCodes;
use LaravelEnso\Api\Enums\Method;
use LaravelEnso\Api\Enums\ResponseCode;

class Api
{
protected int $tries;
protected string $method;
protected Method $method;

public function __construct(protected Endpoint $endpoint)
{
Expand Down Expand Up @@ -88,8 +87,8 @@ protected function headers(): array

if ($this->endpoint instanceof UsesAuth) {
$token = $this->endpoint->tokenProvider()->current();
$type = Authorization::get($this->endpoint->tokenProvider()->type());
$headers['Authorization'] = "{$type} {$token}";
$type = $this->endpoint->tokenProvider()->type();
$headers['Authorization'] = "{$type->value} {$token}";
}

return $headers;
Expand All @@ -104,13 +103,13 @@ protected function shouldRetry(): bool
protected function possibleTokenExpiration(Response $response): bool
{
return $this->endpoint instanceof UsesAuth
&& ResponseCodes::needsAuth($response->status())
&& ResponseCode::from($response->status())->needsAuth()
&& $this->tries === 1;
}

protected function body(): string|array|null
{
if ($this->method === Methods::post) {
if ($this->method === Method::post) {
return $this->endpoint->body();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace LaravelEnso\Api\Contracts;

use LaravelEnso\Api\Enums\Method;

interface Endpoint
{
public function method(): string;
public function method(): Method;

public function url(): string;

Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace LaravelEnso\Api\Contracts;

use LaravelEnso\Api\Enums\Authorization;

interface Token
{
public function type(): string;
public function type(): Authorization;

public function auth(): self;

Expand Down
11 changes: 3 additions & 8 deletions src/Enums/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace LaravelEnso\Api\Enums;

use LaravelEnso\Enums\Services\Enum;

class Authorization extends Enum
enum Authorization :string
{
protected static bool $localisation = false;
protected static bool $validatesKeys = true;

public const Basic = 'Basic';
public const Bearer = 'Bearer';
case Basic = 'Basic';
case Bearer = 'Bearer';
}
9 changes: 9 additions & 0 deletions src/Enums/Call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LaravelEnso\Api\Enums;

enum Call: int
{
case Inbound = 1;
case Outbound = 2;
}
11 changes: 0 additions & 11 deletions src/Enums/Calls.php

This file was deleted.

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

namespace LaravelEnso\Api\Enums;

enum Method: string
{
case get = 'get';
case post = 'post';
case put = 'put';
case delete = 'delete';
}
16 changes: 0 additions & 16 deletions src/Enums/Methods.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Enums/ResponseCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LaravelEnso\Api\Enums;

enum ResponseCode: int
{
case OK = 200;
case Created = 201;

case Unauthorized = 401;
case Forbidden = 403;

case NotFound = 404;
case UnprocessableEntity = 422;

// public static function needsAuth(int $code): bool
// {
// return in_array($code, [self::Unauthorized, self::Forbidden]);
// }
public function needsAuth(): bool
{
return match ($this) {
self::OK => false,
self::Created => false,
self::Unauthorized => true,
self::Forbidden => true,
self::NotFound => false,
self::UnprocessableEntity => false,
};
}
}
22 changes: 0 additions & 22 deletions src/Enums/ResponseCodes.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Http/Middleware/ApiLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace LaravelEnso\Api\Http\Middleware;

use Closure;
use LaravelEnso\Api\Enums\Calls;
use LaravelEnso\Api\Enums\Call;
use LaravelEnso\Api\Exceptions\Handler;
use LaravelEnso\Api\Models\Log;
use LaravelEnso\Helpers\Services\Decimals;
Expand All @@ -23,7 +23,7 @@ public function terminate($request, $response)
'route' => $request->route()->getName(),
'method' => $request->method(),
'status' => $response->status(),
'type' => Calls::Inbound,
'type' => Call::Inbound->value,
'duration' => Decimals::sub(microtime(true), LARAVEL_START),
]);

Expand Down