-
Notifications
You must be signed in to change notification settings - Fork 34
feat: support short-lived token endpoint #369
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
using Deepgram.Clients.Auth.v1; | ||
using Deepgram.Models.Authenticate.v1; | ||
|
||
namespace Deepgram; | ||
|
||
/// <summary> | ||
/// Implements the latest supported version of the Auth Client. | ||
/// </summary> | ||
public class AuthClient : Client | ||
{ | ||
public AuthClient(string apiKey = "", DeepgramHttpClientOptions? deepgramClientOptions = null, | ||
string? httpId = null) : base(apiKey, deepgramClientOptions, httpId) | ||
{ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
using Deepgram.Models.Authenticate.v1; | ||
using Deepgram.Models.Auth.v1; | ||
using Deepgram.Clients.Interfaces.v1; | ||
using Deepgram.Abstractions.v1; | ||
|
||
namespace Deepgram.Clients.Auth.v1; | ||
|
||
/// <summary> | ||
/// Implements version 1 of the Models Client. | ||
/// </summary> | ||
/// <param name="apiKey">Required DeepgramApiKey</param> | ||
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param> | ||
public class Client(string? apiKey = null, IDeepgramClientOptions? deepgramClientOptions = null, string? httpId = null) | ||
: AbstractRestClient(apiKey, deepgramClientOptions, httpId), IAuthClient | ||
{ | ||
/// <summary> | ||
/// Gets a temporary JWT for the Deepgram API. | ||
/// </summary> | ||
/// <returns><see cref="GrantTokenResponse"/></returns> | ||
public async Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default, | ||
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null) | ||
{ | ||
Log.Verbose("AuthClient.GrantToken", "ENTER"); | ||
|
||
var uri = GetUri(_options, $"auth/{UriSegments.GRANTTOKEN}"); | ||
var result = await PostAsync<object, GrantTokenResponse>(uri, null, cancellationToken, addons, headers); | ||
|
||
Log.Information("GrantToken", $"{uri} Succeeded"); | ||
Log.Debug("GrantToken", $"result: {result}"); | ||
Log.Verbose("AuthClient.GrantToken", "LEAVE"); | ||
|
||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
namespace Deepgram.Clients.Auth.v1; | ||
|
||
public static class UriSegments | ||
{ | ||
//using constants instead of inline value(magic strings) make consistence | ||
//across SDK And Test Projects Simpler and Easier to change | ||
public const string GRANTTOKEN = "grant"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
using Deepgram.Models.Auth.v1; | ||
|
||
namespace Deepgram.Clients.Interfaces.v1; | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Implements version 1 of the Auth Client. | ||
/// </summary> | ||
/// <param name="apiKey">Required DeepgramApiKey</param> | ||
/// <param name="deepgramClientOptions"><see cref="DeepgramHttpClientOptions"/> for HttpClient Configuration</param> | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public interface IAuthClient | ||
{ | ||
/// <summary> | ||
/// Gets a short-lived JWT for the Deepgram API. | ||
/// </summary> | ||
/// <returns><see cref="GrantTokenResponse"/></returns> | ||
public Task<GrantTokenResponse> GrantToken(CancellationTokenSource? cancellationToken = default, | ||
Dictionary<string, string>? addons = null, Dictionary<string, string>? headers = null); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
namespace Deepgram.Models.Auth.v1; | ||
|
||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public record GrantTokenResponse | ||
{ | ||
/// <summary> | ||
/// Access token for the Deepgram API | ||
/// </summary> | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
[JsonPropertyName("access_token")] | ||
public string? AccessToken { get; set; } | ||
|
||
/// <summary> | ||
/// TTL of the access token in seconds | ||
/// </summary> | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
[JsonPropertyName("expires_in")] | ||
public decimal? ExpiresIn { get; set; } | ||
|
||
/// <summary> | ||
/// Override ToString method to serialize the object | ||
/// </summary> | ||
public override string ToString() | ||
{ | ||
return Regex.Unescape(JsonSerializer.Serialize(this, JsonSerializeOptions.DefaultOptions)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Deepgram\Deepgram.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Deepgram" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved. | ||
// Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
using Deepgram.Models.Auth.v1; | ||
|
||
namespace SampleApp | ||
naomi-lgbt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
// Initialize Library with default logging | ||
// Normal logging is "Info" level | ||
Library.Initialize(); | ||
|
||
// use the client factory with a API Key set with the "DEEPGRAM_API_KEY" environment variable | ||
var deepgramClient = ClientFactory.CreateAuthClient(); | ||
|
||
// generate token | ||
var tokenResp = await deepgramClient.GrantToken(); | ||
if (tokenResp == null) | ||
{ | ||
Console.WriteLine("GrantToken failed."); | ||
Environment.Exit(1); | ||
} | ||
|
||
string token = tokenResp.AccessToken; | ||
string ttl = tokenResp.ExpiresIn.ToString(); | ||
Console.WriteLine($"Token: {token}"); | ||
Console.WriteLine($"TTL: {ttl}"); | ||
|
||
Console.WriteLine("\n\nPress any key to exit."); | ||
Console.ReadKey(); | ||
|
||
// Teardown Library | ||
Library.Terminate(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.