Skip to content

Commit

Permalink
changed to InitializeApiValuesAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Dec 7, 2023
1 parent d01c728 commit 40ccdc2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
31 changes: 21 additions & 10 deletions LocalGuideAI/LocalGuideAI.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PropertyGroup>
<Authors>Daniel Collingwood</Authors>
<Description>Simple AI travel guide app for any platform.</Description>
<RepositoryUrl>https://github.com/danzuep/LocalGuideAI</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PublishDir>./publish</PublishDir>
<PackageReleaseNotes>
0.1.0 Simple AI travel guide
</PackageReleaseNotes>

<TargetFrameworks>net8.0-android</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('macOS'))">$(TargetFrameworks);net8.0-ios;net8.0-macios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
Expand Down Expand Up @@ -33,34 +42,36 @@

<!-- C# Hot Reload -->
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>


<!-- %APPDATA%\Microsoft\UserSecrets\ -->
<UserSecretsId>LocalGuideAI-69b33f45-de4c-4083-9a70-939952647ea2</UserSecretsId>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
<UserSecretsId>LocalGuideAI-69b33f45-de4c-4083-9a70-939952647ea2</UserSecretsId>
</PropertyGroup>

</PropertyGroup>

<!-- https://learn.microsoft.com/en-us/dotnet/maui/android/deployment/publish-cli?view=net-maui-8.0#define-build-properties-in-your-project-file -->
<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
<OutputType>WinExe</OutputType>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>

<!--<PropertyGroup Condition="$(TargetFramework.Contains('-android')) and '$(Configuration)' == 'Release'">
<AndroidKeyStore>True</AndroidKeyStore>
<PropertyGroup Condition="$(TargetFramework.Contains('-android')) and '$(Configuration)' == 'Release'">
<AndroidPackageFormats>apk</AndroidPackageFormats>
<AndroidKeyStore>True</AndroidKeyStore>
<AndroidSigningKeyStore>../release/android.keystore</AndroidSigningKeyStore>
<AndroidSigningKeyAlias>android-key</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>env:AndroidSigningPassword</AndroidSigningKeyPass>
<AndroidSigningStorePass>env:AndroidSigningPassword</AndroidSigningStorePass>
</PropertyGroup>-->
</PropertyGroup>

