Skip to content

Commit

Permalink
CP-47509: Expose RequestHeaders and ResponseHeaders in C# SDK.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <[email protected]>
  • Loading branch information
kc284 committed Feb 13, 2024
1 parent e17089c commit 4079d84
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
27 changes: 26 additions & 1 deletion ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public JsonRpcClient(string baseUrl)
public bool PreAuthenticate { get; set; }
public CookieContainer Cookies { get; set; }
public RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; set; }
public Dictionary<string, string> RequestHeaders { get; set; }
public Dictionary<string, string> ResponseHeaders { get; set; }

public string Url { get; private set; }

Expand Down Expand Up @@ -285,14 +287,32 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
webRequest.CookieContainer = Cookies ?? webRequest.CookieContainer ?? new CookieContainer();
webRequest.ServerCertificateValidationCallback = ServerCertificateValidationCallback ?? ServicePointManager.ServerCertificateValidationCallback;

if (RequestHeaders != null)
{
foreach (var header in RequestHeaders)
webRequest.Headers.Add(header.Key, header.Value);
}

using (var str = webRequest.GetRequestStream())
{
postStream.CopyTo(str);
str.Flush();
}

using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
HttpWebResponse webResponse = null;
try
{
webResponse = (HttpWebResponse)webRequest.GetResponse();

ResponseHeaders = new Dictionary<string, string>();

if (webResponse.Headers != null)
{
var keys = webResponse.Headers.AllKeys;
foreach (var key in keys)
ResponseHeaders.Add(key, string.Join(",", webResponse.Headers.Get(key)));
}

if (webResponse.StatusCode != HttpStatusCode.OK)
throw new WebException(webResponse.StatusCode.ToString());

Expand All @@ -305,6 +325,11 @@ protected virtual void PerformPostRequest(Stream postStream, Stream responseStre
responseStream.Flush();
}
}
finally
{
RequestHeaders = null;
webResponse?.Dispose();
}
}

private JsonSerializerSettings CreateSettings(IList<JsonConverter> converters)
Expand Down
19 changes: 19 additions & 0 deletions ocaml/sdk-gen/csharp/autogen/src/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ public RemoteCertificateValidationCallback ServerCertificateValidationCallback

public ICredentials Credentials => JsonRpcClient?.WebProxy?.Credentials;

/// <summary>
/// Optional headers in name-value pairs to be passed in the HttpWebRequests. The
/// default value is null. This property can be set by the implementing code before
/// each request. It is automatically reset to null once the request has been sent.
/// </summary>
public Dictionary<string, string> RequestHeaders
{
set => JsonRpcClient.RequestHeaders = value;
get => JsonRpcClient.RequestHeaders;
}

/// <summary>
/// Exposes the headers returned in the HttpWebResponses in name-value pairs.
/// This property is set once a response is received. The values are comma
/// separated strings of header values stored in a header.
/// It returns an empty dictionary if no headers are found.
/// </summary>
public Dictionary<string, string> ResponseHeaders => JsonRpcClient.ResponseHeaders;

/// <summary>
/// Always true before API version 1.6.
/// Filled in after successful session_login_with_password for 1.6 or newer connections
Expand Down

0 comments on commit 4079d84

Please sign in to comment.