Skip to content

Commit

Permalink
Improve Validator, Add Debugger and API Version
Browse files Browse the repository at this point in the history
  • Loading branch information
GeovaneF55 committed Oct 12, 2024
1 parent 50a7a1e commit 8ae2b7e
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 54 deletions.
100 changes: 96 additions & 4 deletions src/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,108 @@

class ApiClient
{
const API_VERSION = 'v1';

/**
* HTTP Client
*
* @var HTTPClient $httpClient
*/
protected $httpClient;

/**
* Token Manager
*
* @var TokenManager $tokenManager
*/
protected $tokenManager;

/**
* Validator
*
* @var Validator $validator
*/
protected $validator;

/**
* Base URL
*
* @var string $baseUrl
*/
protected $baseUrl;

/**
* Grant type
*
* @var string $grantType
*/
protected $grantType;

/**
* Grant type
*
* @var string $grantType
*/
protected $clientId;

/**
* Client Secret
*
* @var string $clientSecret
*/
protected $clientSecret;

/**
* Response Type
*
* @var string $responseType
*/
protected $responseType;

/**
* Redirect URI
*
* @var string $redirectUri
*/
protected $redirectUri;

/**
* Authorization Code
*
* @var string $authorizationCode
*/
protected $authorizationCode;

/**
* Username
*
* @var string $username
*/
protected $username;

/**
* Password
*
* @var string $password
*/
protected $password;

/**
* Debug
*
* @var string $debug
*/
protected $debug;

/**
* Constructor
*
* @param array $configs
*/
public function __construct(array $configs)
public function __construct(array $configs, $apiVersion = 'v1')
{
$this->httpClient = new HttpClient('https://api.moloni.pt/' . self::API_VERSION . '/');
$this->baseUrl = 'https://api.moloni.pt/' . $apiVersion . '/';
$this->debug = false;

$this->httpClient = new HttpClient($this->baseUrl, $this->debug);
$this->tokenManager = new TokenManager();
$this->validator = new Validator();

Expand All @@ -58,6 +138,18 @@ private function setConfigs(array $configs): void
$this->password = $configs['password'] ?? null;
}

/**
* Set Guzzle Debug
*
* @param bool $debug
* @return void
*/
public function setDebug(bool $debug): void
{
$this->debug = $debug;
$this->httpClient->restartClient($this->baseUrl, $this->debug);
}

/**
* Validate
*
Expand Down
81 changes: 69 additions & 12 deletions src/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,76 @@

class HttpClient
{
/**
* Base URL
*
* @var string
*/
protected $baseUrl;

/**
* Client
*
* @var Client $client
*/
protected $client;

/**
* Access Token
*
* @var string $accessToken
*/
protected $accessToken;

/**
* Debug
*
* @var bool $debug
*/
protected $debug;

/**
* Http Client
*
* @param string $baseUrl
* @param bool $debug
*/
public function __construct(string $baseUrl)
public function __construct(string $baseUrl, bool $debug = false)
{
$this->baseUrl = $baseUrl;
$this->debug = $debug;

$this->startClient();
}

/**
* Start Client
*
* @return void
*/
protected function startClient()
{
$this->client = new Client([
'base_uri' => $baseUrl,
'http_errors' => false
'base_uri' => $this->baseUrl,
'http_errors' => $this->debug
]);
}

/**
* Restart Client
*
* @param string $baseUrl
* @param bool $debug
* @return void
*/
public function restartClient(string $baseUrl, bool $debug = false)
{
$this->baseUrl = $baseUrl;
$this->debug = $debug;

$this->startClient();
}

/**
* Set Access Token
*
Expand All @@ -43,12 +95,6 @@ public function setAccessToken(string $accessToken)
*/
public function getProcessedOptions(array $options): array
{
$options = array_merge_recursive($options, [
'query' => [
'json' => true
]
]);

if ($this->accessToken) {
$options = array_merge_recursive($options, [
'query' => [
Expand All @@ -57,6 +103,13 @@ public function getProcessedOptions(array $options): array
]);
}

$options = array_merge_recursive($options, [
'query' => [
'json' => true
],
'debug' => $this->debug
]);

return $options;
}

Expand All @@ -73,13 +126,17 @@ public function request(string $method, string $endpoint, array $options = [])
{
$url = $this->baseUrl . $endpoint;
$options = $this->getProcessedOptions($options);

$response = $this->client->request($method, $url, $options);

if ($response->getStatusCode() != 200) {
throw new ApiException($response, $response->getStatusCode());
$responseCode = $response->getStatusCode();
$responseContents = $response->getBody()->getContents();

if ($responseCode != 200) {
throw new ApiException($response, $responseCode);
}

return json_decode($response->getBody()->getContents(), true);
return json_decode($responseContents, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TokenManager
* @param array $response
* @return void
*/
public function setTokens(array $response)
public function setTokens($response = [])
{
if (isset($response['refresh_token'])) {
$this->refreshToken = $response['refresh_token'];
Expand Down
30 changes: 27 additions & 3 deletions src/Moloni.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,41 @@

class Moloni
{
/**
* API Client
*
* @var ApiClient $apiClient
*/
protected $apiClient;
protected $configs;

/**
* Constructor
*
* @param array $configs
*/
public function __construct(array $configs)
public function __construct(array $configs, $apiVersion = 'v1')
{
$this->apiClient = new ApiClient($configs, $apiVersion);
}

/**
* Start Debug
*
* @return void
*/
public function startDebug()
{
$this->apiClient->setDebug(true);
}

/**
* Stop Debug
*
* @return void
*/
public function stopDebug()
{
$this->apiClient = new ApiClient($configs);
$this->apiClient->setDebug(false);
}

// ACCOUNT AND PROFILE
Expand Down
Loading

0 comments on commit 8ae2b7e

Please sign in to comment.