Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shim to target Vonage APIs #254

Merged
merged 8 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions BlazorHelloWorld/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,32 @@
<div id="subscribers">
</div>
</div>
<div>
<button class="btn btn-success" onclick="@StartArchiving" disabled="@isArchiving">Start archiving</button>
<button class="btn btn-danger" onclick="@StopArchiving" disabled="@(!isArchiving)">Stop archiving</button>
</div>
</div>

@code {
private bool isArchiving => this.archiveId != Guid.Empty;
private SessionCredentials session;
private Guid archiveId;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
var session = Service.CreateSession();
await JavascriptRuntime.InvokeVoidAsync("initializeStream", Service.GetApiKey(), session.SessionId, session.Token);
this.session = Service.CreateSession();
await JavascriptRuntime.InvokeVoidAsync("initializeStream", Service.GetOpenTokId(), session.SessionId, session.Token);
}
}

private async Task StartArchiving()
{
var archive = await Service.StartArchiving(this.session.SessionId);
this.archiveId = archive.Id;
}

private Task StopArchiving() => Service.StopArchiving(this.archiveId.ToString());
}
32 changes: 28 additions & 4 deletions BlazorHelloWorld/Infrastructure/VideoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@ namespace BlazorHelloWorld.Infrastructure;
public class VideoService
{
private readonly OpenTok openTok;
private readonly string apiKey;

public VideoService(OpenTokOptions options)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
openTok = new OpenTok(Convert.ToInt32(options.ApiKey), options.ApiSecret);
this.openTok = InitializeClient(options);
}

private OpenTok InitializeClient(OpenTokOptions options)
{
if (!string.IsNullOrEmpty(options.ApplicationId) && !string.IsNullOrEmpty(options.PrivateKey))
{
return new OpenTok(options.ApplicationId, options.PrivateKey);
}

if (!string.IsNullOrEmpty(options.ApiKey) && !string.IsNullOrEmpty(options.ApiSecret)
&& int.TryParse(options.ApiKey, out var key))
{
return new OpenTok(key, options.ApiSecret);
}

throw new ArgumentException("Missing credentials");
}

public SessionCredentials CreateSession()
Expand All @@ -23,12 +40,19 @@ public SessionCredentials CreateSession()
return new SessionCredentials(session.Id, session.GenerateToken());
}

public int GetApiKey()
public string GetOpenTokId() => this.openTok.GetOpenTokId();

public async Task<Archive> StartArchiving(string sessionId)
{
return await openTok.StartArchiveAsync(sessionId);
}

public async Task StopArchiving(string archiveId)
{
return openTok.ApiKey;
await openTok.StopArchiveAsync(archiveId);
}
}

public record OpenTokOptions(string ApiKey, string ApiSecret);
public record OpenTokOptions(string ApiKey, string ApiSecret, string ApplicationId, string PrivateKey);

public record SessionCredentials(string SessionId, string Token);
2 changes: 1 addition & 1 deletion BlazorHelloWorld/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var options = builder.Configuration.GetSection(nameof(OpenTokOptions)).Get<OpenTokOptions>();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<OpenTokOptions>(options);
builder.Services.AddSingleton(options);
builder.Services.AddScoped<VideoService>();
var app = builder.Build();
app.UseHttpsRedirection();
Expand Down
4 changes: 3 additions & 1 deletion BlazorHelloWorld/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"AllowedHosts": "*",
"OpenTokOptions": {
"ApiKey": "",
"ApiSecret": ""
"ApiSecret": "",
"ApplicationId": "",
"PrivateKey": ""
}
}
4 changes: 2 additions & 2 deletions OpenTok/OpenTok.AudioConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class OpenTok
public async Task<AudioConnector> StartAudioConnectorAsync(AudioConnectorStartRequest request)
{
var response = await this.Client.PostAsync(
$"v2/project/{this.ApiKey}/connect",
$"v2/project/{this.GetOpenTokId()}/connect",
GetHeaderDictionary("application/json"),
request.ToDataDictionary());
return JsonConvert.DeserializeObject<AudioConnector>(response);
Expand All @@ -26,7 +26,7 @@ public async Task<AudioConnector> StartAudioConnectorAsync(AudioConnectorStartRe
/// <param name="connectionId">The OpenTok connection ID for the Audio Connector WebSocket connection in the OpenTok session. See <see cref="AudioConnector.ConnectionId"/>.</param>
public async Task StopAudioConnectorAsync(string connectionId) =>
_ = await this.Client.PostAsync(
$"v2/project/{this.ApiKey}/connect/{connectionId}/stop",
$"v2/project/{this.GetOpenTokId()}/connect/{connectionId}/stop",
new Dictionary<string, string>(),
new Dictionary<string, object>());
}
Expand Down
2 changes: 1 addition & 1 deletion OpenTok/OpenTok.Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ await this.Client.DeleteAsync(
this.BuildUrlWithRouteParameter(RenderEndpoint, renderId),
new Dictionary<string, string>());

private string BuildUrl(string endpoint) => $"v2/project/{this.ApiKey}{endpoint}";
private string BuildUrl(string endpoint) => $"v2/project/{this.GetOpenTokId()}{endpoint}";

private string BuildUrlWithQueryParameter(string endpoint, string queryParameter) =>
$"{this.BuildUrl(endpoint)}?{queryParameter}";
Expand Down
Loading
Loading