Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 1b59890

Browse files
author
Mateusz Gostański
committed
Refactored CallApiAction
Renamed CallApiAction to CallApi. Modified constructor which take baseUrl, OAuthUrl, Client id & key and cache key. Removed checking configuration and throw WrongConfigException. Updated calls for GetToken
1 parent d3616ff commit 1b59890

File tree

2 files changed

+26
-51
lines changed

2 files changed

+26
-51
lines changed
Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,40 @@
44

55
use Grixu\ApiClient\Exceptions\AccessDeniedException;
66
use Grixu\ApiClient\Exceptions\ApiCallException;
7-
use Grixu\ApiClient\Exceptions\WrongConfigException;
87
use Illuminate\Support\Facades\Http;
98

10-
class CallApiAction
9+
class CallApi
1110
{
12-
private GetTokenAction $getToken;
11+
protected GetToken $getToken;
12+
protected string $baseUrl;
1313

14-
public function __construct()
14+
public function __construct(string $baseUrl, string $oAuthUrl, string $clientId, string $clientKey, string $cacheKey)
1515
{
16-
$this->getToken = new GetTokenAction();
16+
$this->baseUrl = $baseUrl;
17+
$this->getToken = new GetToken($oAuthUrl, $clientId, $clientKey, $cacheKey);
1718
}
1819

19-
public function execute(string $url)
20+
public function call(string $url): array
2021
{
21-
if (empty(config('api-client.base_url'))
22-
|| empty(config('api-client.client_key'))
23-
|| empty(config('api-client.client_id'))) {
24-
throw new WrongConfigException();
25-
}
26-
27-
$token = $this->getToken->execute();
22+
$token = $this->getToken->get();
2823

2924
$client = Http::withToken($token)
3025
->withHeaders(
3126
[
3227
'Accept' => 'application/json'
3328
]
3429
)
35-
->get(config('api-client.base_url').$url);
30+
->get($this->baseUrl.$url);
3631

3732
if ($client->successful()) {
3833
return $client->json();
3934
}
4035

4136
if ($client->failed()) {
4237
if ($client->status() === 401) {
43-
$resetTokenAction = new ResetTokenAction();
44-
$resetTokenAction->execute();
38+
$this->getToken->reset();
4539
// Token refreshed, then try again
46-
return $this->execute($url);
40+
return $this->call($url);
4741
}
4842

4943
if ($client->status() == 403) {
Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@
22

33
namespace Grixu\ApiClient\Tests;
44

5-
use Grixu\ApiClient\CallApiAction;
5+
use Grixu\ApiClient\CallApi;
66
use Grixu\ApiClient\Exceptions\AccessDeniedException;
77
use Grixu\ApiClient\Exceptions\ApiCallException;
88
use Grixu\ApiClient\Exceptions\TokenIssueException;
9-
use Grixu\ApiClient\Exceptions\WrongConfigException;
109
use Grixu\ApiClient\ApiClientServiceProvider;
1110
use Illuminate\Support\Facades\Http;
1211
use Orchestra\Testbench\TestCase;
1312

14-
class CallApiActionTest extends TestCase
13+
class CallApiTest extends TestCase
1514
{
16-
private CallApiAction $action;
15+
private CallApi $action;
1716
private string $url;
1817

1918
protected function setUp(): void
2019
{
2120
parent::setUp();
2221

23-
$this->action = new CallApiAction();
24-
$this->url = config('api-client.base_url');
22+
$this->action = new CallApi(
23+
env('TEST_BASE_URL'),
24+
env('TEST_OAUTH'),
25+
env('TEST_CLIENT_ID'),
26+
env('TEST_CLIENT_KEY'),
27+
'test'
28+
);
29+
$this->url = env('TEST_BASE_URL');
2530
}
2631

2732
protected function getPackageProviders($app)
2833
{
2934
return [ApiClientServiceProvider::class];
3035
}
3136

32-
protected function useClearConfig($app)
33-
{
34-
$app->config->set('api-client.base_url', '');
35-
}
36-
3737
/** @test */
3838
public function normal_pass()
3939
{
@@ -56,7 +56,7 @@ public function normal_pass()
5656
]
5757
);
5858

59-
$result = $this->action->execute($this->url);
59+
$result = $this->action->call($this->url);
6060

6161
$this->assertNotEmpty($result);
6262
}
@@ -71,7 +71,7 @@ public function with_http_error()
7171
);
7272

7373
try {
74-
$this->action->execute($this->url);
74+
$this->action->call($this->url);
7575
} catch (ApiCallException $e) {
7676
$this->assertTrue(true);
7777
} catch (TokenIssueException $e) {
@@ -103,8 +103,7 @@ public function with_token_revalidation()
103103
);
104104

105105
try {
106-
$result = $this->action->execute($this->url);
107-
106+
$result = $this->action->call($this->url);
108107
$this->assertIsArray($result);
109108
$this->assertArrayHasKey('data', $result);
110109
} catch (TokenIssueException $e) {
@@ -114,24 +113,6 @@ public function with_token_revalidation()
114113
}
115114
}
116115

117-
/**
118-
* @test
119-
* @environment-setup useClearConfig
120-
*/
121-
public function with_no_config()
122-
{
123-
try {
124-
$this->action->execute($this->url);
125-
$this->assertTrue(false);
126-
} catch (ApiCallException $e) {
127-
$this->assertTrue(false);
128-
} catch (TokenIssueException $e) {
129-
$this->assertTrue(false);
130-
} catch (WrongConfigException $e) {
131-
$this->assertTrue(true);
132-
}
133-
}
134-
135116
/** @test */
136117
public function with_access_denied()
137118
{
@@ -142,7 +123,7 @@ public function with_access_denied()
142123
);
143124

144125
try {
145-
$this->action->execute($this->url);
126+
$this->action->call($this->url);
146127
} catch (ApiCallException $e) {
147128
$this->assertTrue(false);
148129
} catch (TokenIssueException $e) {

0 commit comments

Comments
 (0)