Skip to content

Commit 1aa2e99

Browse files
committed
Add sanity check test, fix namespace via C# options
1 parent cd3fa1c commit 1aa2e99

19 files changed

+376
-31
lines changed

.netconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,8 @@
180180
sha = 407aa2d9319f5db12964540810b446fecc22d419
181181
etag = 0dca55f20a72d3279554837f4eba867a1de37fe0f4a7535c2d9bc43867361cc5
182182
weak
183+
[file "src/Tests/Attributes.cs"]
184+
url = https://github.com/devlooped/catbag/blob/main/Xunit/Attributes.cs
185+
sha = 40914971d4d6b42d6f8a90923b131136f7e609a5
186+
etag = c77e7b435ce1df06fb60a3b0e15a0833d8e45d4d19f366c6184140ebb4814b1a
187+
weak

GrokClient.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<Solution>
22
<Project Path="src/GrokClient/GrokClient.csproj" Id="61e079f0-56ec-4fb5-97cb-4dfdb8981d07" />
3+
<Project Path="src/Tests/Tests.csproj" Id="c22563af-5b36-4100-9f31-93629b7bb17c" />
34
</Solution>

src/GrokClient/Extensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Google.Protobuf.WellKnownTypes;
5+
6+
namespace Devlooped.Grok
7+
{
8+
public static class ModelsClientExtensions
9+
{
10+
extension(Models.ModelsClient client)
11+
{
12+
public async Task<IEnumerable<LanguageModel>> ListLanguageModelsAsync(CancellationToken cancellation = default)
13+
{
14+
var models = await client.ListLanguageModelsAsync(new Empty(), cancellationToken: cancellation);
15+
return models.Models;
16+
}
17+
}
18+
}
19+
}
20+
21+
namespace Devlooped.Grok
22+
{
23+
/// <summary>
24+
/// An API service that let users get details of available models on the
25+
/// platform.
26+
/// </summary>
27+
partial class Models
28+
{
29+
static Models() => __ServiceName = "xai_api.Models";
30+
}
31+
}

src/GrokClient/GrokServiceCollectionExtensions.cs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,110 @@
11
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
24
using Grpc.Net.ClientFactory;
35
using Microsoft.Extensions.DependencyInjection;
46

57
namespace Devlooped.Grok;
68

79
public static class GrokServiceCollectionExtensions
810
{
9-
public static IServiceCollection AddGrokClient(this IServiceCollection services, Action<GrpcClientFactoryOptions>? configure = null)
11+
public static IServiceCollection AddGrokClient(this IServiceCollection services, string apiKey,
12+
Action<GrpcClientFactoryOptions>? configureClient = null,
13+
Action<IHttpClientBuilder>? configureHttp = null)
1014
{
11-
var address = new Uri("https://api.x.ai:443");
12-
15+
var address = new Uri("https://api.x.ai/");
16+
17+
Action<HttpClient> http = client => client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {apiKey}");
18+
1319
services.AddGrpcClient<Auth.AuthClient>(options =>
1420
{
1521
options.Address = address;
16-
configure?.Invoke(options);
22+
configureClient?.Invoke(options);
23+
})
24+
.AddCallCredentials((context, metadata) =>
25+
{
26+
metadata.Add("Authorization", $"Bearer {apiKey}");
27+
return Task.CompletedTask;
1728
});
1829

1930
services.AddGrpcClient<Chat.ChatClient>(options =>
2031
{
2132
options.Address = address;
22-
configure?.Invoke(options);
33+
configureClient?.Invoke(options);
34+
})
35+
.AddCallCredentials((context, metadata) =>
36+
{
37+
metadata.Add("Authorization", $"Bearer {apiKey}");
38+
return Task.CompletedTask;
2339
});
2440

2541
services.AddGrpcClient<Embedder.EmbedderClient>(options =>
2642
{
2743
options.Address = address;
28-
configure?.Invoke(options);
44+
configureClient?.Invoke(options);
45+
})
46+
.AddCallCredentials((context, metadata) =>
47+
{
48+
metadata.Add("Authorization", $"Bearer {apiKey}");
49+
return Task.CompletedTask;
2950
});
3051

3152
services.AddGrpcClient<Image.ImageClient>(options =>
3253
{
3354
options.Address = address;
34-
configure?.Invoke(options);
55+
configureClient?.Invoke(options);
56+
})
57+
.AddCallCredentials((context, metadata) =>
58+
{
59+
metadata.Add("Authorization", $"Bearer {apiKey}");
60+
return Task.CompletedTask;
3561
});
3662

37-
services.AddGrpcClient<Models.ModelsClient>(options =>
63+
var builder = services.AddGrpcClient<Models.ModelsClient>(options =>
3864
{
3965
options.Address = address;
40-
configure?.Invoke(options);
41-
});
66+
configureClient?.Invoke(options);
67+
}).ConfigureHttpClient(http);
68+
69+
configureHttp?.Invoke(builder);
70+
71+
//.AddCallCredentials((context, metadata) =>
72+
//{
73+
// metadata.Add("Authorization", $"Bearer {apiKey}");
74+
// return Task.CompletedTask;
75+
//});
4276

4377
services.AddGrpcClient<Sample.SampleClient>(options =>
4478
{
4579
options.Address = address;
46-
configure?.Invoke(options);
80+
configureClient?.Invoke(options);
81+
})
82+
.AddCallCredentials((context, metadata) =>
83+
{
84+
metadata.Add("Authorization", $"Bearer {apiKey}");
85+
return Task.CompletedTask;
4786
});
4887

4988
services.AddGrpcClient<Tokenize.TokenizeClient>(options =>
5089
{
5190
options.Address = address;
52-
configure?.Invoke(options);
91+
configureClient?.Invoke(options);
92+
})
93+
.AddCallCredentials((context, metadata) =>
94+
{
95+
metadata.Add("Authorization", $"Bearer {apiKey}");
96+
return Task.CompletedTask;
5397
});
5498

5599
services.AddGrpcClient<Documents.DocumentsClient>(options =>
56100
{
57101
options.Address = address;
58-
configure?.Invoke(options);
102+
configureClient?.Invoke(options);
103+
})
104+
.AddCallCredentials((context, metadata) =>
105+
{
106+
metadata.Add("Authorization", $"Bearer {apiKey}");
107+
return Task.CompletedTask;
59108
});
60109

61110
return services;

src/GrokClient/auth.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
import "google/protobuf/empty.proto";
67
import "google/protobuf/timestamp.proto";

src/GrokClient/chat.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
import "google/protobuf/timestamp.proto";
67
import "deferred.proto";

src/GrokClient/deferred.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
// The response from the service, when creating a deferred completion request.
67
message StartDeferredResponse {

src/GrokClient/documents.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
service Documents {
67
rpc Search(SearchRequest) returns (SearchResponse) {}

src/GrokClient/embed.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
import "image.proto";
67
import "usage.proto";

src/GrokClient/image.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
syntax = "proto3";
2+
option csharp_namespace = "Devlooped.Grok";
23

3-
package Devlooped.Grok;
4+
package xai_api;
45

56
// An API service for interaction with image generation models.
67
service Image {

0 commit comments

Comments
 (0)