diff --git a/.gitignore b/.gitignore index 7a35653..32ab2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/obj/ **/target/ /build +/umogenkey.secret diff --git a/README.md b/README.md index 851ad49..5f85025 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# umogen +# Silly Integrated Unified System for Accounting and Contract Handling + +**Working name:** umogen An AI powered document generator. diff --git a/src/Nyanlabs.Umogen.Core/UmoEngine.cs b/src/Nyanlabs.Umogen.Core/UmoEngine.cs new file mode 100644 index 0000000..3b543df --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/UmoEngine.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Headers; +using System.Threading.Tasks; + +namespace Nyanlabs.Umogen.Core; + +public class UmoEngine : IDisposable +{ + public UmoEngine(string? apiKey = null, bool isFilePath = true) + { + if (isFilePath) + { + apiKey ??= Umogen.DEFAULT_API_KEY_FILE; + if (!File.Exists(apiKey)) + { + throw new ArgumentException($"No such file: {apiKey}"); + } + + string contents = File.ReadAllText(apiKey).Trim(); + if (string.IsNullOrWhiteSpace(apiKey)) + { + throw new ArgumentException($"File {apiKey} is empty."); + } + + apiKey = contents; + } + else + { + if (string.IsNullOrWhiteSpace(apiKey)) + { + throw new ArgumentException("API key cannot be empty", nameof(apiKey)); + } + } + + apiKey = apiKey.Trim(); + _http = new() + { + BaseAddress = new Uri("https://api.openai.com/v1/") + }; + _http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); + } + + private readonly HttpClient _http; + + public async Task ValidateKey() + { + //Use the 'models' endpoint for auth + var resp = await _http.GetAsync("models", HttpCompletionOption.ResponseHeadersRead); + return resp.IsSuccessStatusCode; + } + + public void Dispose() + { + GC.SuppressFinalize(this); + _http.Dispose(); + } +} diff --git a/src/Nyanlabs.Umogen.Core/Umogen.cs b/src/Nyanlabs.Umogen.Core/Umogen.cs new file mode 100644 index 0000000..64861c1 --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/Umogen.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Nyanlabs.Umogen.Core; + +public class Umogen +{ + public const string DEFAULT_API_KEY_FILE = "umogenkey.secret"; +} diff --git a/test/Nyanlabs.Umogen.CoreTests/AuthTests.cs b/test/Nyanlabs.Umogen.CoreTests/AuthTests.cs new file mode 100644 index 0000000..b89c827 --- /dev/null +++ b/test/Nyanlabs.Umogen.CoreTests/AuthTests.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Nyanlabs.Umogen.Core; + +namespace Nyanlabs.Umogen.CoreTests; + +public class AuthTests +{ + [Fact] + public async Task TestTokenVerification() + { + // Given + var rootDir = Environment.CurrentDirectory.Replace("test/Nyanlabs.Umogen.CoreTests/bin/Debug/net8.0", ""); + + UmoEngine eng = new(Path.Combine(rootDir, Core.Umogen.DEFAULT_API_KEY_FILE)); + + // When + var result = await eng.ValidateKey(); + + // Then + Assert.True(result); + } +}