Skip to content

Commit

Permalink
Merge pull request #53 from deepgram/requiressl
Browse files Browse the repository at this point in the history
Adding flag to require SSL or not
  • Loading branch information
briancbarrow authored Nov 30, 2022
2 parents 777bbfc + b4f17a7 commit fbf9ddd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Deepgram/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public Credentials()
/// </summary>
/// <param name="apiKey">Deepgram API Key</param>
/// <param name="apiUrl">Url of Deepgram API</param>
public Credentials(string apiKey = null, string apiUrl = null)
/// <param name="requireSSL">Require SSL on requests</param>
public Credentials(string apiKey = null, string apiUrl = null, bool requireSSL = true)
{
ApiKey = apiKey;
ApiUrl = apiUrl;
RequireSSL = requireSSL;
}

/// <summary>
Expand All @@ -29,5 +31,10 @@ public Credentials(string apiKey = null, string apiUrl = null)
/// On-premise Url of the Deepgram API
/// </summary>
public string ApiUrl { get; set; } = null;

/// <summary>
/// Require SSL on requests
/// </summary>
public Nullable<bool> RequireSSL { get; set; } = null;
}
}
15 changes: 14 additions & 1 deletion Deepgram/DeepgramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void InitializeCredentials(Credentials credentials = null)
{
string apiUrl = string.IsNullOrWhiteSpace(credentials?.ApiUrl) ? "" : credentials.ApiUrl;
string apiKey = string.IsNullOrWhiteSpace(credentials?.ApiKey) ? "" : credentials.ApiKey;
Nullable<bool> requireSSL = credentials?.RequireSSL;

if (string.IsNullOrEmpty(apiKey))
{
Expand All @@ -72,7 +73,19 @@ private void InitializeCredentials(Credentials credentials = null)
apiUrl = possibleUri;
}
}
_credentials = new CleanCredentials(apiKey, apiUrl);
if (!requireSSL.HasValue)
{
string possibleRequireSSL = Configuration.Instance.Settings["appSettings:Deepgram.Api.RequireSSL"];
if (string.IsNullOrEmpty(possibleRequireSSL))
{
requireSSL = true;
}
else
{
requireSSL = Convert.ToBoolean(possibleRequireSSL);
}
}
_credentials = new CleanCredentials(apiKey, apiUrl, requireSSL.Value);
}

private void InitializeClients()
Expand Down
8 changes: 5 additions & 3 deletions Deepgram/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Deepgram.Request
public class ApiRequest
{
const string LOGGER_CATEGORY = "Deepgram.Request.ApiRequest";

private static void SetHeaders(ref HttpRequestMessage request, CleanCredentials credentials)
{
request.Headers.Add("Accept", "application/json");
Expand Down Expand Up @@ -89,12 +89,14 @@ internal static async Task<T> DoStreamRequestAsync<T>(HttpMethod method, string

private static Uri GetUriWithQuerystring(CleanCredentials credentials, string uri, object queryParameters = null)
{
string protocol = credentials.RequireSSL ? "https" : "http";

if (null != queryParameters)
{
var querystring = Helpers.GetParameters(queryParameters);
return new Uri($"https://{credentials.ApiUrl}{uri}?{querystring}");
return new Uri($"{protocol}://{credentials.ApiUrl}{uri}?{querystring}");
}
return new Uri($"https://{credentials.ApiUrl}{uri}");
return new Uri($"{protocol}://{credentials.ApiUrl}{uri}");
}

private static async Task<T> SendHttpRequestAsync<T>(HttpRequestMessage request)
Expand Down
12 changes: 9 additions & 3 deletions Deepgram/Request/CleanCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal class CleanCredentials
/// </summary>
/// <param name="apiKey">Deepgram API Key</param>
/// <param name="apiUrl">Url of Deepgram API</param>
public CleanCredentials(string apiKey, string apiUrl)
/// <param name="requireSSL">Require SSL on requests</param>
public CleanCredentials(string apiKey, string apiUrl, bool requireSSL)
{
ApiKey = apiKey;

Expand All @@ -21,6 +22,7 @@ public CleanCredentials(string apiKey, string apiUrl)
}

ApiUrl = apiUrl;
RequireSSL = requireSSL;
}

/// <summary>
Expand All @@ -33,10 +35,14 @@ public CleanCredentials(string apiKey, string apiUrl)
/// </summary>
public string ApiUrl { get; set; }

/// <summary>
/// Require SSL on requests
/// </summary>
public bool RequireSSL { get; set; }

public Credentials ToCredentials()
{
return new Credentials(ApiKey, ApiUrl);
return new Credentials(ApiKey, ApiUrl, RequireSSL);
}

}
}
6 changes: 4 additions & 2 deletions Deepgram/Transcription/LiveTranscriptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@ public void SendData(byte[] data)

private Uri GetWSSUriWithQuerystring(string uri, LiveTranscriptionOptions queryParameters)
{
string protocol = _credentials.RequireSSL ? "wss" : "ws";

if (null != queryParameters)
{
var queryString = Helpers.GetParameters(queryParameters);
return new Uri($"wss://{_credentials.ApiUrl}{uri}?{queryString}");
return new Uri($"{protocol}://{_credentials.ApiUrl}{uri}?{queryString}");
}
return new Uri($"wss://{_credentials.ApiUrl}{uri}");
return new Uri($"{protocol}://{_credentials.ApiUrl}{uri}");
}

private async Task ProcessSenderQueue()
Expand Down

0 comments on commit fbf9ddd

Please sign in to comment.