Skip to content

Commit 1e7ec2e

Browse files
JasonTheAdamsfelixarntzJasonTheAdams
authored
Merge pull request #135 from WordPress/add/provider-credentials-url
Co-authored-by: felixarntz <[email protected]> Co-authored-by: JasonTheAdams <[email protected]>
2 parents e235ecb + 05213dc commit 1e7ec2e

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

src/ProviderImplementations/Anthropic/AnthropicProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ protected static function createProviderMetadata(): ProviderMetadata
6262
return new ProviderMetadata(
6363
'anthropic',
6464
'Anthropic',
65-
ProviderTypeEnum::cloud()
65+
ProviderTypeEnum::cloud(),
66+
'https://console.anthropic.com/settings/keys'
6667
);
6768
}
6869

src/ProviderImplementations/Google/GoogleProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected static function createProviderMetadata(): ProviderMetadata
6565
return new ProviderMetadata(
6666
'google',
6767
'Google',
68-
ProviderTypeEnum::cloud()
68+
ProviderTypeEnum::cloud(),
69+
'https://aistudio.google.com/app/api-keys'
6970
);
7071
}
7172

src/ProviderImplementations/OpenAi/OpenAiProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ protected static function createProviderMetadata(): ProviderMetadata
7171
return new ProviderMetadata(
7272
'openai',
7373
'OpenAI',
74-
ProviderTypeEnum::cloud()
74+
ProviderTypeEnum::cloud(),
75+
'https://platform.openai.com/api-keys'
7576
);
7677
}
7778

src/Providers/DTO/ProviderMetadata.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
* @phpstan-type ProviderMetadataArrayShape array{
1919
* id: string,
2020
* name: string,
21-
* type: string
21+
* type: string,
22+
* credentialsUrl?: ?string
2223
* }
2324
*
2425
* @extends AbstractDataTransferObject<ProviderMetadataArrayShape>
@@ -28,6 +29,7 @@ class ProviderMetadata extends AbstractDataTransferObject
2829
public const KEY_ID = 'id';
2930
public const KEY_NAME = 'name';
3031
public const KEY_TYPE = 'type';
32+
public const KEY_CREDENTIALS_URL = 'credentialsUrl';
3133

3234
/**
3335
* @var string The provider's unique identifier.
@@ -44,6 +46,11 @@ class ProviderMetadata extends AbstractDataTransferObject
4446
*/
4547
protected ProviderTypeEnum $type;
4648

49+
/**
50+
* @var string|null The URL where users can get credentials.
51+
*/
52+
protected ?string $credentialsUrl;
53+
4754
/**
4855
* Constructor.
4956
*
@@ -52,12 +59,14 @@ class ProviderMetadata extends AbstractDataTransferObject
5259
* @param string $id The provider's unique identifier.
5360
* @param string $name The provider's display name.
5461
* @param ProviderTypeEnum $type The provider type.
62+
* @param string|null $credentialsUrl The URL where users can get credentials.
5563
*/
56-
public function __construct(string $id, string $name, ProviderTypeEnum $type)
64+
public function __construct(string $id, string $name, ProviderTypeEnum $type, ?string $credentialsUrl = null)
5765
{
5866
$this->id = $id;
5967
$this->name = $name;
6068
$this->type = $type;
69+
$this->credentialsUrl = $credentialsUrl;
6170
}
6271

