From 7a3c7bd6e646287c9fbed09dca44c6eb423ee3ea Mon Sep 17 00:00:00 2001 From: jannesvh Date: Tue, 26 Jan 2021 10:11:59 +0200 Subject: [PATCH 1/4] test github actions --- GradDemo.Api/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GradDemo.Api/Startup.cs b/GradDemo.Api/Startup.cs index b0fa1d2..2d28444 100644 --- a/GradDemo.Api/Startup.cs +++ b/GradDemo.Api/Startup.cs @@ -75,7 +75,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Applicat { db.Database.Migrate(); } - + } } } From b76ededede366a5fd855bb62c6f91e744420937b Mon Sep 17 00:00:00 2001 From: jannesvh Date: Tue, 26 Jan 2021 10:15:59 +0200 Subject: [PATCH 2/4] actions test fail test --- GradDemo.Tests/CallHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GradDemo.Tests/CallHelper.cs b/GradDemo.Tests/CallHelper.cs index 32198f4..74df71e 100644 --- a/GradDemo.Tests/CallHelper.cs +++ b/GradDemo.Tests/CallHelper.cs @@ -20,7 +20,7 @@ public static class CallHelper var content = new StringContent(JsonConvert.SerializeObject(input), Encoding.UTF8, "application/json"); var result = await client.PostAsync(path, content); var resultContent = await ParseContent(result.Content); - return (resultContent, result); + throw new Exception("test fail"); } private static async Task ParseContent(HttpContent content) where T : class From ecfbd023db4752f5f57233a321234b9c96f8e90a Mon Sep 17 00:00:00 2001 From: David Noakes Date: Tue, 26 Jan 2021 11:06:37 +0200 Subject: [PATCH 3/4] Config and dependancies --- GradDemo.Api/Controllers/CryptoController.cs | 32 ++++++++++++++++++-- GradDemo.Api/Controllers/Currency.cs | 12 ++++++++ GradDemo.Api/Controllers/CurrencyBase.cs | 7 +++++ GradDemo.Api/Providers/CoinGeckoProvider.cs | 27 +++++++++++++++-- GradDemo.Api/Startup.cs | 9 +++++- GradDemo.Api/appsettings.json | 3 ++ GradDemo.Tests/UnitTest1.cs | 16 ++++++++++ 7 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 GradDemo.Api/Controllers/Currency.cs create mode 100644 GradDemo.Api/Controllers/CurrencyBase.cs diff --git a/GradDemo.Api/Controllers/CryptoController.cs b/GradDemo.Api/Controllers/CryptoController.cs index 1741453..7ecb227 100644 --- a/GradDemo.Api/Controllers/CryptoController.cs +++ b/GradDemo.Api/Controllers/CryptoController.cs @@ -20,14 +20,19 @@ namespace GradDemo.Api.Controllers [ApiController] public class CryptoController : ControllerBase { + private readonly CoinGeckoProvider _coinGeckoProvider; + + public CryptoController(CoinGeckoProvider coinProv) + { + _coinGeckoProvider = coinProv; + } [HttpGet("value/for/{coinId}/currency/{currency}")] public async Task> GetCoin(string coinId, string currency) { var result = new CryptoCoinResponse(); - CoinGeckoProvider provider = new CoinGeckoProvider(); - var res = await provider.GetValueForCoin(coinId, currency); + var res = await _coinGeckoProvider.GetValueForCoin(coinId, currency); if (res.HasValue) { @@ -39,5 +44,28 @@ public async Task> GetCoin(string coinId, string cu return Response.Error("Something went wrong"); } + + [HttpGet("GetAllCurrencies")] + public async Task> GetAllCurrencies() + { + string[]? res; + + res = await _coinGeckoProvider.GetAllCurrency(); + + if (res != null) + { + return Response.Successful(res); + + } + + return Response.Error("Something went wrong"); + } + + [HttpPost("{CurrencySymbol}")] + public void SetCurrencyPreference(string CurrencySymbol) + { + // what should I do??? + } + } } diff --git a/GradDemo.Api/Controllers/Currency.cs b/GradDemo.Api/Controllers/Currency.cs new file mode 100644 index 0000000..0c4bd22 --- /dev/null +++ b/GradDemo.Api/Controllers/Currency.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GradDemo.Api.Controllers +{ + public class Currency + { + public string? Symbol { get; set; } + } +} diff --git a/GradDemo.Api/Controllers/CurrencyBase.cs b/GradDemo.Api/Controllers/CurrencyBase.cs new file mode 100644 index 0000000..9d34e81 --- /dev/null +++ b/GradDemo.Api/Controllers/CurrencyBase.cs @@ -0,0 +1,7 @@ +namespace GradDemo.Api.Controllers +{ + public abstract class CurrencyBase + { + public abstract string Symbol { get; set; } + } +} \ No newline at end of file diff --git a/GradDemo.Api/Providers/CoinGeckoProvider.cs b/GradDemo.Api/Providers/CoinGeckoProvider.cs index 7767301..260a1d7 100644 --- a/GradDemo.Api/Providers/CoinGeckoProvider.cs +++ b/GradDemo.Api/Providers/CoinGeckoProvider.cs @@ -1,4 +1,5 @@ -using GradDemo.Api.Models.CoinGecko; +using GradDemo.Api.Controllers; +using GradDemo.Api.Models.CoinGecko; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -12,11 +13,16 @@ public class CoinGeckoProvider { static HttpClient client = new HttpClient(); + public CoinGeckoProvider(string baseUrl) + { + client.BaseAddress = new Uri(baseUrl); + } + public async Task GetValueForCoin(string coinId, string currency) { double? resultValue = null; - string url = $"https://api.coingecko.com/api/v3/simple/price?ids={coinId}&vs_currencies={currency}"; + string url = $"api/v3/simple/price?ids={coinId}&vs_currencies={currency}"; HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { @@ -38,5 +44,22 @@ public class CoinGeckoProvider return null; } + + public async Task GetAllCurrency() + { + string[]? resultValue = null; + + string url = $"/api/v3/simple/supported_vs_currencies"; + HttpResponseMessage response = await client.GetAsync(url); + if (response.IsSuccessStatusCode) + { + var res = await response.Content.ReadAsStringAsync(); + var coinGeckoResult = JsonConvert.DeserializeObject(res); + resultValue = coinGeckoResult; + return resultValue; + } + + return null; + } } } diff --git a/GradDemo.Api/Startup.cs b/GradDemo.Api/Startup.cs index b0fa1d2..e88fbe3 100644 --- a/GradDemo.Api/Startup.cs +++ b/GradDemo.Api/Startup.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using GradDemo.Api.Providers; namespace GradDemo.Api { @@ -48,6 +49,12 @@ public void ConfigureServices(IServiceCollection services) { c.SwaggerDoc("v1", new OpenApiInfo { Title = "graddemo", Version = "v1" }); }); + + services.AddSingleton(x => new CoinGeckoProvider( + Configuration.GetValue("CoinGecko:Url") + ) + ); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -73,7 +80,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Applicat if (!IntergrationTesting) { - db.Database.Migrate(); + //db.Database.Migrate(); } } diff --git a/GradDemo.Api/appsettings.json b/GradDemo.Api/appsettings.json index 3ef279e..3cb0ada 100644 --- a/GradDemo.Api/appsettings.json +++ b/GradDemo.Api/appsettings.json @@ -9,5 +9,8 @@ "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=.;Database=GradDemo;Trusted_Connection=True;" + }, + "CoinGecko": { + "Url": "https://api.coingecko.com" } } diff --git a/GradDemo.Tests/UnitTest1.cs b/GradDemo.Tests/UnitTest1.cs index a728f53..5b00013 100644 --- a/GradDemo.Tests/UnitTest1.cs +++ b/GradDemo.Tests/UnitTest1.cs @@ -135,5 +135,21 @@ public async Task TestGetCurrency() Assert.IsTrue(usdResult.content.Payload.Value < zarresult.content.Payload.Value); } + + [Test] + public async Task TestSetCurrencyPref() + { + + string currencyPref = "usd"; + var usdResult = await CallHelper.GetAndDeserialize>(_httpClient, $"/api/v3/simple/price?ids=bitcoin&vs_currencies={currencyPref}"); + + Assert.IsTrue(usdResult.httpResponse.IsSuccessStatusCode); + + + + Assert.IsTrue(usdResult.httpResponse.IsSuccessStatusCode); + + Assert.IsTrue(usdResult.content.Payload == currencyPref); + } } } \ No newline at end of file From 16e58681f7df491f9645a038c70cfe51840ef410 Mon Sep 17 00:00:00 2001 From: kmgwenya <77482705+kmgwenya@users.noreply.github.com> Date: Tue, 26 Jan 2021 13:32:05 +0200 Subject: [PATCH 4/4] Update Startup.cs --- GradDemo.Api/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GradDemo.Api/Startup.cs b/GradDemo.Api/Startup.cs index 81bd9f5..aec0d3f 100644 --- a/GradDemo.Api/Startup.cs +++ b/GradDemo.Api/Startup.cs @@ -80,7 +80,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Applicat if (!IntergrationTesting) { - //db.Database.Migrate(); + db.Database.Migrate(); } }