private async static Task FetchUser(BGGClient client, string username, bool retry) { var request = new BGGCollectionRequest { Username = username }; var task = client.GetCollectionAsync(request); var response = await task; if (response.StatusCode == HttpStatusCode.Accepted) { if (retry) { Console.WriteLine("Waiting to get collection for {0}", username); Thread.Sleep(TimeSpan.FromSeconds(30)); await FetchUser(client, username, false); return; } else { Console.WriteLine("Failed to fetch collection at second attempt for {0}", username); } } var collection = response.Data; lock (LockObject) { foreach (var game in collection.Items) { var id = game.Id; GameNames[id] = game.Name; if (game.Owned) { var owners = Owners.GetOrCreate(id, () => new List <string>()); owners.Add(username); } if (game.WantToPlay) { var players = WantToPlay.GetOrCreate(id, () => new List <string>()); players.Add(username); } } } }
private async static Task <BGGCollection> FetchCollection(BGGClient client, BggDbContext db, DateTime now, string username, bool retry) { var existingUser = db.Users.SingleOrDefault(user => user.Username == username); if (existingUser != null && now - existingUser.CollectionLastFetched <= TimeSpan.FromDays(7)) { return(null); } var request = new BGGCollectionRequest { Username = username, ExcludeSubtype = BGGSubtype.BoardGameExpansion, Stats = true }; var task = client.GetCollectionAsync(request); var response = await task; if (response.StatusCode == HttpStatusCode.Accepted) { if (retry) { Console.WriteLine("Waiting to get collection for {0}", username); Thread.Sleep(TimeSpan.FromSeconds(30)); return(await FetchCollection(client, db, now, username, false)); } else { Console.WriteLine("Failed to fetch collection at second attempt for {0}", username); throw new InvalidOperationException("Couldn't fetch collection"); } } if (existingUser == null) { db.Users.Add(new User { Username = username, CollectionLastFetched = now }); } else { existingUser.CollectionLastFetched = now; } db.SaveChanges(); return(response.Data); }