Skip to content

Commit

Permalink
SDK regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed Aug 1, 2024
1 parent ba2a065 commit 93db068
Show file tree
Hide file tree
Showing 109 changed files with 1,958 additions and 701 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ jobs:
- name: Build Release
run: dotnet build src -c Release /p:ContinuousIntegrationBuild=true

unit-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v3

- uses: actions/checkout@master

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.x

- name: Install tools
run: |
dotnet tool restore
- name: Run Tests
run: |
dotnet test src
publish:
needs: [compile]
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,3 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

.idea
.runsettings
24 changes: 24 additions & 0 deletions src/AssemblyAI.Test/AssemblyAI.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
<PackageReference Include="coverlet.collector" Version="3.2.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AssemblyAI\AssemblyAI.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions src/AssemblyAI.Test/TestClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NUnit.Framework;

#nullable enable

namespace AssemblyAI.Test;

[TestFixture]
public class TestClient { }
26 changes: 10 additions & 16 deletions src/AssemblyAI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyAI", "AssemblyAI\AssemblyAI.csproj", "{6908A082-04F2-4B91-92F1-671DEA06C378}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyAI", "AssemblyAI\AssemblyAI.csproj", "{B2E2B823-FB0F-4DA2-9FF3-91B73BDA8C08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyAI.UnitTests", "AssemblyAI.UnitTests\AssemblyAI.UnitTests.csproj", "{4C5A0575-B891-42AA-8F7A-92424E2E27C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyAI.IntegrationTests", "AssemblyAI.IntegrationTests\AssemblyAI.IntegrationTests.csproj", "{F68D755B-BF68-496C-860B-1F7CB8F579D3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyAI.Test", "AssemblyAI.Test\AssemblyAI.Test.csproj", "{2D62C667-A356-46C8-920E-589231969C89}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,17 +16,13 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6908A082-04F2-4B91-92F1-671DEA06C378}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6908A082-04F2-4B91-92F1-671DEA06C378}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6908A082-04F2-4B91-92F1-671DEA06C378}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6908A082-04F2-4B91-92F1-671DEA06C378}.Release|Any CPU.Build.0 = Release|Any CPU
{4C5A0575-B891-42AA-8F7A-92424E2E27C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C5A0575-B891-42AA-8F7A-92424E2E27C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C5A0575-B891-42AA-8F7A-92424E2E27C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C5A0575-B891-42AA-8F7A-92424E2E27C0}.Release|Any CPU.Build.0 = Release|Any CPU
{F68D755B-BF68-496C-860B-1F7CB8F579D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F68D755B-BF68-496C-860B-1F7CB8F579D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F68D755B-BF68-496C-860B-1F7CB8F579D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F68D755B-BF68-496C-860B-1F7CB8F579D3}.Release|Any CPU.Build.0 = Release|Any CPU
{B2E2B823-FB0F-4DA2-9FF3-91B73BDA8C08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2E2B823-FB0F-4DA2-9FF3-91B73BDA8C08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2E2B823-FB0F-4DA2-9FF3-91B73BDA8C08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2E2B823-FB0F-4DA2-9FF3-91B73BDA8C08}.Release|Any CPU.Build.0 = Release|Any CPU
{2D62C667-A356-46C8-920E-589231969C89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D62C667-A356-46C8-920E-589231969C89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D62C667-A356-46C8-920E-589231969C89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D62C667-A356-46C8-920E-589231969C89}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
27 changes: 27 additions & 0 deletions src/AssemblyAI/Core/AssemblyAIClientApiException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using AssemblyAI.Core;

#nullable enable

namespace AssemblyAI.Core;

/// <summary>
/// This exception type will be thrown for any non-2XX API responses.
/// </summary>
public class AssemblyAIClientApiException(string message, int statusCode, object body)
: AssemblyAIClientException(message)
{
/// <summary>
/// The error code of the response that triggered the exception.
/// </summary>
public int StatusCode { get; } = statusCode;

/// <summary>
/// The body of the response that triggered the exception.
/// </summary>
public object Body { get; } = body;

public override string ToString()
{
return $"AssemblyAIClientApiException {{ message: {Message}, statusCode: {StatusCode}, body: {Body} }}";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AssemblyAI.Core;

public class Environments
public class AssemblyAIClientEnvironment
{
public static string DEFAULT = "https://api.assemblyai.com";
}
11 changes: 11 additions & 0 deletions src/AssemblyAI/Core/AssemblyAIClientException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

#nullable enable

namespace AssemblyAI.Core;

/// <summary>
/// Base exception class for all exceptions thrown by the SDK.
/// </summary>
public class AssemblyAIClientException(string message, Exception? innerException = null)
: Exception(message, innerException) { }
30 changes: 30 additions & 0 deletions src/AssemblyAI/Core/ClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Net.Http;
using AssemblyAI.Core;

#nullable enable

namespace AssemblyAI.Core;

public partial class ClientOptions
{
/// <summary>
/// The Base URL for the API.
/// </summary>
public string BaseUrl { get; init; } = AssemblyAIClientEnvironment.DEFAULT;

/// <summary>
/// The http client used to make requests.
/// </summary>
public HttpClient HttpClient { get; init; } = new HttpClient();

/// <summary>
/// The http client used to make requests.
/// </summary>
public int MaxRetries { get; init; } = 2;

/// <summary>
/// The timeout for the request.
/// </summary>
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(30);
}
6 changes: 3 additions & 3 deletions src/AssemblyAI/Core/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AssemblyAI.Core;

internal static class Constants
public static class Constants
{
internal const string Version = "0.0.2-alpha";
}
public const string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK";
}
22 changes: 22 additions & 0 deletions src/AssemblyAI/Core/DateTimeSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace AssemblyAI.Core;

public class DateTimeSerializer : JsonConverter<DateTime>
{
public override DateTime Read(
ref Utf8JsonReader reader,
System.Type typeToConvert,
JsonSerializerOptions options
)
{
return DateTime.Parse(reader.GetString()!, null, DateTimeStyles.RoundtripKind);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(Constants.DateTimeFormat));
}
}
30 changes: 30 additions & 0 deletions src/AssemblyAI/Core/JsonConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.Json;

