Ejemplo n.º 1
0
 public async Task<Tuple<IEnumerable<ShortDescription>, ErrorData>> SearchShowsAsync(string searchQuery)
 {
     if (string.IsNullOrWhiteSpace(searchQuery))
         throw new ArgumentException("Argument is null or whitespace", nameof(searchQuery));
     try
     {
         var baseUri = new Uri(MyShowsApiBaseAddress);
         var address = new Uri(baseUri, string.Format(SearchPart, searchQuery));
         var response = await _httpClient.GetAsync(address);
         if (!response.IsSuccessStatusCode)
         {
             ErrorData errorData = null;
             switch (response.StatusCode)
             {
                 case HttpStatusCode.NotFound:
                     errorData = new ErrorData(response.StatusCode, true);
                     break;
                 case HttpStatusCode.InternalServerError:
                     errorData = new ErrorData(response.StatusCode, true);
                     break;
                 default:
                     errorData = new ErrorData(response.StatusCode, false);
                     break;
             }
             return new Tuple<IEnumerable<ShortDescription>, ErrorData>(null, errorData);
         }
         var jsonString = await response.Content.ReadAsStringAsync();
         var parsedResponse = await _parser.DeserializeShortDescriptionAsync(jsonString).ConfigureAwait(false);
         //вместо 404 сервер возвращает пустой массив - он будет считаться за ответ 404!
         if (parsedResponse != null)
         {
             if (!parsedResponse.Any())
             {
                 return new Tuple<IEnumerable<ShortDescription>, ErrorData>(parsedResponse,
                     new ErrorData(HttpStatusCode.NotFound, true));
             }
         }
         return new Tuple<IEnumerable<ShortDescription>, ErrorData>(parsedResponse, null);
     }
     catch (ArgumentException)
     {
         return new Tuple<IEnumerable<ShortDescription>, ErrorData>(null,
             new ErrorData(HttpStatusCode.None, false));
     }
 }
Ejemplo n.º 2
0
 public async Task<Tuple<ExtendedDescription, ErrorData>> GetShowDescriptionAsync(int showId)
 {
     try
     {
         var baseUri = new Uri(MyShowsApiBaseAddress);
         var address = new Uri(baseUri, string.Format(InfoEpisodesPart, showId));
         var response = await _httpClient.GetAsync(address);
         if (!response.IsSuccessStatusCode)
         {
             ErrorData errorData = null;
             switch (response.StatusCode)
             {
                 case HttpStatusCode.NotFound:
                     errorData = new ErrorData(response.StatusCode, true);
                     break;
                 case HttpStatusCode.InternalServerError:
                     errorData = new ErrorData(response.StatusCode, true);
                     break;
                 default:
                     errorData = new ErrorData(response.StatusCode, false);
                     break;
             }
             return new Tuple<ExtendedDescription, ErrorData>(null, errorData);
         }
         var jsonString = await response.Content.ReadAsStringAsync();
         var parsedResponse = await _parser.DeserializeExtendedDescriptionAsync(jsonString).ConfigureAwait(false);
         return parsedResponse == null
             ? new Tuple<ExtendedDescription, ErrorData>(null, new ErrorData(HttpStatusCode.None, false))
             : new Tuple<ExtendedDescription, ErrorData>(parsedResponse, null);
     }
     catch (ArgumentException)
     {
         return new Tuple<ExtendedDescription, ErrorData>(null,
             new ErrorData(HttpStatusCode.None, false));
     }
 }