Skip to content

Commit

Permalink
✨ Basic api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
LNA-DEV committed Sep 27, 2024
1 parent 78201ad commit ce5ebfb
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 26 deletions.
47 changes: 47 additions & 0 deletions OpenMediaServer/Endpoints/ApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using OpenMediaServer.Interfaces.Endpoints;
using OpenMediaServer.Interfaces.Services;

namespace OpenMediaServer.Endpoints;

public class ApiEndpoints : IApiEndpoints
{
private readonly ILogger<ApiEndpoints> _logger;
private readonly IInventoryService _inventoryService;

public ApiEndpoints(ILogger<ApiEndpoints> logger, IInventoryService inventoryService)
{
_logger = logger;
_inventoryService = inventoryService;
}

public void Map(WebApplication app)
{
var group = app.MapGroup("/api");

group.MapGet("/categories", ListCategories);
group.MapGet("/items", ListItems);
group.MapGet("/item", GetItem);
}

public async Task<IResult> ListCategories()
{
var categories = await _inventoryService.ListCategories();

return Results.Ok(categories);
}

public async Task<IResult> ListItems(string category)
{
var items = await _inventoryService.ListItems(category);

return Results.Ok(items);
}

public async Task<IResult> GetItem(string category, string id)
{
var item = await _inventoryService.GetItem(id: id, category: category);

return Results.Ok(item);
}
}
19 changes: 0 additions & 19 deletions OpenMediaServer/Endpoints/MovieEndpoints.cs

This file was deleted.

31 changes: 31 additions & 0 deletions OpenMediaServer/Endpoints/StreamingEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using OpenMediaServer.Interfaces.Endpoints;
using OpenMediaServer.Interfaces.Services;

namespace OpenMediaServer.Endpoints;

public class StreamingEndpoints : IStreamingEndpoints
{
private readonly ILogger<StreamingEndpoints> _logger;
private readonly IStreamingService _streamingService;

public StreamingEndpoints(ILogger<StreamingEndpoints> logger, IStreamingService streamingService)
{
_logger = logger;
_streamingService = streamingService;
}

public void Map(WebApplication app)
{
var group = app.MapGroup("/stream");

group.MapGet("/{category}/{id}", StreamContent);
}

public async Task<IResult> StreamContent(string id, string category)
{
using var stream = await _streamingService.GetMediaStream(id, category);

return Results.Stream(stream, enableRangeProcessing: true, contentType: "video/webm"); // TODO content type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenMediaServer.Interfaces.Endpoints;

public interface IMovieEndpoints
public interface IApiEndpoints
{
public void Map(WebApplication app);
}
8 changes: 8 additions & 0 deletions OpenMediaServer/Interfaces/Endpoints/IStreamingEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace OpenMediaServer.Interfaces.Endpoints;

public interface IStreamingEndpoints
{
public void Map(WebApplication app);
}
3 changes: 3 additions & 0 deletions OpenMediaServer/Interfaces/Services/IInventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public interface IInventoryService
public void AddItem<T>(T item) where T : InventoryItem;
public void AddItems(IEnumerable<InventoryItem> items);
public void CreateFromPaths(IEnumerable<string> paths);
public Task<IEnumerable<string>> ListCategories();
public Task<IEnumerable<InventoryItem>> ListItems(string category);
public Task<InventoryItem> GetItem(string id, string category);
}
8 changes: 8 additions & 0 deletions OpenMediaServer/Interfaces/Services/IStreamingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace OpenMediaServer.Interfaces.Services;

public interface IStreamingService
{
public Task<Stream> GetMediaStream(string id, string category);
}
11 changes: 5 additions & 6 deletions OpenMediaServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@
builder.Services.AddSingleton<IContentDiscoveryService, ContentDiscoveryService>();
builder.Services.AddSingleton<IStorageRepository, FileSystemRepository>();
builder.Services.AddSingleton<IInventoryService, InventoryService>();
builder.Services.AddSingleton<IMovieEndpoints, MovieEndpoints>();
builder.Services.AddSingleton<IStreamingService, StreamingService>();
builder.Services.AddSingleton<IMetadataAPI, OMDbAPI>();
builder.Services.AddSingleton<IApiEndpoints, ApiEndpoints>();
builder.Services.AddSingleton<IStreamingEndpoints, StreamingEndpoints>();

builder.Services.AddHttpClient<IMetadataAPI, OMDbAPI>();

var app = builder.Build();


app.UseSwagger();
app.UseSwaggerUI();


app.UseHttpsRedirection();


var contentDiscoveryService = app.Services.GetService<IContentDiscoveryService>();
contentDiscoveryService?.ActiveScan(Globals.MediaFolder);
contentDiscoveryService?.Watch(Globals.MediaFolder);

var movieEndpoints = app.Services.GetService<IMovieEndpoints>();
movieEndpoints?.Map(app);
app.Services.GetService<IApiEndpoints>()?.Map(app);
app.Services.GetService<IStreamingEndpoints>()?.Map(app);

app.Run();
15 changes: 15 additions & 0 deletions OpenMediaServer/Services/InventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public InventoryService(ILogger<InventoryService> logger, IStorageRepository sto
_configuration = configuration;
}

public async Task<IEnumerable<string>> ListCategories()
{
return new List<string> { }; // TODO Implement
}

public async Task<IEnumerable<InventoryItem>> ListItems(string category)
{
return new List<InventoryItem> { }; // TODO Implement
}

public async Task<InventoryItem> GetItem(string id, string category)
{
return new InventoryItem(); // TODO Implement
}

public async void CreateFromPaths(IEnumerable<string> paths)
{
foreach (var path in paths)
Expand Down
21 changes: 21 additions & 0 deletions OpenMediaServer/Services/StreamingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using OpenMediaServer.Interfaces.Services;

namespace OpenMediaServer.Services;

public class StreamingService : IStreamingService
{
private readonly ILogger<StreamingService> _logger;

public StreamingService(ILogger<StreamingService> logger)
{
_logger = logger;
}

public async Task<Stream> GetMediaStream(string id, string category)
{
// TODO Implement

throw new NotImplementedException();
}
}

0 comments on commit ce5ebfb

Please sign in to comment.