Skip to content

Commit 9cde777

Browse files
authored
Add endpoints for access code (#14)
1 parent e89c796 commit 9cde777

10 files changed

+366
-135
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using AzureOpenAIProxy.ApiApp.Models;
2+
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace AzureOpenAIProxy.ApiApp.Controllers;
6+
7+
/// <summary>
8+
/// This represents the controller entity for management.
9+
/// </summary>
10+
public partial class ManagementController
11+
{
12+
/// <summary>
13+
/// Gets the list of access codes by event ID.
14+
/// </summary>
15+
/// <param name="apiKey">API key.</param>
16+
/// <param name="eventId">Event ID.</param>
17+
/// <param name="page">Page number.</param>
18+
/// <param name="size">Page size.</param>
19+
/// <returns>Returns the <see cref="AccessCodeResponseCollection"/> instance.</returns>
20+
[HttpGet("events/{eventId}/access-codes", Name = "GetListOfEventAccessCodes")]
21+
public async Task<IActionResult> GetEventAccessCodesByEventIdAsync(
22+
[FromHeader(Name = "Authorization")] string apiKey,
23+
[FromRoute] string eventId,
24+
[FromQuery(Name = "page")] int? page = 0,
25+
[FromQuery(Name = "size")] int? size = 20)
26+
{
27+
this._logger.LogInformation("Received a request to get the list of access codes for the given event");
28+
29+
var record = await this._auth.ValidateAsync(apiKey);
30+
if (record == null)
31+
{
32+
this._logger.LogError("Invalid API key");
33+
34+
return new UnauthorizedResult();
35+
}
36+
37+
if (string.IsNullOrWhiteSpace(eventId))
38+
{
39+
this._logger.LogError("No event ID");
40+
41+
return new NotFoundResult();
42+
}
43+
44+
try
45+
{
46+
var result = await this._management.GetAccessCodesAsync(eventId, page, size);
47+
48+
this._logger.LogInformation("Completed the request to get the list of access codes for the given event");
49+
50+
return new OkObjectResult(result);
51+
}
52+
catch (Exception ex)
53+
{
54+
this._logger.LogError(ex, "Failed to get the list of access codes for the given event");
55+
56+
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError };
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Creates the access code.
62+
/// </summary>
63+
/// <param name="apiKey">API key.</param>
64+
/// <param name="eventId">Event ID.</param>
65+
/// <param name="req"><see cref="AccessCodeRequest"/> instance.</param>
66+
/// <returns>Returns the <see cref="AccessCodeResponse"/> instance.</returns>
67+
[HttpPost("events/{eventId}/access-codes", Name = "CreateEventAccessCode")]
68+
public async Task<IActionResult> CreateEventAccessCodeAsync(
69+
[FromHeader(Name = "Authorization")] string apiKey,
70+
[FromRoute] string eventId,
71+
[FromBody] AccessCodeRequest req)
72+
{
73+
this._logger.LogInformation("Received a request to generate an access code");
74+
75+
var record = await this._auth.ValidateAsync(apiKey);
76+
if (record == null)
77+
{
78+
this._logger.LogError("Invalid API key");
79+
80+
return new UnauthorizedResult();
81+
}
82+
83+
if (string.IsNullOrWhiteSpace(eventId))
84+
{
85+
this._logger.LogError("No event ID");
86+
87+
return new NotFoundResult();
88+
}
89+
90+
if (req == null)
91+
{
92+
this._logger.LogError("No request payload");
93+
94+
return new BadRequestResult();
95+
}
96+
97+
try
98+
{
99+
var result = await this._management.CreateAccessCodeAsync(eventId, req);
100+
101+
this._logger.LogInformation("Completed the request to generate the access code");
102+
103+
return new OkObjectResult(result);
104+
}
105+
catch (Exception ex)
106+
{
107+
this._logger.LogError(ex, "Failed to generate the access code");
108+
109+
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError };
110+
}
111+
}
112+
113+
/// <summary>
114+
/// Gets the access code by GitHub alias.
115+
/// </summary>
116+
/// <param name="apiKey">API key.</param>
117+
/// <param name="eventId">Event ID.</param>
118+
/// <param name="gitHubAlias">GitHub alias.</param>
119+
/// <returns>Returns the <see cref="AccessCodeResponse"/> instance.</returns>
120+
[HttpGet("events/{eventId}/access-codes/{gitHubAlias}", Name = "GetEventAccessCodeByGitHubAlias")]
121+
public async Task<IActionResult> GetEventAccessCodeByGitHubAliasAsync(
122+
[FromHeader(Name = "Authorization")] string apiKey,
123+
[FromRoute] string eventId,
124+
[FromRoute] string gitHubAlias)
125+
{
126+
this._logger.LogInformation("Received a request to get the access code belongs to the given GitHub alias");
127+
128+
var record = await this._auth.ValidateAsync(apiKey);
129+
if (record == null)
130+
{
131+
this._logger.LogError("Invalid API key");
132+
133+
return new UnauthorizedResult();
134+
}
135+
136+
if (string.IsNullOrWhiteSpace(eventId))
137+
{
138+
this._logger.LogError("No event ID");
139+
140+
return new NotFoundResult();
141+
}
142+
143+
if (string.IsNullOrWhiteSpace(gitHubAlias))
144+
{
145+
this._logger.LogError("No GitHub alias");
146+
147+
return new NotFoundResult();
148+
}
149+
150+
try
151+
{
152+
var result = await this._management.GetAccessCodeByGitHubAliasAsync(eventId, gitHubAlias);
153+
154+
this._logger.LogInformation("Completed the request to get the access code belongs to the given GitHub alias");
155+
156+
return new OkObjectResult(result);
157+
}
158+
catch (Exception ex)
159+
{
160+
this._logger.LogError(ex, "Failed to get the access code belongs to the given GitHub alias");
161+
162+
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError };
163+
}
164+
}
165+
}

