Skip to content

Commit

Permalink
API 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkart committed Jun 2, 2023
1 parent e2b6ba5 commit 20e0a87
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Looker PHP SDK Advanced

API version: 3.1.0
API version: 4.0

This package is an advanced version of generated [Looker PHP SDK](https://github.com/alexkart/looker-php-sdk).
It adds additional functionality and simplifies the work with the Looker API. You can interact with the whole API
Expand Down Expand Up @@ -80,9 +80,10 @@ $looker = new \Alexkart\Looker\Looker($config);
```
The access token you provided will be used until it is valid and when it expires a new token will be
requested automatically. You can check if the token has been renewed like this:

```php
if ($looker->getConfig()->isAccessTokenRenewed()) {
$token = $looker->getConfig()->getAccessToken();
if ($looker->getLookerConfig()->isAccessTokenRenewed()) {
$token = $looker->getLookerConfig()->getAccessToken();
}
```
If you want the access token to be stored into the persistent storage (database, cache, file, etc.)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
},
"require": {
"alexkart/looker-php-sdk": "^0.1.0"
"php": ">=8.1",
"alexkart/looker-php-sdk": "0.2.*"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
7 changes: 5 additions & 2 deletions examples/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$config = new \Alexkart\Looker\LookerConfiguration(
$lookerConfig = new \Alexkart\Looker\LookerConfiguration(
$_SERVER['LOOKER_HOST'],
$_SERVER['LOOKER_CLIENT_ID'],
$_SERVER['LOOKER_CLIENT_SECRET'],
$_SERVER['LOOKER_ACCESS_TOKEN']
);
$looker = new \Alexkart\Looker\Looker($config);
$clientConfig = [
'verify' => false, // disable SSL verification
];
$looker = new \Alexkart\Looker\Looker($lookerConfig, $clientConfig);

$looks = $looker->lookApi->searchLooks(null, 'test');
var_dump($looks);
Expand Down
7 changes: 5 additions & 2 deletions examples/custom_configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$config = new \App\CustomLookerConfiguration(
$lookerConfig = new \App\CustomLookerConfiguration(
$_SERVER['LOOKER_HOST'],
$_SERVER['LOOKER_CLIENT_ID'],
$_SERVER['LOOKER_CLIENT_SECRET'],
$_SERVER['LOOKER_ACCESS_TOKEN']
);
$looker = new \Alexkart\Looker\Looker($config);
$clientConfig = [
'verify' => false, // disable SSL verification
];
$looker = new \Alexkart\Looker\Looker($lookerConfig, $clientConfig);

$looks = $looker->lookApi->searchLooks(null, 'test');
var_dump($looks);
40 changes: 23 additions & 17 deletions src/Looker.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,29 @@
class Looker {
private Client $authenticatedClient;
private Configuration $apiConfig;
private LookerConfiguration $config;
private LookerConfiguration $lookerConfig;
private array $clientConfig;

public function __construct(LookerConfiguration $config) {
$this->config = $config;
public function __construct(LookerConfiguration $lookerConfig, array $clientConfig = []) {
$this->lookerConfig = $lookerConfig;
$this->clientConfig = $clientConfig;
$this->login();
}

public function login(): void {
$this->apiConfig = new Configuration();
$this->apiConfig->setHost($this->config->getHost());
$this->apiConfig->setHost($this->lookerConfig->getHost());

if ($this->config->getAccessToken() === '') {
if ($this->lookerConfig->getAccessToken() === '') {
$apiInstance = new ApiAuthApi(
new Client(),
new Client($this->clientConfig),
$this->apiConfig
);

try {
$result = $apiInstance->login($this->config->getClientId(), $this->config->getClientSecret());
$this->config->setAccessToken($result->getAccessToken());
$this->config->setAccessTokenRenewed(true);
$result = $apiInstance->login($this->lookerConfig->getClientId(), $this->lookerConfig->getClientSecret());
$this->lookerConfig->setAccessToken($result->getAccessToken());
$this->lookerConfig->setAccessTokenRenewed(true);
} catch (\Throwable $e) {
echo 'Exception when calling ApiAuthApi->login: ', $e->getMessage(), PHP_EOL;
}
Expand All @@ -68,26 +70,26 @@ public function login(): void {
$this->authenticatedClient = new Client([
'verify' => false,
'headers' => [
'Authorization' => 'token ' . $this->config->getAccessToken(),
'Authorization' => 'token ' . $this->lookerConfig->getAccessToken(),
],
]);

if ($this->config->isAccessTokenRenewed()) {
$this->config->storeAccessToken($this->config->getAccessToken());
$this->config->setAccessTokenRenewed(false);
if ($this->lookerConfig->isAccessTokenRenewed()) {
$this->lookerConfig->storeAccessToken($this->lookerConfig->getAccessToken());
$this->lookerConfig->setAccessTokenRenewed(false);
}
}

public function invalidateAccessToken(): void {
$this->config->setAccessToken('');
$this->lookerConfig->setAccessToken('');
}

public function getAuthenticatedClient(): Client {
return $this->authenticatedClient;
}

public function getAccessToken(): string {
return $this->config->getAccessToken();
return $this->lookerConfig->getAccessToken();
}

public function __get(string $name) {
Expand All @@ -96,7 +98,11 @@ public function __get(string $name) {
return new $class($this, $this->authenticatedClient, $this->apiConfig);
}

public function getConfig(): LookerConfiguration {
return $this->config;
public function getLookerConfig(): LookerConfiguration {
return $this->lookerConfig;
}

public function getClientConfig(): array {
return $this->clientConfig;
}
}

0 comments on commit 20e0a87

Please sign in to comment.