using System.Text.Json; var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); app.UseSwaggerUI(options => options.SwaggerEndpoint("/openapi/v1.json", "Open API V1")); } List gCurrencies = ["USD", "EUR", "GBP"]; var gHttpClient = new HttpClient(); var gJsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); var gSemaphoreSlim = new SemaphoreSlim(1, 1); // Add route and a function that serves the result. app.MapGet("/rates/average", () => GetAverageRateAsync(gCurrencies)).WithOpenApi(); app.MapPost("/rates", async (string currency) => { gSemaphoreSlim.Wait(); gCurrencies.Add(currency); gSemaphoreSlim.Release(); }).WithOpenApi(); app.MapDelete("/rates/{currency}", async (string currency) => { lock (gSemaphoreSlim) gCurrencies.Remove(currency); }).WithOpenApi(); app.Run(); decimal ParsePrice(string json) { var result = JsonSerializer.Deserialize(json, gJsonSerializerOptions); return result!.Data.Rates["CZK"]; } async Task GetAverageRateAsync(List currencies) { decimal sum = 0; gSemaphoreSlim.Wait(); { foreach (var currency in currencies) { var json = await gHttpClient.GetStringAsync($"https://api.coinbase.com/v2/exchange-rates?currency={currency}"); sum += ParsePrice(json); } } gSemaphoreSlim.Release(); var avg = sum / currencies.Count; return avg; } public record CurrencyExchangeRatesResult(CurrencyExchangeRates Data); public record CurrencyExchangeRates(string Currency, IReadOnlyDictionary Rates);