Skip to content

Commit

Permalink
Merge pull request #1 from melba-ch/macro
Browse files Browse the repository at this point in the history
Use Macro instead of hacking the base HTTP
  • Loading branch information
cbaconnier authored Aug 8, 2022
2 parents 011edce + 1400b71 commit 43467dd
Show file tree
Hide file tree
Showing 33 changed files with 746 additions and 565 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
}
},
"require": {
"php": "^8.0",
"illuminate/contracts": "^8.37|^9.0",
"illuminate/support": "^8.49|^9.0",
"php": "^8.1",
"illuminate/contracts": "^9.0",
"illuminate/support": "^9.0",
"league/oauth2-client": "^2.6"
},
"require-dev": {
"brianium/paratest": "^6.2",
"orchestra/testbench": "^6.22|^7.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.3",
"nunomaduro/collision": "^6.1"
"nunomaduro/collision": "^6.1",
"spatie/invade": "^1.1"
},
"scripts": {
"test": "./vendor/bin/testbench package:test --parallel --no-coverage",
Expand Down
9 changes: 5 additions & 4 deletions config/zoho.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
*/
'url' => '/oauth2/zoho',
'redirect_url' => '/',
'sandbox' => env('ZOHO_SANDBOX', false),

// When requesting an accessToken, the API may return an error,
// The controller will redirect the user to `on_error_url`
// The controller will redirect the user to `on_error_redirect_to`
// with the error flashed in the session with the key `zoho.access_token_error`
// known error code:
// - 403: invalid_client_secret
// - 403: invalid_code
// - 500: fallback on unknown error
'on_error_url' => '/',
'on_error_redirect_to' => '/',

/**
* Middleware to generate a Token
Expand All @@ -24,7 +25,7 @@
\MelbaCh\LaravelZoho\Middleware\VerifyZohoCredentialsDoesntExists::class,
],

'config_repository' => \MelbaCh\LaravelZoho\Repositories\DefaultConfigRepository::class,
'config_repository' => \MelbaCh\LaravelZoho\Repositories\StorageConfigRepository::class,
/**
* Specific to the Default config Repository
*/
Expand All @@ -36,7 +37,7 @@
'ZohoBooks.settings.READ',
],

'access_token_repository' => \MelbaCh\LaravelZoho\Repositories\DefaultAccessTokenRepository::class,
'access_token_repository' => \MelbaCh\LaravelZoho\Repositories\StorageAccessTokenRepository::class,
/**
* Specific to the Default Access Token Repository
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/ZohoAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function checkResponse(ResponseInterface $response, $data): void
throw new IdentityProviderException(
sprintf('There was an error on response: %s', $data['error']),
match ($data['error']) {
'invalid_client_secret', 'invalid_code' => 403,
'invalid_client_secret', 'invalid_code', 'invalid_client' => 403,
default => 500
},
$data['error']
Expand Down
100 changes: 0 additions & 100 deletions src/Clients/ZohoHttpFactory.php

This file was deleted.

120 changes: 70 additions & 50 deletions src/Clients/ZohoUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@ public function __construct(
$this->config = $configRepository;
}

public function api(string $module, string $url, array $parameters = [])
{
return $this->make($module, $url, $parameters);
}

public function make(string $module, string $url, array $parameters = []): string
public function api(ZohoModules $module, string $url, array $parameters = [])
{
if (Str::startsWith($url, '/')) {
$url = Str::replaceFirst('/', '', $url);
}

if ($module === ZohoModules::Books) {
if ($module === ZohoModules::BOOKS) {
$url = $this->books($url);
} else {
$url = $this->default($module, $url);
$url = Str::finish($this->baseApiUrl($module), '/') . $url;
}

foreach ($parameters as $parameter => $value) {
Expand All @@ -40,13 +35,18 @@ public function make(string $module, string $url, array $parameters = []): strin
return $url;
}

public function web(string $module, string $url, array $parameters = [])
public function web(ZohoModules $module, string $url, array $parameters = [])
{
if (Str::startsWith($url, '/')) {
$url = Str::replaceFirst('/', '', $url);
}

$url = Str::finish($this->baseWebUrl($module), '/') . $url;
if (config('zoho.sandbox', false)) {
$url = Str::finish($this->baseWebUrlSandbox($module), '/') . $url;
} else {
$url = Str::finish($this->baseWebUrl($module), '/') . $url;
}


foreach ($parameters as $parameter => $value) {
$url = $this->addParameterToUrlQuery($url, $parameter, $value);
Expand All @@ -55,15 +55,43 @@ public function web(string $module, string $url, array $parameters = [])
return $url;
}


protected function default(string $module, string $url)
/**
* @param string $type
* @return string
* @internal
*/
public function oauthApiUrl(string $type): string
{
return Str::finish($this->baseApiUrl($module), '/') . $url;
$region = $this->config->region() ?? 'US';

return [
'authorization_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/auth',
'US' => 'https://accounts.zoho.com/oauth/v2/auth',
'IN' => 'https://accounts.zoho.in/oauth/v2/auth',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/auth',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/auth',
],
'access_token_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/token',
'US' => 'https://accounts.zoho.com/oauth/v2/token',
'IN' => 'https://accounts.zoho.in/oauth/v2/token',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/token',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/token',
],
'revoke_access_token_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/token/revoke',
'US' => 'https://accounts.zoho.com/oauth/v2/token/revoke',
'IN' => 'https://accounts.zoho.in/oauth/v2/token/revoke',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/token/revoke',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/token/revoke',
],
][$type][$region];
}

