HttpWebResponseExtensions.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. namespace WindowsPhone.Recipes.Push.Messasges
  7. {
  8. /// <summary>
  9. /// Extends the <see cref="HttpWebResponse"/> type with methods for translating push notification specific status codes strings to strong typed enumeration.
  10. /// </summary>
  11. internal static class HttpWebResponseExtensions
  12. {
  13. /// <summary>
  14. /// Gets the Notification Status code as <see cref="NotificationStatus"/> enumeration.
  15. /// </summary>
  16. /// <param name="response">The http web response instance.</param>
  17. /// <returns>Correlate enumeration value.</returns>
  18. public static NotificationStatus GetNotificationStatus(this HttpWebResponse response)
  19. {
  20. return response.GetStatus(
  21. NotificationStatus.NotApplicable,
  22. PushNotificationMessage.Headers.NotificationStatus);
  23. }
  24. /// <summary>
  25. /// Gets the Device Connection Status code as <see cref="NotificationStatus"/> enumeration.
  26. /// </summary>
  27. /// <param name="response">The http web response instance.</param>
  28. /// <returns>Correlate enumeration value.</returns>
  29. public static DeviceConnectionStatus GetDeviceConnectionStatus(this HttpWebResponse response)
  30. {
  31. return response.GetStatus(
  32. DeviceConnectionStatus.NotApplicable,
  33. PushNotificationMessage.Headers.DeviceConnectionStatus);
  34. }
  35. /// <summary>
  36. /// Gets the Subscription Status code as <see cref="NotificationStatus"/> enumeration.
  37. /// </summary>
  38. /// <param name="response">The http web response instance.</param>
  39. /// <returns>Correlate enumeration value.</returns>
  40. public static SubscriptionStatus GetSubscriptionStatus(this HttpWebResponse response)
  41. {
  42. return response.GetStatus(
  43. SubscriptionStatus.NotApplicable,
  44. PushNotificationMessage.Headers.SubscriptionStatus);
  45. }
  46. private static T GetStatus<T>(this HttpWebResponse response, T def, string header) where T : struct
  47. {
  48. string statusString = response.Headers[header];
  49. T status = def;
  50. Enum.TryParse<T>(statusString, out status);
  51. return status;
  52. }
  53. }
  54. }