Skip to content

Commit edf39fc

Browse files
committed
add content gzip compression for cloudconnection
1 parent 7ef495c commit edf39fc

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a Client SDK for RelationalAI
44

5-
- API version: 1.2.1
5+
- API version: 1.2.2
66

77
## Frameworks supported
88

RelationalAI/KGMSClient.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class GeneratedRelationalAIClient
2222

2323
public const string JSON_CONTENT_TYPE = "application/json";
2424
public const string CSV_CONTENT_TYPE = "text/csv";
25-
public const string USER_AGENT_HEADER = "KGMSClient/1.2.1/csharp";
25+
public const string USER_AGENT_HEADER = "KGMSClient/1.2.2/csharp";
2626

2727
public int DebugLevel = Connection.DEFAULT_DEBUG_LEVEL;
2828

@@ -53,14 +53,25 @@ partial void PrepareRequest(Transaction body, HttpClient client, HttpRequestMess
5353
}
5454
if(conn is CloudConnection) {
5555
query["compute_name"] = conn.ComputeName;
56+
// Note:
57+
// We need to send the gzip content encoding header and a gzip compressed body only in case of a CloudConnection.
58+
// Local rai-server cannot handle gzip encoding, only infra server support does.
59+
60+
// Compress the contents (request body) as gzipped byte array. C# httpclient does not implicitly compress the content over the wire
61+
// if content encoding is gzip; we need to manually compress the body.
62+
// Note: If the client sends content-encoding as gzip but does not encode the content to gzip, then the server will return 400 Bad Request.
63+
request.Content = CompressionUtils.CompressRequestContentAsGzip(request.Content);
64+
//Set the content encoding type header as gzip. It will tell the server that the content is gzip encoded.
65+
request.Content.Headers.Add("content-encoding", "gzip");
5666
}
5767
uriBuilder.Query = query.ToString();
5868
request.RequestUri = uriBuilder.Uri;
5969

6070
// populate headers
6171
request.Headers.Host = request.RequestUri.Host;
72+
//Set the content type header
6273
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
63-
74+
6475
// sign request here
6576
var raiRequest = new RAIRequest(request, conn);
6677
raiRequest.Sign(debugLevel: DebugLevel);

RelationalAI/RAIRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void Sign(string[] includeHeaders = null, int debugLevel=1) {
6161

6262
public void Sign(DateTime t, string[] includeHeaders = null, int debugLevel=1) {
6363
if(includeHeaders == null) {
64-
includeHeaders = new string[]{"host", "content-type", "x-rai-date"};
64+
//Adding a new header content-encoding for signature purposes.
65+
includeHeaders = new string[]{"host", "content-type", "x-rai-date", "content-encoding"};
6566
}
6667
if(this.Creds == null) return;
6768

RelationalAI/RelationalAI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>1.2.1</Version>
4+
<Version>1.2.2</Version>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<PackageId>RelationalAI</PackageId>
77
<Authors>RelationalAI</Authors>

RelationalAI/Util.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Text;
8+
using System.IO;
9+
using System.IO.Compression;
10+
using System.Net.Http;
811

912
namespace Com.RelationalAI
1013
{
@@ -170,4 +173,36 @@ public static string FlattenException(Exception exception)
170173
return stringBuilder.ToString();
171174
}
172175
}
176+
177+
public static class CompressionUtils{
178+
/**
179+
This function will take HttpContent object and then perform the following
180+
- Initialize a memory stream and copy the contents as bytes into it; called the Content Stream.
181+
- Initialize a gzip stream with a compressed stream.
182+
- Compress the content stream as bytes using gzip and copy it to the Compressed Stream.
183+
- Return the HttpContent with compressed stream as bytes
184+
*/
185+
public static HttpContent CompressRequestContentAsGzip(HttpContent content)
186+
{
187+
//Initialize a memory stream to hold the compressed contents.
188+
var compressedStream = new MemoryStream();
189+
190+
//Read bytes from HttpContent into byte array
191+
byte[] byteArray = content.ReadAsByteArrayAsync().Result;
192+
//Initialize a memory stream with byte array content to compress it.
193+
using (var contentStream = new MemoryStream(byteArray))
194+
{
195+
//Initialize the gzip stream and feed compressed stream to copy the compressed contents.
196+
using (var gzipStream = new GZipStream(compressedStream, System.IO.Compression.CompressionMode.Compress))
197+
{
198+
//Copy the contents to gzip stream. Gzip stream will compress and copy them to the compressed stream that we fed earlier.
199+
contentStream.CopyTo(gzipStream);
200+
}
201+
}
202+
//Make the HttpContent as byte array content
203+
var httpContent = new ByteArrayContent(compressedStream.ToArray());
204+
//Return the byte array content.
205+
return httpContent;
206+
}
207+
}
173208
}

default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let
99
in
1010
stdenv.mkDerivation rec {
1111
name = "rai-server-csharp-client-sdk-${version}";
12-
version = "1.2.1";
12+
version = "1.2.2";
1313
buildInputs = [
1414
raiserverBinary
1515
dotnet-sdk_3

0 commit comments

Comments
 (0)