Skip to content

Commit 9b32611

Browse files
Merge pull request #29 from open-olive/SIDE-969-network-sensor-headers-node-csharp
[SIDE-969] - network sensor headers in C# and NodeJS clients
2 parents 62776a9 + a7d234d commit 9b32611

File tree

11 files changed

+673
-13
lines changed

11 files changed

+673
-13
lines changed

ldk/csharp/OliveHelpsLDK/OliveHelpsLDK/Network/INetworkService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Threading;
23
using System.Threading.Tasks;
34

@@ -15,12 +16,16 @@ public struct HTTPRequest
1516
public string Method;
1617

1718
public byte[] Body;
19+
20+
public IDictionary<string, IList<string>> Headers;
1821
}
1922

2023
public struct HTTPResponse
2124
{
2225
public int ResponseCode;
2326

2427
public byte[] Data;
28+
29+
public IDictionary<string, IList<string>> Headers;
2530
}
2631
}

ldk/csharp/OliveHelpsLDK/OliveHelpsLDK/Network/NetworkClient.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System.Collections.Generic;
12
using System.Threading;
23
using System.Threading.Tasks;
34
using Google.Protobuf;
5+
using Google.Protobuf.Collections;
46
using Grpc.Core;
57
using OliveHelpsLDK.Logging;
68
using Proto;
@@ -21,26 +23,62 @@ internal NetworkClient(ChannelBase channelBase, Session session, ILogger logger)
2123

2224

2325
public Task<HTTPResponse> HTTPRequest(HTTPRequest request, CancellationToken cancellationToken = default)
26+
{
27+
var message = ToProto(request);
28+
29+
return Client.HTTPRequestAsync(message, CreateOptions(cancellationToken)).ResponseAsync
30+
.ContinueWith(LoggedParser<Task<HTTPResponseMsg>, HTTPResponse>(task => FromProto(task.Result)),
31+
cancellationToken);
32+
}
33+
34+
private HTTPRequestMsg ToProto(HTTPRequest request)
2435
{
2536
var message = new HTTPRequestMsg
2637
{
2738
Session = CreateSession(),
2839
Body = ByteString.CopyFrom(request.Body),
2940
Method = request.Method,
3041
Url = request.URL
31-
};
32-
return Client.HTTPRequestAsync(message, CreateOptions(cancellationToken)).ResponseAsync
33-
.ContinueWith(LoggedParser<Task<HTTPResponseMsg>, HTTPResponse>(task => FromProto(task.Result)),
34-
cancellationToken);
42+
};
43+
44+
AddHeadersToMessage(message, request.Headers);
45+
46+
return message;
47+
}
48+
49+
private static void AddHeadersToMessage(HTTPRequestMsg message, IDictionary<string, IList<string>> requestHeaders)
50+
{
51+
foreach (var kvp in requestHeaders)
52+
{
53+
var header = new HTTPHeader();
54+
header.Values.Add(kvp.Value);
55+
message.Headers.Add(kvp.Key, header);
56+
}
3557
}
3658

3759
internal static HTTPResponse FromProto(HTTPResponseMsg response)
3860
{
39-
return new HTTPResponse
61+
var clientResponse = new HTTPResponse
4062
{
4163
Data = response.ToByteArray(),
42-
ResponseCode = checked((int) response.ResponseCode)
64+
ResponseCode = checked((int) response.ResponseCode),
65+
Headers = ParseHeaders(response.Headers)
4366
};
67+
68+
return clientResponse;
69+
}
70+
71+
private static IDictionary<string, IList<string>> ParseHeaders(MapField<string, HTTPHeader> headers)
72+
{
73+
var parsedHeaders = new Dictionary<string, IList<string>>();
74+
75+
foreach (var kvp in headers)
76+
{
77+
var values = new List<string>(kvp.Value.Values);
78+
parsedHeaders.Add(kvp.Key, values);
79+
}
80+
81+
return parsedHeaders;
4482
}
4583
}
4684
}

ldk/node/dist/grpc/network_pb.d.ts

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)