namespace AssemblyAI.Core;

public static class JsonOptions
{
public static readonly JsonSerializerOptions JsonSerializerOptions;

static JsonOptions()
{
JsonSerializerOptions = new JsonSerializerOptions
{
Converters = { new DateTimeSerializer() },
WriteIndented = true
};
}
}

public static class JsonUtils
{
public static string Serialize<T>(T obj)
{
return JsonSerializer.Serialize(obj, JsonOptions.JsonSerializerOptions);
}

public static T Deserialize<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, JsonOptions.JsonSerializerOptions)!;
}
}
57 changes: 43 additions & 14 deletions src/AssemblyAI/Core/RawClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Net.Http;
using System.Text;
using System.Text.Json;

namespace AssemblyAI.Core;

Expand All @@ -9,12 +8,16 @@ namespace AssemblyAI.Core;
/// <summary>
/// Utility class for making raw HTTP requests to the API.
/// </summary>
public class RawClient(Dictionary<string, string> headers, ClientOptions clientOptions)
public class RawClient(
Dictionary<string, string> headers,
Dictionary<string, Func<string>> headerSuppliers,
ClientOptions clientOptions
)
{
/// <summary>
/// The http client used to make requests.
/// </summary>
private readonly ClientOptions _clientOptions = clientOptions;
public readonly ClientOptions Options = clientOptions;

/// <summary>
/// Global headers to be sent with every request.
Expand All @@ -23,7 +26,7 @@ public class RawClient(Dictionary<string, string> headers, ClientOptions clientO

public async Task<ApiResponse> MakeRequestAsync(BaseApiRequest request)
{
var url = BuildUrl(request.Path, request.Query);
var url = BuildUrl(request);
var httpRequest = new HttpRequestMessage(request.Method, url);
if (request.ContentType != null)
{
Expand All @@ -34,6 +37,11 @@ public async Task<ApiResponse> MakeRequestAsync(BaseApiRequest request)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
// Add global headers to the request from supplier
foreach (var header in headerSuppliers)
{
httpRequest.Headers.Add(header.Key, header.Value.Invoke());
}
// Add request headers to the request
foreach (var header in request.Headers)
{
Expand All @@ -44,9 +52,8 @@ public async Task<ApiResponse> MakeRequestAsync(BaseApiRequest request)
{
if (jsonRequest.Body != null)
{
var serializerOptions = new JsonSerializerOptions { WriteIndented = true, };
httpRequest.Content = new StringContent(
JsonSerializer.Serialize(jsonRequest.Body, serializerOptions),
JsonUtils.Serialize(jsonRequest.Body),
Encoding.UTF8,
"application/json"
);
Expand All @@ -57,12 +64,15 @@ public async Task<ApiResponse> MakeRequestAsync(BaseApiRequest request)
httpRequest.Content = new StreamContent(streamRequest.Body);
}
// Send the request
var response = await _clientOptions.HttpClient.SendAsync(httpRequest);
var httpClient = request.Options?.HttpClient ?? Options.HttpClient;
var response = await httpClient.SendAsync(httpRequest);
return new ApiResponse { StatusCode = (int)response.StatusCode, Raw = response };
}

public record BaseApiRequest
{
public required string BaseUrl { get; init; }

public required HttpMethod Method { get; init; }

public required string Path { get; init; }
Expand All @@ -73,7 +83,7 @@ public record BaseApiRequest

public Dictionary<string, string> Headers { get; init; } = new();

public object? RequestOptions { get; init; }
public RequestOptions? Options { get; init; }
}

/// <summary>
Expand Down Expand Up @@ -102,17 +112,36 @@ public record ApiResponse
public required HttpResponseMessage Raw { get; init; }
}

private string BuildUrl(string path, Dictionary<string, object> query)
private string BuildUrl(BaseApiRequest request)
{
var trimmedBaseUrl = _clientOptions.BaseUrl.TrimEnd('/');
var trimmedBasePath = path.TrimStart('/');
var baseUrl = request.Options?.BaseUrl ?? request.BaseUrl;
var trimmedBaseUrl = baseUrl.TrimEnd('/');
var trimmedBasePath = request.Path.TrimStart('/');
var url = $"{trimmedBaseUrl}/{trimmedBasePath}";
if (query.Count <= 0)
if (request.Query.Count <= 0)
return url;
url += "?";
url = query.Aggregate(
url = request.Query.Aggregate(
url,
(current, queryItem) => current + $"{queryItem.Key}={queryItem.Value}&"
(current, queryItem) =>
{
if (queryItem.Value is System.Collections.IEnumerable collection and not string)
{
var items = collection
.Cast<object>()
.Select(value => $"{queryItem.Key}={value}")
.ToList();
if (items.Any())
{
current += string.Join("&", items) + "&";
}
}
else
{
current += $"{queryItem.Key}={queryItem.Value}&";
}
return current;
}
);
url = url.Substring(0, url.Length - 1);
return url;
Expand Down
Loading

0 comments on commit 93db068

Please sign in to comment.