Skip to content

Commit

Permalink
updated sdk to 0.2.0 supporting auth v1 and api v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzorz committed Jul 3, 2023
1 parent 0b51629 commit 2b736bc
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
19 changes: 9 additions & 10 deletions sources/Contenda/Contenda.Sdk/Auth/APIKeyAuthProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Contenda.Sdk.Exceptions;
using Contenda.Sdk.Models.Request;
using Contenda.Sdk.Models.Result;
Expand Down Expand Up @@ -56,11 +56,11 @@ public async Task<bool> TryAuthenticate(string apiBaseUri)

var client = PoorMansHttpClientFactory.Instance.Client;

var uri = $"{apiBaseUri}{Constants.Version2Prefix}{Constants.IdentityV2.Token}";
var body = new TokenV2
var uri = $"{apiBaseUri}{Constants.AuthVersion1Prefix}{Constants.AuthV1.Token}";
var body = new AuthV1Token
{
api_key = _apiKey,
email = _email
user_email = _email
};
var bodyString = JsonConvert.SerializeObject(body);

Expand All @@ -79,25 +79,24 @@ public async Task<bool> TryAuthenticate(string apiBaseUri)
}
}

private bool EnsureValid() => DateTime.Now < ValidUntil();
private bool EnsureValid() => DateTime.UtcNow < ValidUntil();

/// <inheritdoc />
public void ModifyHeadersCallback(HttpClient httpClient)
{
if (!EnsureValid()) throw new AuthenticationException("You have to be authenticated to call APIs with authentication.");
// nothing to do here
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiToken);
}

/// <inheritdoc />
public string ModifyQueryCallback(string currentQuery)
{
if (!EnsureValid()) throw new AuthenticationException("You have to be authenticated to call APIs with authentication.");

var nvc = HttpUtility.ParseQueryString((new Uri(currentQuery)).Query);
var paramChar = nvc.Count == 0 ? "?" : "&";
return $"{currentQuery}{paramChar}token={HttpUtility.UrlEncode(_apiToken)}";
}
// nothing to do here

