public async Task <MazebotResponse> GetMazebotRandom(int?minSize, int?maxSize) { var url = $"{_apiUrl}/mazebot/random"; try { var queries = new Dictionary <string, string>(); if (minSize != null) { queries.Add("minSize", minSize.ToString()); } if (maxSize != null) { queries.Add("maxSize", maxSize.ToString()); } var request = _requestProvider.CreateGetRequest(url, queries: queries); var response = await _client.SendAsync(request, async r => { r.EnsureSuccessStatusCode(); var content = await r.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeAnonymousType(content, new MazebotResponse())); }); return(response); } catch (Exception e) { _logger.LogError(e, $"An error has occurred while trying to call the mazebot API {url}."); return(null); } }
public void GetImage_Should_Call_IApiRequestProvider_CreateGetRequest() { _requestProvider.CreateGetRequest(Arg.Any <string>(), Arg.Any <Dictionary <string, string> >(), Arg.Any <Dictionary <string, string> >()); var imageUrl = "imageUrl"; _client.GetImage <Rgb24>(imageUrl); _requestProvider.Received(1).CreateGetRequest(imageUrl, Arg.Any <Dictionary <string, string> >(), Arg.Any <Dictionary <string, string> >()); }
private async Task <IllnessesResponse> SearchAzureIllnesses(string url) { var get = _requestProvider.CreateGetRequest(url); var response = await _apiClient.Send(get, async res => { var body = await res.Content.ReadAsStringAsync(); var obj = JsonConvert.DeserializeObject <IllnessesResponse>(body); return(obj); }); return(response); }
public Image <TPixel> GetImage <TPixel>(string url) where TPixel : struct, IPixel <TPixel> { try { var request = _requestProvider.CreateGetRequest(url); var bytes = _client.Send(request, r => { r.EnsureSuccessStatusCode(); var readTask = r.Content.ReadAsStreamAsync(); readTask.Wait(); using (var content = readTask.Result) { using (var memoryStream = new MemoryStream()) { content.CopyTo(memoryStream); return(memoryStream.ToArray()); } } }); var image = Image.Load <TPixel>(bytes); return(image); } catch (Exception e) { _logger.LogError(e, $"An error has occurred while trying to retrieve image {url}."); return(null); } }
public async void GetHexbot_Should_Call_IApiRequestProvider_CreateGetRequest() { Dictionary <string, string> queries = null; _requestProvider.CreateGetRequest(Arg.Any <string>(), Arg.Any <Dictionary <string, string> >(), Arg.Do <Dictionary <string, string> >(a => queries = a)); await _client.GetHexbot(null, null, null, null); _requestProvider.Received(1).CreateGetRequest($"{_noopsUrl}/hexbot", Arg.Any <Dictionary <string, string> >(), Arg.Any <Dictionary <string, string> >()); queries.Should().BeEmpty(); }
public async void SearchIllnesses_Should_Call_IApiClient_Send() { Func <HttpResponseMessage, Task <IllnessesResponse> > mapper = null; var request = new HttpRequestMessage(); _requestProvider.CreateGetRequest(Arg.Any <string>()).Returns(request); await _apiClient.Send(Arg.Any <HttpRequestMessage>(), Arg.Do <Func <HttpResponseMessage, Task <IllnessesResponse> > >(a => mapper = a)); await _repository.SearchIllnesses(new SearchIllnessesRequest()); await _apiClient.Received(1).Send <IllnessesResponse>(request, Arg.Any <Func <HttpResponseMessage, Task <IllnessesResponse> > >()); var expected = new IllnessesResponse { _embedded = new IllnessesObject { Illnesses = new [] { new AzureIllness { Illness = new IllnessObject { Id = 1 } } } }, Page = new PageObject { Number = 123 } }; var body = JObject.FromObject(expected); var response = new HttpResponseMessage(); response.Content = new StringContent(body.ToString()); var actual = await mapper(response); actual.Should().BeEquivalentTo(expected); request.Dispose(); }
public async Task <HexbotResponse> GetHexbot(int?count, int?width, int?height, string seed) { var url = $"{_apiUrl}/hexbot"; try { var queries = new Dictionary <string, string>(); if (count != null) { queries.Add("count", count.ToString()); } if (width != null) { queries.Add("width", width.ToString()); } if (height != null) { queries.Add("height", height.ToString()); } if (!string.IsNullOrWhiteSpace(seed)) { queries.Add("seed", seed); } var request = _requestProvider.CreateGetRequest(url, queries: queries); var response = await _client.SendAsync(request, async r => { r.EnsureSuccessStatusCode(); var content = await r.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeAnonymousType(content, new HexbotResponse())); }); return(response); } catch (Exception e) { _logger.LogError(e, $"An error has occurred while trying to call the hexbot API {url}."); return(null); } }
public void CreateGetRequest_Should_Set_HttpMethod_Correctly() { _provider.CreateGetRequest("https://api.noopschallenge.com").Method.Should().Be(HttpMethod.Get); }