6372
/**
@@ -96,6 +105,18 @@ public function getType(): ProviderTypeEnum
96105
return $this->type;
97106
}
98107

108+
/**
109+
* Gets the credentials URL.
110+
*
111+
* @since 0.1.0
112+
*
113+
* @return string|null The credentials URL.
114+
*/
115+
public function getCredentialsUrl(): ?string
116+
{
117+
return $this->credentialsUrl;
118+
}
119+
99120
/**
100121
* {@inheritDoc}
101122
*
@@ -119,6 +140,10 @@ public static function getJsonSchema(): array
119140
'enum' => ProviderTypeEnum::getValues(),
120141
'description' => 'The provider type (cloud, server, or client).',
121142
],
143+
self::KEY_CREDENTIALS_URL => [
144+
'type' => 'string',
145+
'description' => 'The URL where users can get credentials.',
146+
],
122147
],
123148
'required' => [self::KEY_ID, self::KEY_NAME, self::KEY_TYPE],
124149
];
@@ -137,6 +162,7 @@ public function toArray(): array
137162
self::KEY_ID => $this->id,
138163
self::KEY_NAME => $this->name,
139164
self::KEY_TYPE => $this->type->value,
165+
self::KEY_CREDENTIALS_URL => $this->credentialsUrl,
140166
];
141167
}
142168

@@ -152,7 +178,8 @@ public static function fromArray(array $array): self
152178
return new self(
153179
$array[self::KEY_ID],
154180
$array[self::KEY_NAME],
155-
ProviderTypeEnum::from($array[self::KEY_TYPE])
181+
ProviderTypeEnum::from($array[self::KEY_TYPE]),
182+
$array[self::KEY_CREDENTIALS_URL] ?? null
156183
);
157184
}
158185
}

tests/unit/Providers/DTO/ProviderMetadataTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ public function testConstructorAndGetters(): void
3333
$this->assertEquals($name, $metadata->getName());
3434
$this->assertSame($type, $metadata->getType());
3535
$this->assertTrue($metadata->getType()->isCloud());
36+
$this->assertNull($metadata->getCredentialsUrl());
37+
}
38+
39+
/**
40+
* Tests constructor with credentials URL.
41+
*
42+
* @return void
43+
*/
44+
public function testConstructorWithCredentialsUrl(): void
45+
{
46+
$id = 'openai';
47+
$name = 'OpenAI';
48+
$type = ProviderTypeEnum::cloud();
49+
$credentialsUrl = 'https://platform.openai.com/account/api-keys';
50+
51+
$metadata = new ProviderMetadata($id, $name, $type, $credentialsUrl);
52+
53+
$this->assertEquals($id, $metadata->getId());
54+
$this->assertEquals($name, $metadata->getName());
55+
$this->assertSame($type, $metadata->getType());
56+
$this->assertEquals($credentialsUrl, $metadata->getCredentialsUrl());
3657
}
3758

3859
/**
@@ -78,11 +99,13 @@ public function testGetJsonSchema(): void
7899
$this->assertArrayHasKey(ProviderMetadata::KEY_ID, $schema['properties']);
79100
$this->assertArrayHasKey(ProviderMetadata::KEY_NAME, $schema['properties']);
80101
$this->assertArrayHasKey(ProviderMetadata::KEY_TYPE, $schema['properties']);
102+
$this->assertArrayHasKey(ProviderMetadata::KEY_CREDENTIALS_URL, $schema['properties']);
81103

82104
// Check property types
83105
$this->assertEquals('string', $schema['properties'][ProviderMetadata::KEY_ID]['type']);
84106
$this->assertEquals('string', $schema['properties'][ProviderMetadata::KEY_NAME]['type']);
85107
$this->assertEquals('string', $schema['properties'][ProviderMetadata::KEY_TYPE]['type']);
108+
$this->assertEquals('string', $schema['properties'][ProviderMetadata::KEY_CREDENTIALS_URL]['type']);
86109

87110
// Check enum values for type
88111
$this->assertArrayHasKey('enum', $schema['properties'][ProviderMetadata::KEY_TYPE]);
@@ -110,7 +133,8 @@ public function testToArray(): void
110133
$this->assertEquals('anthropic', $array[ProviderMetadata::KEY_ID]);
111134
$this->assertEquals('Anthropic', $array[ProviderMetadata::KEY_NAME]);
112135
$this->assertEquals('cloud', $array[ProviderMetadata::KEY_TYPE]);
113-
$this->assertCount(3, $array);
136+
$this->assertNull($array[ProviderMetadata::KEY_CREDENTIALS_URL]);
137+
$this->assertCount(4, $array);
114138
}
115139

116140
/**
@@ -123,7 +147,8 @@ public function testFromArray(): void
123147
$data = [
124148
ProviderMetadata::KEY_ID => 'custom-provider',
125149
ProviderMetadata::KEY_NAME => 'Custom Provider',
126-
ProviderMetadata::KEY_TYPE => 'server'
150+
ProviderMetadata::KEY_TYPE => 'server',
151+
ProviderMetadata::KEY_CREDENTIALS_URL => 'https://example.com/credentials',
127152
];
128153

129154
$metadata = ProviderMetadata::fromArray($data);
@@ -132,6 +157,7 @@ public function testFromArray(): void
132157
$this->assertEquals('custom-provider', $metadata->getId());
133158
$this->assertEquals('Custom Provider', $metadata->getName());
134159
$this->assertTrue($metadata->getType()->isServer());
160+
$this->assertEquals('https://example.com/credentials', $metadata->getCredentialsUrl());
135161
}
136162

137163
/**

0 commit comments

Comments
 (0)