src/AzureOpenAIProxy.ApiApp/Controllers/ManagementController.cs renamed to src/AzureOpenAIProxy.ApiApp/Controllers/ManagementEventController.cs

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace AzureOpenAIProxy.ApiApp.Controllers;
1313
/// <param name="logger"><see cref="ILogger{TCategoryName}"/> instance.</param>
1414
[ApiController]
1515
[Route("api/management")]
16-
public class ManagementController(
16+
public partial class ManagementController(
1717
[FromKeyedServices("management")] IAuthService<EventRecord> auth,
1818
IManagementService management,
1919
ILogger<ManagementController> logger) : ControllerBase
@@ -22,12 +22,18 @@ public class ManagementController(
2222
private readonly IManagementService _management = management ?? throw new ArgumentNullException(nameof(management));
2323
private readonly ILogger<ManagementController> _logger = logger ?? throw new ArgumentNullException(nameof(logger));
2424

25+
/// <summary>
26+
/// Gets the list of events.
27+
/// </summary>
28+
/// <param name="apiKey">API key.</param>
29+
/// <param name="page">Page number.</param>
30+
/// <param name="size">Page size.</param>
31+
/// <returns>Returns the <see cref="EventResponseCollection"/> instance.</returns>
2532
[HttpGet("events", Name = "GetListOfEvents")]
2633
public async Task<IActionResult> GetEventsAsync(
2734
[FromHeader(Name = "Authorization")] string apiKey,
2835
[FromQuery(Name = "page")] int? page = 0,
29-
[FromQuery(Name = "size")] int? size = 20
30-
)
36+
[FromQuery(Name = "size")] int? size = 20)
3137
{
3238
this._logger.LogInformation("Received a request to get all events");
3339

@@ -55,8 +61,14 @@ public async Task<IActionResult> GetEventsAsync(
5561
}
5662
}
5763

64+
/// <summary>
65+
/// Creates the event.
66+
/// </summary>
67+
/// <param name="apiKey">API key.</param>
68+
/// <param name="req"><see cref="EventRequest"/> instance.</param>
69+
/// <returns>Returns the <see cref="EventResponse"/> instance.</returns>
5870
[HttpPost("events", Name = "CreateEvent")]
59-
public async Task<IActionResult> CreateEvent(
71+
public async Task<IActionResult> CreateEventAsync(
6072
[FromHeader(Name = "Authorization")] string apiKey,
6173
[FromBody] EventRequest req)
6274
{
@@ -93,6 +105,12 @@ public async Task<IActionResult> CreateEvent(
93105
}
94106
}
95107

108+
/// <summary>
109+
/// Gets the event by ID.
110+
/// </summary>
111+
/// <param name="apiKey">API key.</param>
112+
/// <param name="eventId">Event ID.</param>
113+
/// <returns>Returns the <see cref="EventResponse"/> instance.</returns>
96114
[HttpGet("events/{eventId}", Name = "GetEventById")]
97115
public async Task<IActionResult> GetEventByEventIdAsync(
98116
[FromHeader(Name = "Authorization")] string apiKey,
@@ -130,72 +148,4 @@ public async Task<IActionResult> GetEventByEventIdAsync(
130148
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError };
131149
}
132150
}
133-
134-
[HttpGet("events/{eventId}/access-codes", Name = "GetListOfEventAccessCodes")]
135-
public async Task<IActionResult> GetAccessCodesByEventIdAsync(
136-
[FromHeader(Name = "Authorization")] string apiKey,
137-
[FromRoute] string eventId)
138-
{
139-
var result = new OkObjectResult("Pong");
140-
141-
return await Task.FromResult(result);
142-
}
143-
144-
[HttpPost("events/{eventId}/access-codes", Name = "CreateEventAccessCode")]
145-
public async Task<IActionResult> CreateEvent(
146-
[FromHeader(Name = "Authorization")] string apiKey,
147-
[FromRoute] string eventId,
148-
[FromBody] AccessCodeRequest req)
149-
{
150-
this._logger.LogInformation("Received a request to generate an access code");
151-
152-
var record = await this._auth.ValidateAsync(apiKey);
153-
if (record == null)
154-
{
155-
this._logger.LogError("Invalid API key");
156-
157-
return new UnauthorizedResult();
158-
}
159-
160-
if (string.IsNullOrWhiteSpace(eventId))
161-
{
162-
this._logger.LogError("No event ID");
163-
164-
return new NotFoundResult();
165-
}
166-
167-
if (req == null)
168-
{
169-
this._logger.LogError("No request payload");
170-
171-
return new BadRequestResult();
172-
}
173-
174-
try
175-
{
176-
req.EventId = eventId;
177-
var result = await this._management.CreateAccessCodeAsync(req);
178-
179-
this._logger.LogInformation("Completed the request to generate the access code");
180-
181-
return new OkObjectResult(result);
182-
}
183-
catch (Exception ex)
184-
{
185-
this._logger.LogError(ex, "Failed to generate the access code");
186-
187-
return new ObjectResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError };
188-
}
189-
}
190-
191-
[HttpGet("events/{eventId}/access-codes/{gitHubAlias}", Name = "GetEventAccessCodeByGitHubAlias")]
192-
public async Task<IActionResult> GetAccessCodesByEventIdAsync(
193-
[FromHeader(Name = "Authorization")] string apiKey,
194-
[FromRoute] string eventId,
195-
[FromRoute] string gitHubAlias)
196-
{
197-
var result = new OkObjectResult("Pong");
198-
199-
return await Task.FromResult(result);
200-
}
201151
}

