|
1 | 1 | using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
2 | 4 | using Grpc.Net.ClientFactory; |
3 | 5 | using Microsoft.Extensions.DependencyInjection; |
4 | 6 |
|
5 | 7 | namespace Devlooped.Grok; |
6 | 8 |
|
7 | 9 | public static class GrokServiceCollectionExtensions |
8 | 10 | { |
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) |
10 | 14 | { |
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 | + |
13 | 19 | services.AddGrpcClient<Auth.AuthClient>(options => |
14 | 20 | { |
15 | 21 | 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; |
17 | 28 | }); |
18 | 29 |
|
19 | 30 | services.AddGrpcClient<Chat.ChatClient>(options => |
20 | 31 | { |
21 | 32 | 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; |
23 | 39 | }); |
24 | 40 |
|
25 | 41 | services.AddGrpcClient<Embedder.EmbedderClient>(options => |
26 | 42 | { |
27 | 43 | 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; |
29 | 50 | }); |
30 | 51 |
|
31 | 52 | services.AddGrpcClient<Image.ImageClient>(options => |
32 | 53 | { |
33 | 54 | 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; |
35 | 61 | }); |
36 | 62 |
|
37 | | - services.AddGrpcClient<Models.ModelsClient>(options => |
| 63 | + var builder = services.AddGrpcClient<Models.ModelsClient>(options => |
38 | 64 | { |
39 | 65 | 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 | + //}); |
42 | 76 |
|
43 | 77 | services.AddGrpcClient<Sample.SampleClient>(options => |
44 | 78 | { |
45 | 79 | 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; |
47 | 86 | }); |
48 | 87 |
|
49 | 88 | services.AddGrpcClient<Tokenize.TokenizeClient>(options => |
50 | 89 | { |
51 | 90 | 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; |
53 | 97 | }); |
54 | 98 |
|
55 | 99 | services.AddGrpcClient<Documents.DocumentsClient>(options => |
56 | 100 | { |
57 | 101 | 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; |
59 | 108 | }); |
60 | 109 |
|
61 | 110 | return services; |
|
0 commit comments