Skip to content

Commit ad41502

Browse files
authored
Merge branch 'main' into cost_info
2 parents adf3167 + c5ecca6 commit ad41502

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
* added query stats
2+
* added ReadTokenFromFile
23
* added lambda on exception in retryTransaction
34

45
## 1.10.0

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ The following algorithm that is the same for YDB-PHP-SDK applies:
417417
4. Otherwise, if the value of the `YDB_ACCESS_TOKEN_CREDENTIALS` environment variable is set, the **Access token** authentication mode is used, where the this variable value is passed.
418418
5. Otherwise, the **Metadata** authentication mode is used.
419419

420+
## Reading from text file
421+
422+
```php
423+
<?php
424+
425+
use YdbPlatform\Ydb\Ydb;
426+
use YdbPlatform\Ydb\Auth\ReadTokenFromFile;
427+
428+
$config = [
429+
430+
// Database path
431+
'database' => '/local',
432+
433+
// Database endpoint
434+
'endpoint' => 'localhost:2136',
435+
436+
// Auto discovery (dedicated server only)
437+
'discovery' => false,
438+
439+
// IAM config
440+
'iam_config' => [
441+
'insecure' => true,
442+
],
443+
444+
'credentials' => new ReadTokenFromFile($tokenPath, $refreshTime)
445+
];
446+
447+
$ydb = new Ydb($config);
448+
```
449+
420450
# Usage
421451

422452
You should initialize a session from the Table service to start querying with retry.

src/Auth/ReadTokenFromFile.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Auth;
4+
5+
class ReadTokenFromFile extends Auth
6+
{
7+
8+
protected $fileName;
9+
protected $readInterval;
10+
11+
/**
12+
* @param string $fileName
13+
* @param int $readInterval
14+
*/
15+
public function __construct(string $fileName = "token.txt", int $readInterval = 60)
16+
{
17+
if(file_get_contents($fileName)===false){
18+
throw new \Exception("Error reading the file '$fileName'");
19+
}
20+
$this->fileName = $fileName;
21+
$this->readInterval = $readInterval;
22+
}
23+
24+
public function getTokenInfo(): TokenInfo
25+
{
26+
$token = file_get_contents($this->fileName);
27+
if($token===false){
28+
throw new \Exception("Error reading the file '$this->fileName'");
29+
}
30+
$token = trim($token);
31+
return new TokenInfo($token, $this->readInterval, 1);
32+
}
33+
34+
public function getName(): string
35+
{
36+
return "from file";
37+
}
38+
}

tests/CheckReadTokenFileTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace YdbPlatform\Ydb\Test;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use YdbPlatform\Ydb\Auth\ReadTokenFromFile;
6+
use YdbPlatform\Ydb\Ydb;
7+
use YdbPlatform\Ydb\YdbTable;
8+
9+
class CheckReadTokenFileTest extends TestCase{
10+
public function testReadTokenFile(){
11+
$config = [
12+
13+
// Database path
14+
'database' => '/local',
15+
16+
// Database endpoint
17+
'endpoint' => 'localhost:2136',
18+
19+
// Auto discovery (dedicated server only)
20+
'discovery' => false,
21+
22+
'credentials' => new ReadTokenFromFile("./tests/token.txt")
23+
];
24+
25+
$ydb = new Ydb($config);
26+
self::assertEquals('test-example-token',
27+
$ydb->iam()->token()
28+
);
29+
}
30+
}

tests/token.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-example-token

0 commit comments

Comments
 (0)