-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
366 additions
and
135 deletions.
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
src/AzureOpenAIProxy.ApiApp/Controllers/ManagementAccessCodeController.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,165 @@ | ||
using AzureOpenAIProxy.ApiApp.Models; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Controllers; | ||
|
||
/// <summary> | ||
/// This represents the controller entity for management. | ||
/// </summary> | ||
public partial class ManagementController | ||
{ | ||
/// <summary> | ||
/// Gets the list of access codes by event ID. | ||
/// </summary> | ||
/// <param name="apiKey">API key.</param> | ||
/// <param name="eventId">Event ID.</param> | ||
/// <param name="page">Page number.</param> | ||
/// <param name="size">Page size.</param> | ||
/// <returns>Returns the <see cref="AccessCodeResponseCollection"/> instance.</returns> | ||
[HttpGet("events/{eventId}/access-codes", Name = "GetListOfEventAccessCodes")] | ||
public async Task<IActionResult> GetEventAccessCodesByEventIdAsync( | ||
[FromHeader(Name = "Authorization")] string apiKey, | ||
[FromRoute] string eventId, | ||
[FromQuery(Name = "page")] int? page = 0, | ||
[FromQuery(Name = "size")] int? size = 20) | ||
{ | ||
this._logger.LogInformation("Received a request to get the list of access codes for the given event"); | ||
|
||
var record = await this._auth.ValidateAsync(apiKey); | ||
if (record == null) | ||
{ | ||
this._logger.LogError("Invalid API key"); | ||
|
||
return new UnauthorizedResult(); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(eventId)) | ||
{ | ||
this._logger.LogError("No event ID"); | ||
|
||
return new NotFoundResult(); | ||
} | ||
|
||
try | ||
{ | ||
var result = await this._management.GetAccessCodesAsync(eventId, page, size); | ||
|
||
this._logger.LogInformation("Completed the request to get the list of access codes for the given event"); | ||
|
||
return new OkObjectResult(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, "Failed to get the list of access codes for the given event"); | ||
|
||
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates the access code. | ||
/// </summary> | ||
/// <param name="apiKey">API key.</param> | ||
/// <param name="eventId">Event ID.</param> | ||
/// <param name="req"><see cref="AccessCodeRequest"/> instance.</param> | ||
/// <returns>Returns the <see cref="AccessCodeResponse"/> instance.</returns> | ||
[HttpPost("events/{eventId}/access-codes", Name = "CreateEventAccessCode")] | ||
public async Task<IActionResult> CreateEventAccessCodeAsync( | ||
[FromHeader(Name = "Authorization")] string apiKey, | ||
[FromRoute] string eventId, | ||
[FromBody] AccessCodeRequest req) | ||
{ | ||
this._logger.LogInformation("Received a request to generate an access code"); | ||
|
||
var record = await this._auth.ValidateAsync(apiKey); | ||
if (record == null) | ||
{ | ||
this._logger.LogError("Invalid API key"); | ||
|
||
return new UnauthorizedResult(); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(eventId)) | ||
{ | ||
this._logger.LogError("No event ID"); | ||
|
||
return new NotFoundResult(); | ||
} | ||
|
||
if (req == null) | ||
{ | ||
this._logger.LogError("No request payload"); | ||
|
||
return new BadRequestResult(); | ||
} | ||
|
||
try | ||
{ | ||
var result = await this._management.CreateAccessCodeAsync(eventId, req); | ||
|
||
this._logger.LogInformation("Completed the request to generate the access code"); | ||
|
||
return new OkObjectResult(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, "Failed to generate the access code"); | ||
|
||
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the access code by GitHub alias. | ||
/// </summary> | ||
/// <param name="apiKey">API key.</param> | ||
/// <param name="eventId">Event ID.</param> | ||
/// <param name="gitHubAlias">GitHub alias.</param> | ||
/// <returns>Returns the <see cref="AccessCodeResponse"/> instance.</returns> | ||
[HttpGet("events/{eventId}/access-codes/{gitHubAlias}", Name = "GetEventAccessCodeByGitHubAlias")] | ||
public async Task<IActionResult> GetEventAccessCodeByGitHubAliasAsync( | ||
[FromHeader(Name = "Authorization")] string apiKey, | ||
[FromRoute] string eventId, | ||
[FromRoute] string gitHubAlias) | ||
{ | ||
this._logger.LogInformation("Received a request to get the access code belongs to the given GitHub alias"); | ||
|
||
var record = await this._auth.ValidateAsync(apiKey); | ||
if (record == null) | ||
{ | ||
this._logger.LogError("Invalid API key"); | ||
|
||
return new UnauthorizedResult(); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(eventId)) | ||
{ | ||
this._logger.LogError("No event ID"); | ||
|
||
return new NotFoundResult(); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(gitHubAlias)) | ||
{ | ||
this._logger.LogError("No GitHub alias"); | ||
|
||
return new NotFoundResult(); | ||
} | ||
|
||
try | ||
{ | ||
var result = await this._management.GetAccessCodeByGitHubAliasAsync(eventId, gitHubAlias); | ||
|
||
this._logger.LogInformation("Completed the request to get the access code belongs to the given GitHub alias"); | ||
|
||
return new OkObjectResult(result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, "Failed to get the access code belongs to the given GitHub alias"); | ||
|
||
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError }; | ||
} | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/AzureOpenAIProxy.ApiApp/Models/AccessCodeResponseCollection.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,8 @@ | ||
namespace AzureOpenAIProxy.ApiApp.Models; | ||
|
||
/// <summary> | ||
/// This represents the response entity collection for access code. | ||
/// </summary> | ||
public class AccessCodeResponseCollection : EntityResponseCollection<AccessCodeResponse> | ||
{ | ||
} |
39 changes: 39 additions & 0 deletions
39
src/AzureOpenAIProxy.ApiApp/Models/EntityResponseCollection.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,39 @@ | ||
namespace AzureOpenAIProxy.ApiApp.Models; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the response entity classes. | ||
/// </summary> | ||
public interface IEntityResponse | ||
{ | ||
/// <summary> | ||
/// Gets or sets the entity ID. | ||
/// </summary> | ||
string? Id { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// This represents the response entity collection. This MUST be inherited. | ||
/// </summary> | ||
/// <typeparam name="T">Type of response</typeparam> | ||
public abstract class EntityResponseCollection<T> where T : IEntityResponse | ||
{ | ||
/// <summary> | ||
/// Gets or sets the current page number. | ||
/// </summary> | ||
public int? CurrentPage { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the page size. | ||
/// </summary> | ||
public int? PageSize { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the total number of items. | ||
/// </summary> | ||
public int? Total { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the list of <see cref="T"/> instances. | ||
/// </summary> | ||
public List<T> Items { get; set; } = []; | ||
} |
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
Oops, something went wrong.