Scrobbler.SendCachedScrobblesAsync()

This commit is contained in:
Rikki Tooley 2015-04-26 19:36:17 +01:00
parent 3e899833c2
commit 65448d6032
2 changed files with 16 additions and 5 deletions

View File

@ -15,5 +15,7 @@ public interface IScrobbler
Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble); Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble);
Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles); Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles);
Task<ScrobbleResponse> SendCachedScrobblesAsync();
} }
} }

View File

@ -1,7 +1,6 @@
using IF.Lastfm.Core.Api; using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Api.Commands.Track; using IF.Lastfm.Core.Api.Commands.Track;
using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Helpers; using IF.Lastfm.Core.Helpers;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -32,7 +31,17 @@ public Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble)
return ScrobbleAsync(new[] {scrobble}); return ScrobbleAsync(new[] {scrobble});
} }
public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles) public Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles)
{
return ScrobbleAsyncInternal(scrobbles);
}
public Task<ScrobbleResponse> SendCachedScrobblesAsync()
{
return ScrobbleAsyncInternal(Enumerable.Empty<Scrobble>());
}
public async Task<ScrobbleResponse> ScrobbleAsyncInternal(IEnumerable<Scrobble> scrobbles)
{ {
var scrobblesList = scrobbles.ToList(); var scrobblesList = scrobbles.ToList();
var cached = await GetCachedAsync(); var cached = await GetCachedAsync();
@ -43,7 +52,7 @@ public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble
} }
var batches = pending.Batch(MaxBatchSize); var batches = pending.Batch(MaxBatchSize);
var responses = new List<ScrobbleResponse>(pending.Count / MaxBatchSize); var responses = new List<ScrobbleResponse>(pending.Count % MaxBatchSize);
var responseExceptions = new List<Exception>(); var responseExceptions = new List<Exception>();
foreach(var batch in batches) foreach(var batch in batches)
{ {
@ -65,7 +74,7 @@ public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble
} }
ScrobbleResponse scrobblerResponse; ScrobbleResponse scrobblerResponse;
if (responses.All(r => r.Success)) if (!responses.Any() || responses.All(r => r.Success))
{ {
scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful); scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
} }