return currentQuery;
}

}
}
14 changes: 8 additions & 6 deletions sources/Contenda/Contenda.Sdk/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
{
internal static class Constants
{
internal const string BaseApiUri = "https://prod.contenda.io/";
internal const string BaseApiUri = "https://prod.contenda.co/";

internal const string Version2Prefix = "api/v2/";
internal const string MainVersion3Prefix = "api/v3/";

internal const string AuthVersion1Prefix = "auth/v1/";

internal const string Health = "health";

internal const string ClientUserAgent = "Contenda .NET SDK";

internal const string JsonMimeType = "application/json";

internal static class IdentityV2
internal static class AuthV1
{
internal const string Token = "identity/token";
internal const string Token = "flow/apilogin";
}

internal static class JobsV2
internal static class JobsV3
{
internal const string Status = "jobs/status/";

Expand All @@ -26,7 +28,7 @@ internal static class JobsV2
internal const string UsageLimits = "jobs/usage-limits";
}

internal static class ContentV2
internal static class ContentV3
{
internal const string BlogMarkdown = "content/blog/{0}/markdown";
}
Expand Down
6 changes: 3 additions & 3 deletions sources/Contenda/Contenda.Sdk/Contenda.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<FileVersion>0.1.1.0</FileVersion>
<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
<Title>Contenda .NET SDK</Title>
<Authors>tomzorz</Authors>
<Company>Contenda</Company>
Expand All @@ -16,7 +16,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/Contenda-Team/contenda-dotnet-sdk</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>0.1.1</Version>
<Version>0.2.0</Version>
<RepositoryUrl>https://github.com/Contenda-Team/contenda-dotnet-sdk</RepositoryUrl>
<PackageIcon>logo_white.png</PackageIcon>
</PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions sources/Contenda/Contenda.Sdk/ContendaAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<bool> Authenticate()
public async Task<UsageLimits?> GetUsageLimits()
{
var client = PoorMansHttpClientFactory.Instance.Client;
var uri = $"{_apiBaseUri}{Constants.Version2Prefix}{Constants.JobsV2.UsageLimits}";
var uri = $"{_apiBaseUri}{Constants.MainVersion3Prefix}{Constants.JobsV3.UsageLimits}";

_authProvider.ModifyHeadersCallback(client);
uri = _authProvider.ModifyQueryCallback(uri);
Expand All @@ -84,7 +84,7 @@ public async Task<bool> Authenticate()
public async Task<JobStatusResult?> GetJobStatus(string jobId)
{
var client = PoorMansHttpClientFactory.Instance.Client;
var uri = $"{_apiBaseUri}{Constants.Version2Prefix}{Constants.JobsV2.Status}{HttpUtility.UrlEncode(jobId)}";
var uri = $"{_apiBaseUri}{Constants.MainVersion3Prefix}{Constants.JobsV3.Status}{HttpUtility.UrlEncode(jobId)}";

_authProvider.ModifyHeadersCallback(client);
uri = _authProvider.ModifyQueryCallback(uri);
Expand Down Expand Up @@ -126,12 +126,12 @@ public async Task<bool> Authenticate()
public async Task<JobStatusResult?> SubmitVideoToBlogJob(string sourceId, VideoToBlogJobSubType subType, string? statusUpdateWebhookUrl = null, string? statusUpdateEmail = null)
{
var client = PoorMansHttpClientFactory.Instance.Client;
var uri = $"{_apiBaseUri}{Constants.Version2Prefix}{Constants.JobsV2.SubmitVideoToBlog}";
var uri = $"{_apiBaseUri}{Constants.MainVersion3Prefix}{Constants.JobsV3.SubmitVideoToBlog}";

_authProvider.ModifyHeadersCallback(client);
uri = _authProvider.ModifyQueryCallback(uri);

var content = new StringContent(JsonConvert.SerializeObject(new SubmitJobV2
var content = new StringContent(JsonConvert.SerializeObject(new SubmitJobV3
{
source_id = sourceId,
status_update_email = statusUpdateEmail,
Expand All @@ -153,7 +153,7 @@ public async Task<bool> Authenticate()
public async Task<string> GetBlogAsMarkdown(string blogId)
{
var client = PoorMansHttpClientFactory.Instance.Client;
var uri = $"{_apiBaseUri}{Constants.Version2Prefix}{Constants.ContentV2.BlogMarkdown}";
var uri = $"{_apiBaseUri}{Constants.MainVersion3Prefix}{Constants.ContentV3.BlogMarkdown}";

uri = string.Format(uri, blogId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Contenda.Sdk.Models.Request
{
internal class TokenV2
internal class AuthV1Token
{
public string? email { [UsedImplicitly] get; set; }
public string? user_email { [UsedImplicitly] get; set; }

public string? api_key { [UsedImplicitly] get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Contenda.Sdk.Models.Request
{
internal class SubmitJobV2
internal class SubmitJobV3
{
public string? source_id { [UsedImplicitly] get; set; }

Expand Down
2 changes: 0 additions & 2 deletions sources/Contenda/Contenda.Sdk/Models/Result/TokenResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace Contenda.Sdk.Models.Result
{
internal class TokenResult
{
[UsedImplicitly] public string? valid_until { get; set; }

public string? access_token { get; [UsedImplicitly] set; }
}
}
2 changes: 2 additions & 0 deletions sources/Contenda/Contenda.SdkDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

Console.WriteLine($"Successfully submitted job: {jobId}");

await Task.Delay(TimeSpan.FromSeconds(5));

var jobStatus = await api.GetJobStatus(jobId!);

while (jobStatus!.Status != "succeeded" && jobStatus.Status != "failed")
Expand Down

0 comments on commit 2b736bc

Please sign in to comment.