protected function books(string $url)
{
$url = Str::finish($this->baseApiUrl(ZohoModules::Books), '/') . $url;
$url = Str::finish($this->baseApiUrl(ZohoModules::BOOKS), '/') . $url;
return $this->addParameterToUrlQuery($url, 'organization_id', $this->config->currentOrganizationId());
}

Expand All @@ -83,92 +111,84 @@ protected function addParameterToUrlQuery(string $url, string $parameter, $value
return $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query'];
}

public function baseApiUrl(string $module): string
protected function baseApiUrl(ZohoModules $module): string
{
$region = $this->config->region() ?? 'US';

return [
ZohoModules::Books => [
ZohoModules::BOOKS->value => [
'EU' => 'https://books.zoho.eu/api/v3',
'US' => 'https://books.zoho.com/api/v3',
'IN' => 'https://books.zoho.in/api/v3',
'AU' => 'https://books.zoho.com.au/api/v3',
'CN' => 'https://books.zoho.com.cn/api/v3',
],
ZohoModules::Crm => [
'EU' => 'https://www.zohoapis.eu/crm/v2',
'US' => 'https://www.zohoapis.com/crm/v2',
'IN' => 'https://www.zohoapis.in/crm/v2',
'AU' => 'https://www.zohoapis.com.au/crm/v2',
'CN' => 'https://www.zohoapis.com.cn/crm/v2',
ZohoModules::CRM->value => [
'EU' => 'https://www.zohoapis.eu/crm/v3',
'US' => 'https://www.zohoapis.com/crm/v3',
'IN' => 'https://www.zohoapis.in/crm/v3',
'AU' => 'https://www.zohoapis.com.au/crm/v3',
'CN' => 'https://www.zohoapis.com.cn/crm/v3',
],
ZohoModules::Recruit => [
ZohoModules::RECRUIT->value => [
'EU' => 'https://recruit.zoho.eu/recruit/v2',
'US' => 'https://recruit.zoho.com/recruit/v2',
'IN' => 'https://recruit.zoho.in/recruit/v2',
'AU' => 'https://recruit.zoho.com.au/recruit/v2',
'CN' => 'https://recruit.zoho.com.cn/recruit/v2',
],
][$module][$region];
][$module->value][$region];
}

public function oauthApiUrl(string $type): string
protected function baseWebUrlSandbox(ZohoModules $module): string
{
$region = $this->config->region() ?? 'US';
$organization = $this->config->currentOrganizationId();

return [
'authorization_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/auth',
'US' => 'https://accounts.zoho.com/oauth/v2/auth',
'IN' => 'https://accounts.zoho.in/oauth/v2/auth',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/auth',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/auth',
ZohoModules::BOOKS->value => [
// Not implemented yet
],
'access_token_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/token',
'US' => 'https://accounts.zoho.com/oauth/v2/token',
'IN' => 'https://accounts.zoho.in/oauth/v2/token',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/token',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/token',
ZohoModules::CRM->value => [
'EU' => "https://crmsandbox.zoho.eu/crm/{$organization}",
'US' => "https://crmsandbox.zoho.com/crm/{$organization}",
'IN' => "https://crmsandbox.zoho.in/crm/{$organization}",
'AU' => "https://crmsandbox.zoho.com.eu/crm/{$organization}",
'CN' => "https://crmsandbox.zoho.com.cn/crm/{$organization}",
],
'revoke_access_token_url' => [
'EU' => 'https://accounts.zoho.eu/oauth/v2/token/revoke',
'US' => 'https://accounts.zoho.com/oauth/v2/token/revoke',
'IN' => 'https://accounts.zoho.in/oauth/v2/token/revoke',
'AU' => 'https://accounts.zoho.com.au/oauth/v2/token/revoke',
'CN' => 'https://accounts.zoho.com.cn/oauth/v2/token/revoke',
ZohoModules::RECRUIT->value => [
// Not implemented yet
],
][$type][$region];
][$module->value][$region];
}

public function baseWebUrl(string $module): string
protected function baseWebUrl(ZohoModules $module): string
{
$region = $this->config->region() ?? 'US';
$organization = $this->config->currentOrganizationId();

return [
ZohoModules::Books => [
ZohoModules::BOOKS->value => [
'EU' => 'https://books.zoho.eu/app#',
'US' => 'https://books.zoho.com/app#',
'IN' => 'https://books.zoho.in/app#',
'AU' => 'https://books.zoho.com.au/app#',
'CN' => 'https://books.zoho.com.cn/app#',
],
ZohoModules::Crm => [
ZohoModules::CRM->value => [
'EU' => "https://crm.zoho.eu/crm/{$organization}",
'US' => "https://crm.zoho.com/crm/{$organization}",
'IN' => "https://crm.zoho.in/crm/{$organization}",
'AU' => "https://crm.zoho.com.eu/crm/{$organization}",
'CN' => "https://crm.zoho.com.cn/crm/{$organization}",
],
ZohoModules::Recruit => [
ZohoModules::RECRUIT->value => [
'EU' => "https://recruit.zoho.eu/recruit/{$organization}",
'US' => "https://recruit.zoho.com/recruit/{$organization}",
'IN' => "https://recruit.zoho.in/recruit/{$organization}",
'AU' => "https://recruit.zoho.com.eu/recruit/{$organization}",
'CN' => "https://recruit.zoho.com.cn/recruit/{$organization}",
],
][$module][$region];
][$module->value][$region];
}

}
2 changes: 1 addition & 1 deletion src/Controllers/ZohoAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function getAccessToken(
'message' => $e->getMessage(),
]);

return redirect(config('zoho.on_error_url', '/'));
return redirect(config('zoho.on_error_redirect_to', '/'));
}

$accessTokenRepository->store($accessToken);
Expand Down
Loading

0 comments on commit 43467dd

Please sign in to comment.