PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/MalApi/RetryOnFailureMyAnimeListApi.cs

https://bitbucket.org/LHCGreg/mal-api
C# | 98 lines | 70 code | 9 blank | 19 comment | 5 complexity | 00bfb4a2cee97189548f122209461c62 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace MalApi
  7. {
  8. /// <summary>
  9. /// Retries failed requests after waiting for a short period. After a certain number of failures, it gives up and propagates the
  10. /// thrown exception.
  11. /// </summary>
  12. public class RetryOnFailureMyAnimeListApi : IMyAnimeListApi
  13. {
  14. private IMyAnimeListApi m_underlyingApi;
  15. private bool m_ownApi;
  16. private int m_numTriesBeforeGivingUp;
  17. private int m_timeBetweenRetriesInMs;
  18. public RetryOnFailureMyAnimeListApi(IMyAnimeListApi underlyingApi, int numTriesBeforeGivingUp, int timeBetweenRetriesInMs, bool ownApi = false)
  19. {
  20. m_underlyingApi = underlyingApi;
  21. m_numTriesBeforeGivingUp = numTriesBeforeGivingUp;
  22. m_timeBetweenRetriesInMs = timeBetweenRetriesInMs;
  23. m_ownApi = ownApi;
  24. }
  25. private TResult DoActionWithRetry<TResult>(Func<TResult> action, string baseErrorMessage)
  26. {
  27. bool success = false;
  28. int numTries = 0;
  29. while (true)
  30. {
  31. try
  32. {
  33. return action();
  34. }
  35. catch (MalApiRequestException ex)
  36. {
  37. numTries++;
  38. Logging.Log.ErrorFormat("{0} (failure {1}): {2}", ex, baseErrorMessage, numTries, ex.Message);
  39. if (numTries < m_numTriesBeforeGivingUp)
  40. {
  41. Logging.Log.InfoFormat("Waiting {0} ms before trying again.", m_timeBetweenRetriesInMs);
  42. Thread.Sleep(m_timeBetweenRetriesInMs);
  43. }
  44. else
  45. {
  46. throw;
  47. }
  48. }
  49. }
  50. }
  51. public MalUserLookupResults GetAnimeListForUser(string user)
  52. {
  53. return DoActionWithRetry(() => m_underlyingApi.GetAnimeListForUser(user),
  54. baseErrorMessage: string.Format("Error getting anime list for user {0}", user));
  55. }
  56. public RecentUsersResults GetRecentOnlineUsers()
  57. {
  58. return DoActionWithRetry(() => m_underlyingApi.GetRecentOnlineUsers(),
  59. baseErrorMessage: "Error getting recently active MAL users");
  60. }
  61. public AnimeDetailsResults GetAnimeDetails(int animeId)
  62. {
  63. return DoActionWithRetry(() => m_underlyingApi.GetAnimeDetails(animeId),
  64. baseErrorMessage: string.Format("Error getting details for anime id {0}", animeId));
  65. }
  66. public void Dispose()
  67. {
  68. if (m_ownApi && m_underlyingApi != null)
  69. {
  70. m_underlyingApi.Dispose();
  71. }
  72. }
  73. }
  74. }
  75. /*
  76. Copyright 2012 Greg Najda
  77. Licensed under the Apache License, Version 2.0 (the "License");
  78. you may not use this file except in compliance with the License.
  79. You may obtain a copy of the License at
  80. http://www.apache.org/licenses/LICENSE-2.0
  81. Unless required by applicable law or agreed to in writing, software
  82. distributed under the License is distributed on an "AS IS" BASIS,
  83. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84. See the License for the specific language governing permissions and
  85. limitations under the License.
  86. */