public async Task GetRequest_FailedRequest_ReturnsStatusCodeAndResponseContent() { var httpClient = Substitute.For <IHttpClient>(); httpClient.GetResponse(Arg.Is <HttpRequestOptions>(o => o.AcceptHeader == "application/json" && o.Url == "https://api.thetvdb.com/series/122/episodes?page=1" && o.RequestContent == null && o.RequestContentType == null)) .ReturnsForAnyArgs(Task.FromResult(new HttpResponseInfo { Content = Streams.ToStream("{\"Error\": \"Not Authorized\"}"), StatusCode = HttpStatusCode.Unauthorized })); var jsonSerialiser = Substitute.For <ICustomJsonSerialiser>(); var request = new GetEpisodesRequest(122, 1); var connection = new JsonConnection(httpClient, jsonSerialiser, Substitute.For <ILogManager>()); var response = await connection.GetAsync(request, Option <string> .None); response.IsLeft.Should().BeTrue(); response.IfLeft(fr => { ((object)fr.StatusCode).Should().Be(HttpStatusCode.Unauthorized); fr.ResponseContent.Should().Be("{\"Error\": \"Not Authorized\"}"); }); }
private async Task <Option <List <TvDbEpisodeData> > > RequestEpisodesAsync(int tvDbSeriesId) { var token = await _token.GetTokenAsync(); var request = new GetEpisodesRequest(tvDbSeriesId, 1); var response = await _jsonConnection.GetAsync(request, token); return(await response.Match(async r => { var episodes = r.Data.Data.ToList(); if (r.Data.Links.Last > 1) { episodes = episodes.Concat(await RequestEpisodePagesAsync(tvDbSeriesId, 2, r.Data.Links.Last, token)) .ToList(); } var episodeDetails = (await episodes.Map(e => e.Id).Map(RequestEpisodeDetailAsync)).Somes().ToList(); SaveTvDbEpisodes(tvDbSeriesId, episodeDetails); return (Option <List <TvDbEpisodeData> >)episodeDetails.ToList(); }, fr => Task.FromResult(Option <List <TvDbEpisodeData> > .None))); }
private async Task <Option <List <TvDbEpisodeSummaryData> > > RequestEpisodesPageAsync(int tvDbSeriesId, int pageIndex, Option <string> token) { var request = new GetEpisodesRequest(tvDbSeriesId, pageIndex); var response = await _jsonConnection.GetAsync(request, token); return(response.Match(r => r.Data.Data.ToList(), fr => Option <List <TvDbEpisodeSummaryData> > .None)); }
private async Task <Option <List <TvDbEpisodeData> > > RequestEpisodesPageAsync(int tvDbSeriesId, int pageIndex) { var request = new GetEpisodesRequest(tvDbSeriesId, pageIndex); var response = await this.tvDbClient.Series.GetEpisodesAsync(tvDbSeriesId, pageIndex); var data = this.ParseResponse <EpisodeRecord[], TvDbEpisodeData[]>(response); return(data.Match(r => r.Data.ToList(), fr => Option <List <TvDbEpisodeData> > .None)); }
public async Task GetRequest_SuccessfulRequest_ReturnsResponse() { var httpClient = Substitute.For <IHttpClient>(); httpClient.GetResponse(Arg.Is <HttpRequestOptions>(o => o.AcceptHeader == "application/json" && o.Url == "https://api.thetvdb.com/series/122/episodes?page=1" && o.RequestContent == null && o.RequestContentType == null)) .Returns(Task.FromResult(new HttpResponseInfo { Content = Streams.ToStream( @"{ ""data"": [ { ""absoluteNumber"": 1, ""airedEpisodeNumber"": 2, ""airedSeason"": 3, ""dvdEpisodeNumber"": 4, ""dvdSeason"": 5, ""episodeName"": ""EpisodeName1"", ""firstAired"": ""01/01/2017"", ""id"": 6, ""lastUpdated"": 7, ""overview"": ""EpisodeOverview1"" }, { ""absoluteNumber"": 8, ""airedEpisodeNumber"": 9, ""airedSeason"": 10, ""dvdEpisodeNumber"": 11, ""dvdSeason"": 12, ""episodeName"": ""EpisodeName2"", ""firstAired"": ""01/01/2015"", ""id"": 13, ""lastUpdated"": 14, ""overview"": ""EpisodeOverview2"" } ], ""errors"": { ""invalidFilters"": [ ""string"" ], ""invalidLanguage"": ""string"", ""invalidQueryParams"": [ ""string"" ] }, ""links"": { ""first"": 1, ""last"": 2, ""next"": 3, ""previous"": 4 } }"), StatusCode = HttpStatusCode.OK })); var jsonSerialiser = Substitute.For <ICustomJsonSerialiser>(); var request = new GetEpisodesRequest(122, 1); jsonSerialiser.Deserialise <GetEpisodesRequest.Response>(null) .ReturnsForAnyArgs(new GetEpisodesRequest.Response(new[] { new TvDbEpisodeSummaryData(6, "EpisodeName1", 1L, 2, 3, 7, new DateTime(2017, 1, 2, 3, 4, 5), "Overview"), new TvDbEpisodeSummaryData(13, "EpisodeName2", 8L, 9, 10, 17, new DateTime(2017, 1, 2, 3, 4, 5), "Overview") }, new GetEpisodesRequest.PageLinks(1, 2, 3, 4))); var connection = new JsonConnection(httpClient, jsonSerialiser, Substitute.For <ILogManager>()); var response = await connection.GetAsync(request, Option <string> .None); response.IsRight.Should().BeTrue(); response.IfRight(r => r.Data.Data.Should().HaveCount(2)); }