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", (string currency) => { lock(gCurrencies) gCurrencies.Add(currency); }).WithOpenApi(); app.MapDelete("/rates/{currency}", (string currency) => { lock (gCurrencies) 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) { List copy; lock (gCurrencies) copy = [..currencies]; decimal sum = 0; foreach (var currency in copy) { var json = await gHttpClient.GetStringAsync($"https://api.coinbase.com/v2/exchange-rates?currency={currency}"); sum += ParsePrice(json); } var avg = sum / copy.Count; return avg; } public record CurrencyExchangeRatesResult(CurrencyExchangeRates Data); public record CurrencyExchangeRates(string Currency, IReadOnlyDictionary Rates);