Skip to content

Commit 648193c

Browse files
feat: use $_ENV aware getenv helper
1 parent 85b427d commit 648193c

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/Client.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ public function __construct(
3737
?string $baseUrl = null,
3838
RequestOptions|array|null $requestOptions = null,
3939
) {
40-
$this->browserbaseAPIKey = (string) ($browserbaseAPIKey ?? getenv('BROWSERBASE_API_KEY'));
41-
$this->browserbaseProjectID = (string) ($browserbaseProjectID ?? getenv('BROWSERBASE_PROJECT_ID'));
42-
$this->modelAPIKey = (string) ($modelAPIKey ?? getenv('MODEL_API_KEY'));
43-
44-
$baseUrl ??= getenv(
40+
$this->browserbaseAPIKey = (string) ($browserbaseAPIKey ?? Util::getenv(
41+
'BROWSERBASE_API_KEY'
42+
));
43+
$this->browserbaseProjectID = (string) ($browserbaseProjectID ?? Util::getenv(
44+
'BROWSERBASE_PROJECT_ID'
45+
));
46+
$this->modelAPIKey = (string) ($modelAPIKey ?? Util::getenv(
47+
'MODEL_API_KEY'
48+
));
49+
50+
$baseUrl ??= Util::getenv(
4551
'STAGEHAND_BASE_URL'
4652
) ?: 'https://api.stagehand.browserbase.com';
4753

src/Core/Util.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ final class Util
2525

2626
public const JSONL_CONTENT_TYPE = '/^application\/(:?x-(?:n|l)djson)|(:?(?:x-)?jsonl)/';
2727

28+
public static function getenv(string $key): ?string
29+
{
30+
if (array_key_exists($key, array: $_ENV)) {
31+
if (!is_string($value = $_ENV[$key])) {
32+
throw new \InvalidArgumentException;
33+
}
34+
35+
return $value;
36+
}
37+
38+
if (is_string($value = getenv($key))) {
39+
return $value;
40+
}
41+
42+
return null;
43+
}
44+
2845
/**
2946
* @return array<string,mixed>
3047
*/

tests/Services/SessionsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\Attributes\Test;
77
use PHPUnit\Framework\TestCase;
88
use Stagehand\Client;
9+
use Stagehand\Core\Util;
910
use Stagehand\Sessions\SessionActResponse;
1011
use Stagehand\Sessions\SessionEndResponse;
1112
use Stagehand\Sessions\SessionExecuteResponse;
@@ -28,7 +29,7 @@ protected function setUp(): void
2829
{
2930
parent::setUp();
3031

31-
$testUrl = getenv('TEST_API_BASE_URL') ?: 'http://127.0.0.1:4010';
32+
$testUrl = Util::getenv('TEST_API_BASE_URL') ?: 'http://127.0.0.1:4010';
3233
$client = new Client(
3334
browserbaseAPIKey: 'My Browserbase API Key',
3435
browserbaseProjectID: 'My Browserbase Project ID',

0 commit comments

Comments
 (0)