Skip to content

Commit 766d02d

Browse files
committed
Add another example
1 parent a288648 commit 766d02d

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"autoload": {
33
"psr-4": {
4-
"Alexkart\\Looker\\": "src/"
4+
"Alexkart\\Looker\\": "src/",
5+
"App\\": "examples/"
56
}
67
},
78
"require": {

examples/Looker.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class Looker extends \Alexkart\Looker\Looker {
6+
protected function storeAccessToken($accessToken): void {
7+
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'looker_access_token.txt', $accessToken);
8+
}
9+
10+
protected function loadAccessToken(): string {
11+
return (string)file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'looker_access_token.txt');
12+
}
13+
}

examples/test1.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once(dirname(__DIR__) . '/vendor/autoload.php');
4+
5+
$looker = new \App\Looker(
6+
'https://looker-host:19999/api/3.1',
7+
'client-id',
8+
'client-secret',
9+
);
10+
11+
$looks = $looker->lookApi->searchLooks(null, 'test');
12+
var_dump($looks);

src/Looker.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ class Looker {
5050
* @var void
5151
*/
5252
private $config;
53+
private bool $accessTokenRenewed = false;
5354

5455
public function __construct(string $host, string $clientId, string $clientSecret, string $accessToken = '') {
5556
$this->host = $host;
5657
$this->clientId = $clientId;
5758
$this->clientSecret = $clientSecret;
58-
$this->accessToken = $accessToken;
59+
$this->accessToken = $accessToken ?: $this->loadAccessToken();
5960
$this->login();
6061
}
6162

@@ -78,6 +79,7 @@ public function login(): void {
7879
try {
7980
$result = $apiInstance->login($this->clientId, $this->clientSecret);
8081
$this->accessToken = $result->getAccessToken();
82+
$this->accessTokenRenewed = true;
8183
} catch (Exception $e) {
8284
echo 'Exception when calling ApiAuthApi->login: ', $e->getMessage(), PHP_EOL;
8385
}
@@ -89,6 +91,10 @@ public function login(): void {
8991
'Authorization' => 'token ' . $this->accessToken,
9092
],
9193
]);
94+
95+
if ($this->accessTokenRenewed) {
96+
$this->storeAccessToken($this->accessToken);
97+
}
9298
}
9399

94100
public function invalidateAccessToken(): void {
@@ -98,4 +104,18 @@ public function invalidateAccessToken(): void {
98104
public function getAuthenticatedClient() {
99105
return $this->authenticatedClient;
100106
}
107+
108+
protected function storeAccessToken($accessToken): void {
109+
}
110+
111+
protected function loadAccessToken(): string {
112+
return '';
113+
}
114+
115+
/**
116+
* @return string
117+
*/
118+
public function getAccessToken(): string {
119+
return $this->accessToken;
120+
}
101121
}

0 commit comments

Comments
 (0)