/// <summary> /// Load <see cref="IEndpoint"> /// </summary> /// <param name="services"></param> public override void Load(IServiceCollection services) { var config = new ConfigurationBuilder() .AddEnvironmentVariables("hukRestClient") .Build(); var endpointConfig = config.Get <EndpointConfig>(); if (endpointConfig == null) { endpointConfig = new EndpointConfig(); } if (string.IsNullOrEmpty(endpointConfig?.EndpointType)) { endpointConfig.EndpointType = "test"; } services.AddSingleton <EndpointConfig>(); services.AddSingleton <IEndpoint>(endpoint => { IEndpointFactory factory = new EndpointFactory(); return(factory.Create(endpointConfig.EndpointType)); } ); services.AddTransient <IEndpointFactory, EndpointFactory>(); }
/// <summary> /// Execute GET method in Rest endpoint /// </summary> /// <typeparam name="IRestApiEndpoint">Rest api endpoint</typeparam> /// <param name="action">Rest api request build method</param> /// <returns>Response</returns> public static async Task <HttpResponseMessage> Get <IRestApiEndpoint>(Func <IRestApiEndpoint, string> action) { HttpClient client = new HttpClient(); var endpoint = (IRestApiEndpoint)EndpointFactory.Create <IRestApiEndpoint>(client); var requestUri = action(endpoint); var response = await client.GetAsync(requestUri); return(response); }
public void Should_Create_Production_Endpoint() { //arrange var factory = new EndpointFactory(); //act var endpoint = factory.Create("production"); //act Assert.True(endpoint.GetType() == typeof(ProductionEndpoint) , $"Assert failed, expecting {typeof(ProductionEndpoint)} instead of {endpoint.GetType()}"); Assert.True(endpoint.CurrentVersion == Constants.CurrentVersion, $"Assert failed, expected {endpoint.CurrentVersion}, received {Constants.CurrentVersion}"); Assert.True(endpoint.BaseUrl == Constants.BaseProductionURL, $"Assert failed, expected {endpoint.BaseUrl}, received {Constants.BaseProductionURL}"); }
public void Should_Create_Test_Endpoint_When_Type_Is_Blank() { //arrange var factory = new EndpointFactory(); //act var endpoint = factory.Create(string.Empty); //act Assert.True(endpoint.GetType() == typeof(TestEndpoint) , $"Assert failed, expecting {typeof(TestEndpoint)} instead of {endpoint.GetType()}"); Assert.True(endpoint.CurrentVersion == Constants.CurrentVersion, $"Assert failed, expected {endpoint.CurrentVersion}, received {Constants.CurrentVersion}"); Assert.True(endpoint.BaseUrl == Constants.BaseTestURL, $"Assert failed, expected {endpoint.BaseUrl}, received {Constants.BaseTestURL}"); }
public async Task <WeatherResultByDay> GetData(Filter filter) { // Creates the endpoint var endpoint = EndpointFactory.Create(filter, _configuration); // Get json object concerning 5 days forecast from API var jsonForecast = await _client.GetStringAsync(endpoint.Forecast); // Get json object concerning today's weather from API var jsonCurrent = await _client.GetStringAsync(endpoint.Weather); // Parse data from forecat var forecastResult = JsonHelper.Deserialize <WeatherResult>(jsonForecast); // Average parse from forecast list var groupResult = forecastResult .List .GroupBy(item => DateTimeOffset.FromUnixTimeSeconds(item.Dt).Date) .Select(group => new WeatherResultByDayList { Date = group.Key, Forecast = WeatherResultByDayForecastFactory.Create(group) }); var result = new WeatherResultByDay { City = forecastResult.City, ListByDay = groupResult }; // Parse data from today's weather result.Today = WeatherResultByDayForecastFactory.Create( JsonHelper.Deserialize <WeatherResultList>(jsonCurrent) ); return(result); }