summaryrefslogtreecommitdiff
path: root/src/marlin.agents.shell/MarlinApiClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/marlin.agents.shell/MarlinApiClient.cs')
-rw-r--r--src/marlin.agents.shell/MarlinApiClient.cs33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/marlin.agents.shell/MarlinApiClient.cs b/src/marlin.agents.shell/MarlinApiClient.cs
index aa4f013..900101c 100644
--- a/src/marlin.agents.shell/MarlinApiClient.cs
+++ b/src/marlin.agents.shell/MarlinApiClient.cs
@@ -1,16 +1,28 @@
namespace marlin.agents.shell;
using System.Net.Http.Json;
+using core.contracts;
using core.models;
+using Grpc.Core;
+using Grpc.Net.Client;
+using ProtoBuf.Grpc.Client;
public class MarlinApiClient
{
+ private readonly string _baseUrl;
+ private readonly Guid _agentId;
+ private readonly string _agentToken;
+
private readonly HttpClient _http;
public MarlinApiClient(string baseUrl, Guid agentId, string agentToken)
{
- _http = new HttpClient { BaseAddress = new Uri(baseUrl) };
- _http.DefaultRequestHeaders.Add("Agent", $"{agentId} {agentToken}");
+ this._baseUrl = baseUrl;
+ this._agentId = agentId;
+ this._agentToken = agentToken;
+
+ _http = new HttpClient { BaseAddress = new Uri(this._baseUrl) };
+ _http.DefaultRequestHeaders.Add("Agent", $"{this._agentId} {this._agentToken}");
}
public static MarlinApiClient FromEnvironment()
@@ -35,4 +47,21 @@ public class MarlinApiClient
public async Task<DispatchJob?> QueueNextJob(CancellationToken ct = default)
=> await _http.GetFromJsonAsync<DispatchJob?>("api/dispatch/queue", ct);
+
+ public GrpcChannel CreateJobScopedRpc(DispatchJob job)
+ {
+ var credentials = CallCredentials.FromInterceptor(
+ async (_, metadata) =>
+ {
+ metadata.Add("Agent", $"{this._agentId} {this._agentToken}");
+ metadata.Add("Job", $"{job.Id} {job.Token}");
+ });
+
+ var channel = GrpcChannel.ForAddress(this._baseUrl, new GrpcChannelOptions
+ {
+ Credentials = ChannelCredentials.Create(new SslCredentials(), credentials)
+ });
+
+ return channel;
+ }
}