src/AzureOpenAIProxy.ApiApp/Models/AccessCodeResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace AzureOpenAIProxy.ApiApp.Models;
55
/// <summary>
66
/// This represents the response entity for access code.
77
/// </summary>
8-
public class AccessCodeResponse : AccessCodeRequest
8+
public class AccessCodeResponse : AccessCodeRequest, IEntityResponse
99
{
1010
/// <summary>
1111
/// Initializes a new instance of the <see cref="AccessCodeResponse"/> class.
@@ -48,4 +48,4 @@ public AccessCodeResponse(AccessCodeRecord record)
4848
/// </summary>
4949
[JsonPropertyName("maxTokens")]
5050
public int? MaxTokens { get; set; }
51-
}
51+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AzureOpenAIProxy.ApiApp.Models;
2+
3+
/// <summary>
4+
/// This represents the response entity collection for access code.
5+
/// </summary>
6+
public class AccessCodeResponseCollection : EntityResponseCollection<AccessCodeResponse>
7+
{
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace AzureOpenAIProxy.ApiApp.Models;
2+
3+
/// <summary>
4+
/// This provides interfaces to the response entity classes.
5+
/// </summary>
6+
public interface IEntityResponse
7+
{
8+
/// <summary>
9+
/// Gets or sets the entity ID.
10+
/// </summary>
11+
string? Id { get; set; }
12+
}
13+
14+
/// <summary>
15+
/// This represents the response entity collection. This MUST be inherited.
16+
/// </summary>
17+
/// <typeparam name="T">Type of response</typeparam>
18+
public abstract class EntityResponseCollection<T> where T : IEntityResponse
19+
{
20+
/// <summary>
21+
/// Gets or sets the current page number.
22+
/// </summary>
23+
public int? CurrentPage { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the page size.
27+
/// </summary>
28+
public int? PageSize { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the total number of items.
32+
/// </summary>
33+
public int? Total { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the list of <see cref="T"/> instances.
37+
/// </summary>
38+
public List<T> Items { get; set; } = [];
39+
}

src/AzureOpenAIProxy.ApiApp/Models/EventRecord.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public class EventRecord : ITableEntity
5454
/// </summary>
5555
public string? ApiKey { get; set; }
5656

57+
/// <summary>
58+
/// Gets or sets the maximum number of tokens for the event. Defaults to 4096.
59+
/// </summary>
60+
public int? MaxTokens { get; set; } = 4096;
61+
5762
/// <inheritdoc />
5863
public DateTimeOffset? Timestamp { get; set; }
5964

src/AzureOpenAIProxy.ApiApp/Models/EventResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace AzureOpenAIProxy.ApiApp.Models;
55
/// <summary>
66
/// This represents the response entity for event.
77
/// </summary>
8-
public class EventResponse : EventRequest
8+
public class EventResponse : EventRequest, IEntityResponse
99
{
1010
/// <summary>
1111
/// Initializes a new instance of the <see cref="EventResponse"/> class.

0 commit comments

Comments
 (0)