forked from aliencube/azure-openai-sdk-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backend API] Implement endpoint for new resource details aliencube#308
- Loading branch information
Showing
9 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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
65 changes: 65 additions & 0 deletions
65
src/AzureOpenAIProxy.ApiApp/Repositories/AdminResourceRepository.cs
This file contains 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,65 @@ | ||
using Azure.Data.Tables; | ||
|
||
using AzureOpenAIProxy.ApiApp.Configurations; | ||
using AzureOpenAIProxy.ApiApp.Models; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Repositories; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the <see cref="AdminResourceRepository"/> class. | ||
/// </summary> | ||
public interface IAdminResourceRepository | ||
{ | ||
/// <summary> | ||
/// Creates a new record of resource details. | ||
/// </summary> | ||
/// <param name="resourceDetails">Resource details instance.</param> | ||
/// <returns>Returns the resource details instance created.</returns> | ||
Task<AdminResourceDetails> CreateResource(AdminResourceDetails resourceDetails); | ||
} | ||
|
||
/// <summary> | ||
/// This represents the repository entity for the admin resource. | ||
/// </summary> | ||
public class AdminResourceRepository(TableServiceClient tableServiceClient, StorageAccountSettings storageAccountSettings) : IAdminResourceRepository | ||
{ | ||
private readonly TableServiceClient _tableServiceClient = tableServiceClient ?? throw new ArgumentNullException(nameof(tableServiceClient)); | ||
private readonly StorageAccountSettings _storageAccountSettings = storageAccountSettings ?? throw new ArgumentNullException(nameof(storageAccountSettings)); | ||
|
||
/// <inheritdoc /> | ||
public async Task<AdminResourceDetails> CreateResource(AdminResourceDetails resourceDetails) | ||
{ | ||
TableClient tableClient = await GetTableClientAsync(); | ||
|
||
await tableClient.AddEntityAsync(resourceDetails).ConfigureAwait(false); | ||
|
||
return resourceDetails; | ||
} | ||
|
||
private async Task<TableClient> GetTableClientAsync() | ||
{ | ||
TableClient tableClient = _tableServiceClient.GetTableClient(_storageAccountSettings.TableStorage.TableName); | ||
|
||
await tableClient.CreateIfNotExistsAsync().ConfigureAwait(false); | ||
|
||
return tableClient; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This represents the extension class for <see cref="IServiceCollection"/> | ||
/// </summary> | ||
public static class AdminResourceRepositoryExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the <see cref="AdminResourceRepository"/> instance to the service collection. | ||
/// </summary> | ||
/// <param name="services"><see cref="IServiceCollection"/> instance.</param> | ||
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns> | ||
public static IServiceCollection AddAdminResourceRepository(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IAdminResourceRepository, AdminResourceRepository>(); | ||
|
||
return services; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/AzureOpenAIProxy.ApiApp/Services/AdminResourceService.cs
This file contains 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,57 @@ | ||
using AzureOpenAIProxy.ApiApp.Models; | ||
using AzureOpenAIProxy.ApiApp.Repositories; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Services; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the <see cref="AdminResourceService"/> class. | ||
/// </summary> | ||
public interface IAdminResourceService | ||
{ | ||
/// <summary> | ||
/// Creates a new resource. | ||
/// </summary> | ||
/// <param name="resourceDetails">Resource payload.</param> | ||
/// <returns>Returns the resource payload created.</returns> | ||
Task<AdminResourceDetails> CreateResource(AdminResourceDetails resourceDetails); | ||
} | ||
|
||
/// <summary> | ||
/// This represents the service entity for admin resource. | ||
/// </summary> | ||
public class AdminResourceService : IAdminResourceService | ||
{ | ||
private readonly IAdminResourceRepository _repository; | ||
|
||
public AdminResourceService(IAdminResourceRepository repository) | ||
{ | ||
_repository = repository ?? throw new ArgumentNullException(nameof(repository)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<AdminResourceDetails> CreateResource(AdminResourceDetails resourceDetails) | ||
{ | ||
resourceDetails.PartitionKey = PartitionKeys.ResourceDetails; | ||
resourceDetails.RowKey = resourceDetails.ResourceId.ToString(); | ||
|
||
var result = await _repository.CreateResource(resourceDetails).ConfigureAwait(false); | ||
return result; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This represents the extension class for <see cref="IServiceCollection"/>. | ||
/// </summary> | ||
public static class AdminResourceServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the <see cref="AdminResourceService"/> instance to the service collection. | ||
/// </summary> | ||
/// <param name="services"><see cref="IServiceCollection"/> instance.</param> | ||
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns> | ||
public static IServiceCollection AddAdminResourceService(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IAdminResourceService, AdminResourceService>(); | ||
return services; | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
}, | ||
"StorageAccount": { | ||
"TableStorage": { | ||
"TableName": "events" | ||
"TableName": "resources" | ||
} | ||
} | ||
}, | ||
|
19 changes: 19 additions & 0 deletions
19
test/AzureOpenAIProxy.ApiApp.Tests/Repositories/AdminResourceRepositoryTests.cs
This file contains 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 @@ | ||
using Azure; | ||
using Azure.Data.Tables; | ||
|
||
using AzureOpenAIProxy.ApiApp.Configurations; | ||
using AzureOpenAIProxy.ApiApp.Models; | ||
using AzureOpenAIProxy.ApiApp.Repositories; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using NSubstitute; | ||
using NSubstitute.ExceptionExtensions; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Tests.Repositories; | ||
|
||
public class AdminResourceRepositoryTests | ||
{ | ||
} |
18 changes: 18 additions & 0 deletions
18
test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminResourceServiceTests.cs
This file contains 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,18 @@ | ||
using Azure; | ||
|
||
using AzureOpenAIProxy.ApiApp.Models; | ||
using AzureOpenAIProxy.ApiApp.Repositories; | ||
using AzureOpenAIProxy.ApiApp.Services; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using NSubstitute; | ||
using NSubstitute.ExceptionExtensions; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Tests.Services; | ||
|
||
public class AdminResourceServiceTests | ||
{ | ||
} |