Last active
August 29, 2015 14:27
-
-
Save christopherbauer/def49a272aa2272c4180 to your computer and use it in GitHub Desktop.
Api Requestor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IApiRequester | |
| { | |
| Task<TResult> PostAsyncResult<TResult>(Uri uri, object data) where TResult : class; | |
| Task<TResult> GetAsyncResult<TResult>(Uri uri) where TResult : class; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class SearchItemsDataServiceTests | |
| { | |
| [TestFixture] | |
| public class when_getting_data_to_build_the_search_view | |
| { | |
| [Test] | |
| public void then_should_get_search_data_to_build_search_view() | |
| { | |
| // Arrange | |
| var url = @"http://test"; | |
| var apiRequestor = new Mock<IApiRequester>(); | |
| var getModelTask = Task.Run(() => new Model()); | |
| apiRequestor.Setup( | |
| requester => requester.GetAsyncResult<Model>(new Uri(url))) | |
| .Returns(getModelTask); | |
| var searchItemsDataService = new SearchItemsDataService(apiRequestor.Object); | |
| // Act | |
| var searchDataModel = searchItemsDataService.GetDataForSearchView(); | |
| // Assert | |
| Assert.That(searchDataModel, Is.Not.Null); | |
| } | |
| } | |
| [TestFixture] | |
| public class when_getting_data_to_build_the_search_result | |
| { | |
| [Test] | |
| public void then_should_get_search_data_to_build_search_view() | |
| { | |
| // Arrange | |
| var url = @"http://test/"; | |
| var apiRequester = new Mock<IApiRequester>(); | |
| var param = new Param(); | |
| Func<Task<IList<Model>>> listOfModelFunc = () => | |
| Task.Run<IList<Model>>(() => new List<Model> {new Model()}); | |
| apiRequester.Setup( | |
| requester => | |
| requester.PostAsyncResult<IList<Model>>( | |
| new Uri(url), param)) | |
| .Returns(listOfModelFunc); | |
| var searchItemsDataService = new SearchItemsDataService(apiRequester.Object); | |
| // Act | |
| var searchResult = searchItemsDataService.GetSearchResult(param); | |
| // Assert | |
| Assert.That(searchResult, Is.Not.Null); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UserCredentialApiRequester : IApiRequester | |
| { | |
| private static readonly HttpClient HttpClient = | |
| new HttpClient(new HttpClientHandler {UseDefaultCredentials = true}); | |
| public async Task<TResult> PostAsyncResult<TResult>(Uri uri, object data) where TResult : class | |
| { | |
| HttpClient.DefaultRequestHeaders.Accept.Clear(); | |
| HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
| var asyncResult = await HttpClient.PostAsync(uri, data, new JsonMediaTypeFormatter()).ConfigureAwait(false); | |
| if (!asyncResult.IsSuccessStatusCode) | |
| { | |
| return null; | |
| } | |
| return await asyncResult.Content.ReadAsAsync<TResult>(); | |
| } | |
| public async Task<TResult> GetAsyncResult<TResult>(Uri uri) where TResult : class | |
| { | |
| HttpClient.DefaultRequestHeaders.Accept.Clear(); | |
| HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
| var asyncResult = await HttpClient.GetAsync(uri).ConfigureAwait(false); | |
| if (!asyncResult.IsSuccessStatusCode) | |
| { | |
| return null; | |
| } | |
| return await asyncResult.Content.ReadAsAsync<TResult>(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment