@@ -16,20 +16,34 @@ public static class APIConsumer
1616 /// </summary>
1717 private const string CONTENTTYPE = "application/json; charset=utf-8" ;
1818
19+ /// <summary>
20+ /// Fixed string for POST method
21+ /// </summary>
22+ private const string POST_WebMethod = "POST" ;
23+
24+ /// <summary>
25+ /// Fixed string for GET method
26+ /// </summary>
27+ private const string GET_WebMethod = "GET" ;
28+
1929 /// <summary>
2030 /// POST to Resful API sending Json body.
2131 /// </summary>
2232 /// <param name="url">API URL</param>
2333 /// <param name="JsonBody">Content Application By Default Json</param>
34+ /// <param name="Token">Header Authorization token</param>
2435 /// <returns>String Api result</returns>
25- public static string POSTMethod ( string url , string JsonBody )
36+ public static string POSTMethod ( string url , string JsonBody , string Token = "" )
2637 {
2738 string ContentResult = string . Empty ;
2839 try
2940 {
3041 HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
3142 request . ContentType = CONTENTTYPE ;
32- request . Method = "POST" ;
43+ request . Method = POST_WebMethod ;
44+
45+ if ( ! String . IsNullOrEmpty ( Token ) )
46+ request . Headers . Add ( HttpRequestHeader . Authorization , Token ) ;
3347
3448 using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
3549 {
@@ -57,15 +71,19 @@ public static string POSTMethod(string url, string JsonBody)
5771 /// Request GET Method to the URL API provided.
5872 /// </summary>
5973 /// <param name="url">API URL</param>
74+ /// <param name="Token">Header Authorization Token</param>
6075 /// <returns>String Api result</returns>
61- public static string GETMethod ( string url )
76+ public static string GETMethod ( string url , string Token = "" )
6277 {
6378 string ContentResult = string . Empty ;
6479 try
6580 {
6681 HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
6782 request . ContentType = CONTENTTYPE ;
68- request . Method = "GET" ;
83+ request . Method = GET_WebMethod ;
84+
85+ if ( ! String . IsNullOrEmpty ( Token ) )
86+ request . Headers . Add ( HttpRequestHeader . Authorization , Token ) ;
6987
7088 var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
7189 using ( var streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
@@ -88,15 +106,19 @@ public static string GETMethod(string url)
88106 /// </summary>
89107 /// <param name="url">API URL</param>
90108 /// <param name="Id">Id</param>
109+ /// <param name="Token">Authorization Token</param>
91110 /// <returns>String Api result</returns>
92- public static string GETMethod ( string url , string Id )
111+ public static string GETMethod ( string url , string Id , string Token = "" )
93112 {
94113 string ContentResult = string . Empty ;
95114 try
96115 {
97116 HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( string . Concat ( url , "/" , Id ) ) ;
98117 request . ContentType = CONTENTTYPE ;
99- request . Method = "GET" ;
118+ request . Method = GET_WebMethod ;
119+
120+ if ( ! string . IsNullOrEmpty ( Token ) )
121+ request . Headers . Add ( HttpRequestHeader . Authorization , Token ) ;
100122
101123 var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
102124 using ( var streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
@@ -119,15 +141,19 @@ public static string GETMethod(string url, string Id)
119141 /// </summary>
120142 /// <typeparam name="T">Object used to deserialize the result</typeparam>
121143 /// <param name="url">API URL</param>
144+ /// <param name="Token">Authorization token</param>
122145 /// <returns>String Api result</returns>
123- public static string GETMethod < T > ( string url , ref T ObjectResult )
146+ public static string GETMethod < T > ( string url , ref T ObjectResult , string Token = "" )
124147 {
125148 string ContentResult = string . Empty ;
126149 try
127150 {
128151 HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
129152 request . ContentType = CONTENTTYPE ;
130- request . Method = "GET" ;
153+ request . Method = GET_WebMethod ;
154+
155+ if ( ! string . IsNullOrEmpty ( Token ) )
156+ request . Headers . Add ( HttpRequestHeader . Authorization , Token ) ;
131157
132158 var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
133159 using ( var streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
@@ -151,15 +177,19 @@ public static string GETMethod<T>(string url, ref T ObjectResult)
151177 /// </summary>
152178 /// <typeparam name="T">Object used to deserialize the result</typeparam>
153179 /// <param name="url">API URL</param>
180+ /// <param name="Token">Authorization token</param>
154181 /// <returns>String Api result</returns>
155- public static string GETMethod < T > ( string url , string Id , ref T ObjectResult )
182+ public static string GETMethod < T > ( string url , string Id , ref T ObjectResult , string Token = "" )
156183 {
157184 string ContentResult = string . Empty ;
158185 try
159186 {
160187 HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( string . Concat ( url , "/" , Id ) ) ;
161188 request . ContentType = CONTENTTYPE ;
162- request . Method = "GET" ;
189+ request . Method = GET_WebMethod ;
190+
191+ if ( ! string . IsNullOrEmpty ( Token ) )
192+ request . Headers . Add ( HttpRequestHeader . Authorization , Token ) ;
163193
164194 var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
165195 using ( var streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
0 commit comments