|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Net; |
| 5 | + |
| 6 | +namespace SQLAPI_Consumer |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// This class will be used for consume API using the URL provided. |
| 10 | + /// Actually only GET & POST Method are supported. |
| 11 | + /// </summary> |
| 12 | + public static class APIConsumer |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Fixed Context type supported. |
| 16 | + /// </summary> |
| 17 | + private const string CONTENTTYPE = "application/json; charset=utf-8"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// POST to Resful API sending Json body. |
| 21 | + /// </summary> |
| 22 | + /// <param name="url">API URL</param> |
| 23 | + /// <param name="JsonBody">Content Application By Default Json</param> |
| 24 | + /// <returns>String Api result</returns> |
| 25 | + public static string POSTMethod(string url, string JsonBody) |
| 26 | + { |
| 27 | + string ContentResult = string.Empty ; |
| 28 | + try |
| 29 | + { |
| 30 | + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
| 31 | + request.ContentType = CONTENTTYPE; |
| 32 | + request.Method = "POST"; |
| 33 | + |
| 34 | + using (var streamWriter = new StreamWriter(request.GetRequestStream())) |
| 35 | + { |
| 36 | + streamWriter.Write(JsonBody); |
| 37 | + streamWriter.Flush(); |
| 38 | + } |
| 39 | + |
| 40 | + var httpResponse = (HttpWebResponse)request.GetResponse(); |
| 41 | + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) |
| 42 | + { |
| 43 | + var result = streamReader.ReadToEnd(); |
| 44 | + ContentResult = result; |
| 45 | + } |
| 46 | + } |
| 47 | + catch (Exception ex) |
| 48 | + { |
| 49 | + ContentResult = ex.Message.ToString(); |
| 50 | + throw ex; |
| 51 | + } |
| 52 | + |
| 53 | + return ContentResult; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Request GET Method to the URL API provided. |
| 58 | + /// </summary> |
| 59 | + /// <param name="url">API URL</param> |
| 60 | + /// <returns>String Api result</returns> |
| 61 | + public static string GETMethod(string url) |
| 62 | + { |
| 63 | + string ContentResult = string.Empty; |
| 64 | + try |
| 65 | + { |
| 66 | + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
| 67 | + request.ContentType = CONTENTTYPE; |
| 68 | + request.Method = "GET"; |
| 69 | + |
| 70 | + var httpResponse = (HttpWebResponse)request.GetResponse(); |
| 71 | + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) |
| 72 | + { |
| 73 | + var result = streamReader.ReadToEnd(); |
| 74 | + ContentResult = result; |
| 75 | + } |
| 76 | + } |
| 77 | + catch (Exception ex) |
| 78 | + { |
| 79 | + ContentResult = ex.Message.ToString(); |
| 80 | + throw ex; |
| 81 | + } |
| 82 | + |
| 83 | + return ContentResult; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Request GET Method to the URL API provided. |
| 88 | + /// </summary> |
| 89 | + /// <param name="url">API URL</param> |
| 90 | + /// <param name="Id">Id</param> |
| 91 | + /// <returns>String Api result</returns> |
| 92 | + public static string GETMethod(string url, string Id) |
| 93 | + { |
| 94 | + string ContentResult = string.Empty; |
| 95 | + try |
| 96 | + { |
| 97 | + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id)); |
| 98 | + request.ContentType = CONTENTTYPE; |
| 99 | + request.Method = "GET"; |
| 100 | + |
| 101 | + var httpResponse = (HttpWebResponse)request.GetResponse(); |
| 102 | + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) |
| 103 | + { |
| 104 | + var result = streamReader.ReadToEnd(); |
| 105 | + ContentResult = result; |
| 106 | + } |
| 107 | + } |
| 108 | + catch (Exception ex) |
| 109 | + { |
| 110 | + ContentResult = ex.Message.ToString(); |
| 111 | + throw ex; |
| 112 | + } |
| 113 | + |
| 114 | + return ContentResult; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Request GET Method to the URL API provided. |
| 119 | + /// </summary> |
| 120 | + /// <typeparam name="T">Object used to deserialize the result</typeparam> |
| 121 | + /// <param name="url">API URL</param> |
| 122 | + /// <returns>String Api result</returns> |
| 123 | + public static string GETMethod<T>(string url, ref T ObjectResult) |
| 124 | + { |
| 125 | + string ContentResult = string.Empty; |
| 126 | + try |
| 127 | + { |
| 128 | + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
| 129 | + request.ContentType = CONTENTTYPE; |
| 130 | + request.Method = "GET"; |
| 131 | + |
| 132 | + var httpResponse = (HttpWebResponse)request.GetResponse(); |
| 133 | + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) |
| 134 | + { |
| 135 | + var result = streamReader.ReadToEnd(); |
| 136 | + ObjectResult = JsonConvert.DeserializeObject<T>(result); |
| 137 | + ContentResult = result; |
| 138 | + } |
| 139 | + } |
| 140 | + catch (Exception ex) |
| 141 | + { |
| 142 | + ContentResult = ex.Message.ToString(); |
| 143 | + throw ex; |
| 144 | + } |
| 145 | + |
| 146 | + return ContentResult; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Request GET Method to the URL API provided. |
| 151 | + /// </summary> |
| 152 | + /// <typeparam name="T">Object used to deserialize the result</typeparam> |
| 153 | + /// <param name="url">API URL</param> |
| 154 | + /// <returns>String Api result</returns> |
| 155 | + public static string GETMethod<T>(string url, string Id, ref T ObjectResult) |
| 156 | + { |
| 157 | + string ContentResult = string.Empty; |
| 158 | + try |
| 159 | + { |
| 160 | + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id)); |
| 161 | + request.ContentType = CONTENTTYPE; |
| 162 | + request.Method = "GET"; |
| 163 | + |
| 164 | + var httpResponse = (HttpWebResponse)request.GetResponse(); |
| 165 | + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) |
| 166 | + { |
| 167 | + var result = streamReader.ReadToEnd(); |
| 168 | + ObjectResult = JsonConvert.DeserializeObject<T>(result); |
| 169 | + ContentResult = result; |
| 170 | + } |
| 171 | + } |
| 172 | + catch (Exception ex) |
| 173 | + { |
| 174 | + ContentResult = ex.Message.ToString(); |
| 175 | + throw ex; |
| 176 | + } |
| 177 | + |
| 178 | + return ContentResult; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments