Skip to content

Commit

Permalink
[OpenAPI] Add endpoint for list event details #217 (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
tae0y authored Aug 24, 2024
1 parent c710072 commit bdbd200
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 117 deletions.
25 changes: 17 additions & 8 deletions src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEndpointUrls.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
namespace AzureOpenAIProxy.ApiApp.Endpoints;

public static class AdminEndpointUrls
{
/// <summary>
/// Declares the admin event details endpoint.
/// </summary>
public const string AdminEventDetails = "/admin/events/{eventId}";
namespace AzureOpenAIProxy.ApiApp.Endpoints;

public static class AdminEndpointUrls
{
/// <summary>
/// Declares the admin event details endpoint.
/// </summary>
public const string AdminEventDetails = "/admin/events/{eventId}";

/// <summary>
/// Declares the admin event list endpoint.
/// </summary>
/// <remarks>
/// - Get method for listing all events
/// - Post method for new event creation
/// </remarks>
public const string AdminEvents = "/admin/events";
}
117 changes: 74 additions & 43 deletions src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,74 @@
using AzureOpenAIProxy.ApiApp.Models;

using Microsoft.AspNetCore.Mvc;

namespace AzureOpenAIProxy.ApiApp.Endpoints;

/// <summary>
/// This represents the endpoint entity for get event details by admin
/// </summary>
public static class AdminEventEndpoints
{
/// <summary>
/// Adds the get event details by admin endpoint
/// </summary>
/// <param name="app"><see cref="WebApplication"/> instance.</param>
/// <returns>Returns <see cref="RouteHandlerBuilder"/> instance.</returns>
public static RouteHandlerBuilder AddAdminEvents(this WebApplication app)
{
// Todo: Issue #19 https://github.com/aliencube/azure-openai-sdk-proxy/issues/19
// Need authorization by admin
var builder = app.MapGet(AdminEndpointUrls.AdminEventDetails, (
[FromRoute] string eventId) =>
{
// Todo: Issue #208 https://github.com/aliencube/azure-openai-sdk-proxy/issues/208
return Results.Ok();
// Todo: Issue #208
})
.Produces<AdminEventDetails>(statusCode: StatusCodes.Status200OK, contentType: "application/json")
.Produces(statusCode: StatusCodes.Status401Unauthorized)
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain")
.WithTags("admin")
.WithName("GetAdminEventDetails")
.WithOpenApi(operation =>
{
operation.Summary = "Gets event details from the given event ID";
operation.Description = "This endpoint gets the event details from the given event ID.";
return operation;
});

return builder;
}
}
using AzureOpenAIProxy.ApiApp.Models;

using Microsoft.AspNetCore.Mvc;

namespace AzureOpenAIProxy.ApiApp.Endpoints;

/// <summary>
/// This represents the endpoint entity for get event details by admin
/// </summary>
public static class AdminEventEndpoints
{
/// <summary>
/// Adds the get event details by admin endpoint
/// </summary>
/// <param name="app"><see cref="WebApplication"/> instance.</param>
/// <returns>Returns <see cref="RouteHandlerBuilder"/> instance.</returns>
public static RouteHandlerBuilder AddAdminEvents(this WebApplication app)
{
// Todo: Issue #19 https://github.com/aliencube/azure-openai-sdk-proxy/issues/19
// Need authorization by admin
var builder = app.MapGet(AdminEndpointUrls.AdminEventDetails, (
[FromRoute] string eventId) =>
{
// Todo: Issue #208 https://github.com/aliencube/azure-openai-sdk-proxy/issues/208
return Results.Ok();
// Todo: Issue #208
})
.Produces<AdminEventDetails>(statusCode: StatusCodes.Status200OK, contentType: "application/json")
.Produces(statusCode: StatusCodes.Status401Unauthorized)
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain")
.WithTags("admin")
.WithName("GetAdminEventDetails")
.WithOpenApi(operation =>
{
operation.Summary = "Gets event details from the given event ID";
operation.Description = "This endpoint gets the event details from the given event ID.";
return operation;
});

return builder;
}

/// <summary>
/// Adds the get event lists by admin endpoint
/// </summary>
/// <param name="app"><see cref="WebApplication"/> instance.</param>
/// <returns>Returns <see cref="RouteHandlerBuilder"/> instance.</returns>
public static RouteHandlerBuilder AddAdminEventList(this WebApplication app)
{
// Todo: Issue #19 https://github.com/aliencube/azure-openai-sdk-proxy/issues/19
// Need authorization by admin
var builder = app.MapGet(AdminEndpointUrls.AdminEvents, () =>
{
// Todo: Issue #218 https://github.com/aliencube/azure-openai-sdk-proxy/issues/218
return Results.Ok();
// Todo: Issue #218
})
.Produces<List<AdminEventDetails>>(statusCode: StatusCodes.Status200OK, contentType: "application/json")
.Produces(statusCode: StatusCodes.Status401Unauthorized)
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain")
.WithTags("admin")
.WithName("GetAdminEvents")
.WithOpenApi(operation =>
{
operation.Summary = "Gets all events";
operation.Description = "This endpoint gets all events";
return operation;
});

return builder;
}
}
43 changes: 22 additions & 21 deletions src/AzureOpenAIProxy.ApiApp/Filters/OpenApiTagFilter.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace AzureOpenAIProxy.ApiApp.Filters;

/// <summary>
/// This represents the document filter entity for global tags.
/// </summary>
public class OpenApiTagFilter : IDocumentFilter
{
/// <inheritdoc />
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags =
[
new OpenApiTag { Name = "weather", Description = "Weather forecast operations" },
new OpenApiTag { Name = "openai", Description = "Azure OpenAI operations" },
];
}
}
using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace AzureOpenAIProxy.ApiApp.Filters;

/// <summary>
/// This represents the document filter entity for global tags.
/// </summary>
public class OpenApiTagFilter : IDocumentFilter
{
/// <inheritdoc />
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags =
[
new OpenApiTag { Name = "weather", Description = "Weather forecast operations" },
new OpenApiTag { Name = "openai", Description = "Azure OpenAI operations" },
new OpenApiTag { Name = "admin", Description = "Admin for organizing events" }
];
}
}
91 changes: 46 additions & 45 deletions src/AzureOpenAIProxy.ApiApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
using AzureOpenAIProxy.ApiApp.Endpoints;
using AzureOpenAIProxy.ApiApp.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

// Add KeyVault service
builder.Services.AddKeyVaultService();

// Add Azure OpenAI service.
builder.Services.AddOpenAIService();

// Add OpenAPI service
builder.Services.AddOpenApiService();

var app = builder.Build();

app.MapDefaultEndpoints();

// https://stackoverflow.com/questions/76962735/how-do-i-set-a-prefix-in-my-asp-net-core-7-web-api-for-all-endpoints
var basePath = "/api";
app.UsePathBase(basePath);
app.UseRouting();

// Configure the HTTP request pipeline.
// Use Swagger UI
app.UseSwaggerUI(basePath);

// Enable buffering
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next.Invoke();
});

app.UseHttpsRedirection();

app.AddWeatherForecast();
app.AddChatCompletions();

// Admin Endpoints
app.AddAdminEvents();

await app.RunAsync();
using AzureOpenAIProxy.ApiApp.Endpoints;
using AzureOpenAIProxy.ApiApp.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

// Add KeyVault service
builder.Services.AddKeyVaultService();

// Add Azure OpenAI service.
builder.Services.AddOpenAIService();

// Add OpenAPI service
builder.Services.AddOpenApiService();

var app = builder.Build();

app.MapDefaultEndpoints();

// https://stackoverflow.com/questions/76962735/how-do-i-set-a-prefix-in-my-asp-net-core-7-web-api-for-all-endpoints
var basePath = "/api";
app.UsePathBase(basePath);
app.UseRouting();

// Configure the HTTP request pipeline.
// Use Swagger UI
app.UseSwaggerUI(basePath);

// Enable buffering
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next.Invoke();
});

app.UseHttpsRedirection();

app.AddWeatherForecast();
app.AddChatCompletions();

// Admin Endpoints
app.AddAdminEvents();
app.AddAdminEventList();

await app.RunAsync();
Loading

0 comments on commit bdbd200

Please sign in to comment.