PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Server/DeviceHive.Test/JsonClient.cs

https://github.com/oryol/devicehive-.net
C# | 135 lines | 118 code | 14 blank | 3 comment | 7 complexity | 9df93fdbc6711121cab0a9e140e06325 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.IO;
  6. using System.Text;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. namespace DeviceHive.Test
  10. {
  11. public class JsonClient
  12. {
  13. private string _baseUrl;
  14. public JsonClient(string baseUrl)
  15. {
  16. _baseUrl = baseUrl;
  17. }
  18. public JsonResponse Get(string url, Authorization auth = null)
  19. {
  20. return Run("GET", url, auth: auth);
  21. }
  22. public JsonResponse Post(string url, object request, Authorization auth = null)
  23. {
  24. return Run("POST", url, jsonRequest: JObject.FromObject(request), auth: auth);
  25. }
  26. public JsonResponse Put(string url, object request, Authorization auth = null)
  27. {
  28. return Run("PUT", url, jsonRequest: JObject.FromObject(request), auth: auth);
  29. }
  30. public JsonResponse Delete(string url, Authorization auth = null)
  31. {
  32. return Run("DELETE", url, auth: auth);
  33. }
  34. public JsonResponse Run(string method, string url, JObject jsonRequest = null, Authorization auth = null)
  35. {
  36. if (string.IsNullOrEmpty(method))
  37. throw new ArgumentException("Method is null or empty!", "method");
  38. if (string.IsNullOrEmpty(url))
  39. throw new ArgumentException("URL is null or empty!", "url");
  40. // prepare request
  41. var request = (HttpWebRequest)HttpWebRequest.Create(_baseUrl + url);
  42. request.Method = method;
  43. request.Accept = "application/json";
  44. if (auth != null)
  45. {
  46. switch (auth.Type)
  47. {
  48. case "User":
  49. request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(
  50. Encoding.UTF8.GetBytes(string.Format("{0}:{1}", auth.Login, auth.Password)));
  51. break;
  52. case "Device":
  53. request.Headers["Auth-DeviceID"] = auth.Login;
  54. request.Headers["Auth-DeviceKey"] = auth.Password;
  55. break;
  56. }
  57. }
  58. if (jsonRequest != null)
  59. {
  60. request.ContentType = "application/json";
  61. using (var stream = request.GetRequestStream())
  62. {
  63. using (var writer = new StreamWriter(stream))
  64. {
  65. writer.Write(jsonRequest.ToString());
  66. }
  67. }
  68. }
  69. // perform a call
  70. HttpWebResponse response;
  71. try
  72. {
  73. response = (HttpWebResponse)request.GetResponse();
  74. }
  75. catch (WebException ex)
  76. {
  77. response = (HttpWebResponse)ex.Response;
  78. }
  79. // parse response
  80. using (var stream = response.GetResponseStream())
  81. {
  82. using (var reader = new StreamReader(stream))
  83. {
  84. var responseString = reader.ReadToEnd();
  85. try
  86. {
  87. var json = string.IsNullOrEmpty(responseString) ? null : JToken.Parse(responseString);
  88. return new JsonResponse((int)response.StatusCode, json);
  89. }
  90. catch (JsonReaderException ex)
  91. {
  92. throw new WebException(string.Format("Error while parsing server response! " +
  93. "Status: {0}, Response: {1}", (int)response.StatusCode, responseString), ex);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. public class Authorization
  100. {
  101. public string Type { get; private set; }
  102. public string Login { get; private set; }
  103. public string Password { get; private set; }
  104. public Authorization(string type, string login, string password)
  105. {
  106. Type = type;
  107. Login = login;
  108. Password = password;
  109. }
  110. }
  111. public class JsonResponse
  112. {
  113. public int Status { get; private set; }
  114. public JToken Json { get; private set; }
  115. public JsonResponse(int status, JToken json)
  116. {
  117. Status = status;
  118. Json = json;
  119. }
  120. }
  121. }