update schedules direct to automatically reacquire token

pull/702/head
Luke 9 years ago
parent efdd1b1f2e
commit c5c23f932b

@ -114,7 +114,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
var requestString = _jsonSerializer.SerializeToString(requestList); var requestString = _jsonSerializer.SerializeToString(requestList);
_logger.Debug("Request string for schedules is: " + requestString); _logger.Debug("Request string for schedules is: " + requestString);
httpOptions.RequestContent = requestString; httpOptions.RequestContent = requestString;
using (var response = await Post(httpOptions).ConfigureAwait(false)) using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
{ {
StreamReader reader = new StreamReader(response.Content); StreamReader reader = new StreamReader(response.Content);
string responseString = reader.ReadToEnd(); string responseString = reader.ReadToEnd();
@ -138,7 +138,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]"; var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
httpOptions.RequestContent = requestBody; httpOptions.RequestContent = requestBody;
using (var innerResponse = await Post(httpOptions).ConfigureAwait(false)) using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
{ {
StreamReader innerReader = new StreamReader(innerResponse.Content); StreamReader innerReader = new StreamReader(innerResponse.Content);
responseString = innerReader.ReadToEnd(); responseString = innerReader.ReadToEnd();
@ -148,7 +148,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
responseString); responseString);
var programDict = programDetails.ToDictionary(p => p.programID, y => y); var programDict = programDetails.ToDictionary(p => p.programID, y => y);
var images = await GetImageForPrograms(programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken); var images = await GetImageForPrograms(info, programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken);
var schedules = dailySchedules.SelectMany(d => d.programs); var schedules = dailySchedules.SelectMany(d => d.programs);
foreach (ScheduleDirect.Program schedule in schedules) foreach (ScheduleDirect.Program schedule in schedules)
@ -229,7 +229,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
httpOptions.RequestHeaders["token"] = token; httpOptions.RequestHeaders["token"] = token;
using (var response = await Get(httpOptions).ConfigureAwait(false)) using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
{ {
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response); var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
_logger.Info("Found " + root.map.Count() + " channels on the lineup on ScheduleDirect"); _logger.Info("Found " + root.map.Count() + " channels on the lineup on ScheduleDirect");
@ -447,7 +447,9 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
return url; return url;
} }
private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(List<string> programIds, private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(
ListingsProviderInfo info,
List<string> programIds,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var imageIdString = "["; var imageIdString = "[";
@ -472,7 +474,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
TimeoutMs = 60000 TimeoutMs = 60000
}; };
List<ScheduleDirect.ShowImages> images; List<ScheduleDirect.ShowImages> images;
using (var innerResponse2 = await Post(httpOptions).ConfigureAwait(false)) using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
{ {
images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>( images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
innerResponse2.Content); innerResponse2.Content);
@ -504,7 +506,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
try try
{ {
using (Stream responce = await Get(options).ConfigureAwait(false)) using (Stream responce = await Get(options, false, info).ConfigureAwait(false))
{ {
var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce); var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
@ -606,30 +608,58 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
} }
} }
private async Task<HttpResponseInfo> Post(HttpRequestOptions options) private async Task<HttpResponseInfo> Post(HttpRequestOptions options,
bool enableRetry,
ListingsProviderInfo providerInfo)
{ {
try try
{ {
return await _httpClient.Post(options).ConfigureAwait(false); return await _httpClient.Post(options).ConfigureAwait(false);
} }
catch catch (HttpException ex)
{ {
_tokens.Clear(); _tokens.Clear();
throw;
} if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry) {
throw;
}
}
var newToken = await GetToken (providerInfo, options.CancellationToken).ConfigureAwait (false);
options.RequestHeaders ["token"] = newToken;
return await Post (options, false, providerInfo).ConfigureAwait (false);
} }
private async Task<Stream> Get(HttpRequestOptions options) private async Task<Stream> Get(HttpRequestOptions options,
bool enableRetry,
ListingsProviderInfo providerInfo)
{ {
try try
{ {
return await _httpClient.Get(options).ConfigureAwait(false); return await _httpClient.Get(options).ConfigureAwait(false);
} }
catch catch (HttpException ex)
{ {
_tokens.Clear(); _tokens.Clear();
throw;
} if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry) {
throw;
}
}
var newToken = await GetToken (providerInfo, options.CancellationToken).ConfigureAwait (false);
options.RequestHeaders ["token"] = newToken;
return await Get (options, false, providerInfo).ConfigureAwait (false);
} }
private async Task<string> GetTokenInternal(string username, string password, private async Task<string> GetTokenInternal(string username, string password,
@ -646,7 +676,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
//_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " + //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
// httpOptions.RequestContent); // httpOptions.RequestContent);
using (var responce = await Post(httpOptions).ConfigureAwait(false)) using (var responce = await Post(httpOptions, false, null).ConfigureAwait(false))
{ {
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content); var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
if (root.message == "OK") if (root.message == "OK")
@ -728,7 +758,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings
try try
{ {
using (var response = await Get(options).ConfigureAwait(false)) using (var response = await Get(options, false, null).ConfigureAwait(false))
{ {
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response); var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);

Loading…
Cancel
Save