<!--<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
</PropertyGroup>-->
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
Expand Down
38 changes: 5 additions & 33 deletions LocalGuideAI/Services/ChatGptClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,18 @@ namespace LocalGuideAI.Services
{
internal sealed class ChatGptClientFactory : IChatGptClientFactory
{
private readonly string? _apiKey;
private readonly string? _proxyUrl;
private readonly ValueTask _initialization;

public ChatGptClientFactory(IConfiguration configuration)
{
(_apiKey, _proxyUrl) = InitializeApiValues(configuration);
}

static (string?, string?) InitializeApiValues(IConfiguration configuration, string sectionName = "Azure")
{
var section = configuration.GetSection(sectionName);
var apiUrl = InitializeProxyUrl(section);
var apiKey = InitializeApiKey(section);
return (apiKey, apiUrl);
}

static string? InitializeApiKey(IConfiguration section, string? apiKeyValue = null, string apiKeyName = "ApiKey")
{
var apiKey = section[apiKeyName];
if (!string.IsNullOrWhiteSpace(apiKey))
apiKeyValue = apiKey;
if (!string.IsNullOrWhiteSpace(apiKeyValue))
StorageHelper.ApiKey = apiKeyValue;
return apiKeyValue;
}

static string? InitializeProxyUrl(IConfiguration section, string? proxyUrl = "https://aoai.hacktogether.net", string apiUrlName = "ProxyUrl")
{
var apiUrl = section[apiUrlName];
if (!string.IsNullOrWhiteSpace(apiUrl))
proxyUrl = apiUrl;
if (!string.IsNullOrWhiteSpace(proxyUrl))
StorageHelper.ApiUrl = proxyUrl;
return proxyUrl;
_initialization = StorageHelper.InitializeApiValuesAsync(configuration);
}

public async Task<OpenAIClient> CreateAsync()
{
var apiKey = await StorageHelper.GetKeyAsync() ?? _apiKey;
var proxyUrl = await StorageHelper.GetUrlAsync() ?? _proxyUrl;
await _initialization;
var apiKey = await StorageHelper.GetKeyAsync();
var proxyUrl = await StorageHelper.GetUrlAsync();
ArgumentException.ThrowIfNullOrWhiteSpace(apiKey, nameof(apiKey));
// the full key is appended by "/YOUR-GITHUB-ALIAS"
AzureKeyCredential token = new(apiKey);
Expand Down
41 changes: 28 additions & 13 deletions LocalGuideAI/Services/StorageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
namespace LocalGuideAI.Services
using Microsoft.Extensions.Configuration;

namespace LocalGuideAI.Services
{
internal sealed class StorageHelper
{
internal static readonly string _storageKeyName = "chat_gpt_api_key";
internal static readonly string _storageUrlName = "chat_gpt_api_url";

internal static string? ApiKey
{
get => GetKeyAsync().GetAwaiter().GetResult();
set => SetKeyAsync(value).GetAwaiter().GetResult();
}

internal static string? ApiUrl
{
get => GetUrlAsync().GetAwaiter().GetResult();
set => SetUrlAsync(value).GetAwaiter().GetResult();
}

internal static async Task<string?> GetKeyAsync() =>
await SecureStorage.Default.GetAsync(_storageKeyName);

Expand All @@ -28,5 +18,30 @@ internal static async Task SetKeyAsync(string? value) =>

internal static async Task SetUrlAsync(string? value) =>
await SecureStorage.Default.SetAsync(_storageUrlName, value ?? string.Empty);

internal static async ValueTask InitializeApiValuesAsync(IConfiguration configuration, string sectionName = "Azure")
{
var section = configuration.GetSection(sectionName);
await InitializeApiKeyAsync(section);
await InitializeProxyUrlAsync(section);
}

static async ValueTask InitializeApiKeyAsync(IConfiguration section, string? apiKeyValue = null, string apiKeyName = "ApiKey")
{
var apiKey = section[apiKeyName];
if (!string.IsNullOrWhiteSpace(apiKey))
apiKeyValue = apiKey;
if (!string.IsNullOrWhiteSpace(apiKeyValue))
await SetKeyAsync(apiKeyValue);
}

static async ValueTask InitializeProxyUrlAsync(IConfiguration section, string? proxyUrl = "https://aoai.hacktogether.net", string apiUrlName = "ProxyUrl")
{
var apiUrl = section[apiUrlName];
if (!string.IsNullOrWhiteSpace(apiUrl))
proxyUrl = apiUrl;
if (!string.IsNullOrWhiteSpace(proxyUrl))
await SetUrlAsync(proxyUrl);
}
}
}
12 changes: 11 additions & 1 deletion Scripts/publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ $projectName = $projectDirectory.BaseName;
$projectAppName = "${projectName}";
$projectFile="${projectFolder}/${projectAppName}/${projectAppName}.csproj";

# #$dotnetVersion="8.0.x";
# $NuGetLink="https://api.nuget.org/v3/index.json"
# # https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json
# $SourceLink="https://aka.ms/dotnet8/nuget/index.json"
# # https://maui.blob.core.windows.net/metadata/rollbacks/net8.0.json
# $RollbackLink="https://aka.ms/dotnet/maui/net8.0.json"
# $PackageLinks="--from-rollback-file ${RollbackLink} --source ${SourceLink} --source ${NuGetLink}"
# dotnet workload install maui-android ${PackageLinks}

# restore the base project dependencies
Set-Location -Path "${projectFolder}";
dotnet restore "${projectFile}";
Expand Down Expand Up @@ -44,7 +53,8 @@ Write-Host "Local .NET Version: "; dotnet --version;
Write-Host "Target Framework: ${targetFramework}";
Write-Host "Project File: ${projectFile} (version ${buildVersion})";

dotnet publish "${projectFile}" -c $configuration --framework $targetFramework /p:Version=$buildVersion /p:AndroidPackageFormats=$androidPackageFormats /p:AndroidKeyStore=true /p:AndroidSigningKeyStore="${kestorePath}" /p:AndroidSigningKeyAlias="${androidSigningAlias}" /p:AndroidSigningKeyPass="${Env:AndroidSigningPassword}" /p:AndroidSigningStorePass="${Env:AndroidSigningPassword}" -o "${publishOutputFolder}" --no-restore --nologo;
# dotnet publish "${projectFile}" -c $configuration --framework $targetFramework /p:Version=$buildVersion /p:AndroidPackageFormats=$androidPackageFormats /p:AndroidKeyStore=true /p:AndroidSigningKeyStore="${kestorePath}" /p:AndroidSigningKeyAlias="${androidSigningAlias}" /p:AndroidSigningKeyPass=env:AndroidSigningPassword /p:AndroidSigningStorePass=env:AndroidSigningPassword -o "${publishOutputFolder}" --no-restore --nologo;
dotnet publish "${projectFile}" -c $configuration -f $targetFramework /p:Version=$buildVersion --no-restore --nologo;
if (-not $?) {
Write-Host "Project failed to publish.";
Exit 1;
Expand Down

0 comments on commit 40ccdc2

Please sign in to comment.