1
1
using System ;
2
- using System . IO ;
2
+ using System . Collections . Generic ;
3
3
using System . Net ;
4
+ using System . Net . Http ;
5
+ using System . Threading . Tasks ;
4
6
using Microsoft . Extensions . Logging ;
5
7
using Microsoft . Extensions . Logging . Abstractions ;
6
8
using Newtonsoft . Json ;
@@ -25,33 +27,44 @@ public ResourceFetcher(ILoggerFactory loggerFactory)
25
27
/// Fetch a json object from a given uri and deserialize it to the specified class: T
26
28
/// </summary>
27
29
/// <returns></returns>
28
- public T Fetch < T > ( Uri endpoint )
30
+ public T FetchJson < T > ( Uri endpoint , string method , Dictionary < string , string > header = null )
29
31
{
30
- string response = ReadResource ( endpoint , "GET" ) ;
32
+ string response = ReadResource ( endpoint , method , header ) . Result ;
31
33
32
34
return JsonConvert . DeserializeObject < T > ( response ) ;
33
35
}
34
36
35
- private string ReadResource ( Uri endpoint , string method )
37
+ /// <summary>
38
+ /// Fetch string from a given uri
39
+ /// </summary>
40
+ /// <returns></returns>
41
+ public string FetchString ( Uri endpoint , string method , Dictionary < string , string > headers = null )
42
+ {
43
+ string response = ReadResource ( endpoint , method , headers ) . Result ;
44
+
45
+ return response ;
46
+ }
47
+
48
+ private async Task < string > ReadResource ( Uri endpoint , string method , Dictionary < string , string > headers )
36
49
{
37
50
try
38
51
{
39
- var httpWebRequest = GetHttpWebRequest ( endpoint , method ) ;
52
+ headers ??= new Dictionary < string , string > ( ) ;
40
53
41
- var httpWebResponse = ( HttpWebResponse ) httpWebRequest . GetResponse ( ) ;
54
+ var httpResponse = GetResponse ( endpoint , method , headers ) ;
42
55
43
- if ( httpWebResponse . StatusCode == HttpStatusCode . OK )
44
- {
45
- return GetResponse ( httpWebResponse ) ;
46
- }
47
- else if ( httpWebResponse . StatusCode == HttpStatusCode . NotFound )
56
+ if ( httpResponse . StatusCode == HttpStatusCode . OK )
48
57
{
49
- throw new EMFClientException ( "The requested metadata is not found at " + httpWebRequest . RequestUri . AbsolutePath ) ;
58
+ return await httpResponse . Content . ReadAsStringAsync ( ) ;
50
59
}
51
- else
60
+
61
+ if ( httpResponse . StatusCode == HttpStatusCode . NotFound )
52
62
{
53
- HandleErrorResponse ( httpWebResponse ) ;
63
+ throw new EMFClientException ( "The requested data is not found at " + endpoint . AbsolutePath ) ;
54
64
}
65
+
66
+ throw new EMFClientException ( "Failed to get resource. Error code: " + httpResponse . StatusCode +
67
+ ", error message: " + httpResponse . ReasonPhrase ) ;
55
68
}
56
69
catch ( Exception e )
57
70
{
@@ -61,52 +74,22 @@ private string ReadResource(Uri endpoint, string method)
61
74
+ "\n Attempting to reconnect." ) ;
62
75
throw new EMFClientException ( "Failed to connect to service endpoint: " , e ) ;
63
76
}
64
-
65
- return string . Empty ;
66
77
}
67
78
68
- private void HandleErrorResponse ( HttpWebResponse httpWebResponse )
79
+ private HttpResponseMessage GetResponse ( Uri endpoint , string method , Dictionary < string , string > headers )
69
80
{
70
- string errorResponse = GetResponse ( httpWebResponse ) ;
81
+ HttpClient client = new HttpClient ( ) ;
71
82
72
- try
73
- {
74
- /*JsonNode node = Jackson.jsonNodeOf(errorResponse);
75
- JsonNode code = node.get("code");
76
- JsonNode message = node.get("message");
77
- if (code != null && message != null) {
78
- errorCode = code.asText();
79
- responseMessage = message.asText();
80
- }
81
-
82
- String exceptionMessage =
83
- String.format(
84
- "Failed to get resource. Error code: %s, error message: %s ",
85
- errorCode, responseMessage);
86
- throw new EMFClientException(exceptionMessage);*/
87
- }
88
- catch ( System . Exception exception )
83
+ var httpMethod = new HttpMethod ( method . ToUpper ( ) ) ;
84
+ HttpRequestMessage request = new HttpRequestMessage ( httpMethod , endpoint ) ;
85
+ foreach ( KeyValuePair < string , string > header in headers )
89
86
{
90
- throw new EMFClientException ( "Unable to parse error stream: " , exception ) ;
87
+ request . Headers . Add ( header . Key , header . Value ) ;
91
88
}
92
- }
93
-
94
- private HttpWebRequest GetHttpWebRequest ( Uri endpoint , string method )
95
- {
96
- var httpWebRequest = ( HttpWebRequest ) WebRequest . CreateHttp ( endpoint ) ;
97
- httpWebRequest . Method = method ;
98
- httpWebRequest . Timeout = 1000 ;
99
- httpWebRequest . ReadWriteTimeout = 1000 ;
100
- return httpWebRequest ;
101
- }
102
-
103
- private string GetResponse ( HttpWebResponse response )
104
- {
105
- var inputStream = response . GetResponseStream ( ) ;
106
89
107
- // convert stream to string
108
- using var reader = new StreamReader ( inputStream ) ;
109
- return reader . ReadToEnd ( ) ;
90
+ Task < HttpResponseMessage > response = client . SendAsync ( request ) ;
91
+ HttpResponseMessage result = response . Result ;
92
+ return result ;
110
93
}
111
94
}
112
95
}
0 commit comments