@@ -24,34 +24,29 @@ internal class SteamWebRequest : ISteamWebRequest
2424 {
2525 private string steamWebApiBaseUrl ;
2626 private readonly string steamWebApiKey ;
27- private ISteamWebHttpClient steamWebHttpClient ;
27+ private readonly HttpClient httpClient ;
2828
2929 /// <summary>
3030 /// Every web request requires a secret Steam Web API key
3131 /// </summary>
3232 /// <param name="steamWebApiBaseUrl">Base URL for the API (such as http://api.steampowered.com)</param>
3333 /// <param name="steamWebApiKey">Secret Steam Web API key provided at the Valve developer website</param>
3434 /// <param name="steamWebHttpClient">Optional custom http client implementation for dependency injection</param>
35- public SteamWebRequest ( string steamWebApiBaseUrl , string steamWebApiKey , ISteamWebHttpClient steamWebHttpClient )
35+ public SteamWebRequest ( string steamWebApiBaseUrl , string steamWebApiKey , HttpClient httpClient = null )
3636 {
37- if ( String . IsNullOrWhiteSpace ( steamWebApiBaseUrl ) )
37+ if ( string . IsNullOrWhiteSpace ( steamWebApiBaseUrl ) )
3838 {
3939 throw new ArgumentNullException ( nameof ( steamWebApiBaseUrl ) ) ;
4040 }
4141
42- if ( String . IsNullOrWhiteSpace ( steamWebApiKey ) )
42+ if ( string . IsNullOrWhiteSpace ( steamWebApiKey ) )
4343 {
4444 throw new ArgumentNullException ( nameof ( steamWebApiKey ) ) ;
4545 }
4646
47- if ( steamWebHttpClient == null )
48- {
49- throw new ArgumentNullException ( nameof ( steamWebHttpClient ) ) ;
50- }
51-
5247 this . steamWebApiBaseUrl = steamWebApiBaseUrl ;
5348 this . steamWebApiKey = steamWebApiKey ;
54- this . steamWebHttpClient = steamWebHttpClient ;
49+ this . httpClient = httpClient ?? new HttpClient ( ) ;
5550 }
5651
5752 /// <summary>
@@ -65,8 +60,8 @@ public SteamWebRequest(string steamWebApiBaseUrl, string steamWebApiKey, ISteamW
6560 /// <returns>Deserialized object from JSON response</returns>
6661 public async Task < ISteamWebResponse < T > > GetAsync < T > ( string interfaceName , string methodName , int methodVersion , IList < SteamWebRequestParameter > parameters = null )
6762 {
68- Debug . Assert ( ! String . IsNullOrWhiteSpace ( interfaceName ) ) ;
69- Debug . Assert ( ! String . IsNullOrWhiteSpace ( methodName ) ) ;
63+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( interfaceName ) ) ;
64+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( methodName ) ) ;
7065 Debug . Assert ( methodVersion > 0 ) ;
7166
7267 return await SendWebRequestAsync < T > ( HttpMethod . GET , interfaceName , methodName , methodVersion , parameters ) ;
@@ -83,8 +78,8 @@ public async Task<ISteamWebResponse<T>> GetAsync<T>(string interfaceName, string
8378 /// <returns>Deserialized object from JSON response</returns>
8479 public async Task < ISteamWebResponse < T > > PostAsync < T > ( string interfaceName , string methodName , int methodVersion , IList < SteamWebRequestParameter > parameters = null )
8580 {
86- Debug . Assert ( ! String . IsNullOrWhiteSpace ( interfaceName ) ) ;
87- Debug . Assert ( ! String . IsNullOrWhiteSpace ( methodName ) ) ;
81+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( interfaceName ) ) ;
82+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( methodName ) ) ;
8883 Debug . Assert ( methodVersion > 0 ) ;
8984
9085 return await SendWebRequestAsync < T > ( HttpMethod . POST , interfaceName , methodName , methodVersion , parameters ) ;
@@ -102,8 +97,8 @@ public async Task<ISteamWebResponse<T>> PostAsync<T>(string interfaceName, strin
10297 /// <returns>Deserialized object from JSON response</returns>
10398 private async Task < ISteamWebResponse < T > > SendWebRequestAsync < T > ( HttpMethod httpMethod , string interfaceName , string methodName , int methodVersion , IList < SteamWebRequestParameter > parameters = null )
10499 {
105- Debug . Assert ( ! String . IsNullOrWhiteSpace ( interfaceName ) ) ;
106- Debug . Assert ( ! String . IsNullOrWhiteSpace ( methodName ) ) ;
100+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( interfaceName ) ) ;
101+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( methodName ) ) ;
107102 Debug . Assert ( methodVersion > 0 ) ;
108103
109104 if ( parameters == null )
@@ -119,7 +114,13 @@ private async Task<ISteamWebResponse<T>> SendWebRequestAsync<T>(HttpMethod httpM
119114 {
120115 string command = BuildRequestCommand ( interfaceName , methodName , methodVersion , parameters ) ;
121116
122- httpResponse = await steamWebHttpClient . GetAsync ( command ) . ConfigureAwait ( false ) ;
117+ httpResponse = await httpClient . GetAsync ( command ) . ConfigureAwait ( false ) ;
118+ httpResponse . EnsureSuccessStatusCode ( ) ;
119+ if ( httpResponse . Content == null )
120+ {
121+ httpResponse = new HttpResponseMessage ( HttpStatusCode . NoContent ) ;
122+ }
123+
123124 }
124125 else if ( httpMethod == HttpMethod . POST )
125126 {
@@ -129,7 +130,12 @@ private async Task<ISteamWebResponse<T>> SendWebRequestAsync<T>(HttpMethod httpM
129130 // Instead, parameters are passed through this container.
130131 FormUrlEncodedContent content = BuildRequestContent ( parameters ) ;
131132
132- httpResponse = await steamWebHttpClient . PostAsync ( command , content ) . ConfigureAwait ( false ) ;
133+ httpResponse = await httpClient . PostAsync ( command , content ) . ConfigureAwait ( false ) ;
134+ httpResponse . EnsureSuccessStatusCode ( ) ;
135+ if ( httpResponse . Content == null )
136+ {
137+ httpResponse = new HttpResponseMessage ( HttpStatusCode . NoContent ) ;
138+ }
133139 }
134140
135141 var headers = httpResponse . Content ? . Headers ;
@@ -166,22 +172,22 @@ private async Task<ISteamWebResponse<T>> SendWebRequestAsync<T>(HttpMethod httpM
166172 /// <returns></returns>
167173 private string BuildRequestCommand ( string interfaceName , string methodName , int methodVersion , IEnumerable < SteamWebRequestParameter > parameters )
168174 {
169- Debug . Assert ( ! String . IsNullOrWhiteSpace ( interfaceName ) ) ;
170- Debug . Assert ( ! String . IsNullOrWhiteSpace ( methodName ) ) ;
175+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( interfaceName ) ) ;
176+ Debug . Assert ( ! string . IsNullOrWhiteSpace ( methodName ) ) ;
171177 Debug . Assert ( methodVersion > 0 ) ;
172178
173179 if ( steamWebApiBaseUrl . EndsWith ( "/" ) )
174180 {
175181 steamWebApiBaseUrl = steamWebApiBaseUrl . Remove ( steamWebApiBaseUrl . Length - 1 , 1 ) ;
176182 }
177183
178- string commandUrl = String . Format ( "{0}/{1}/{2}/v{3}/" , steamWebApiBaseUrl , interfaceName , methodName , methodVersion ) ;
184+ string commandUrl = string . Format ( "{0}/{1}/{2}/v{3}/" , steamWebApiBaseUrl , interfaceName , methodName , methodVersion ) ;
179185
180186 // if we have parameters, join them together with & delimiter and append them to the command URL
181187 if ( parameters != null && parameters . Count ( ) > 0 )
182188 {
183- string parameterString = String . Join ( "&" , parameters ) ;
184- commandUrl += String . Format ( "?{0}" , parameterString ) ;
189+ string parameterString = string . Join ( "&" , parameters ) ;
190+ commandUrl += string . Format ( "?{0}" , parameterString ) ;
185191 }
186192
187193 return commandUrl ;
@@ -210,9 +216,9 @@ private static FormUrlEncodedContent BuildRequestContent(IEnumerable<SteamWebReq
210216 /// <returns>String containing the http endpoint response contents</returns>
211217 private static string CleanupResponseString ( string stringToClean )
212218 {
213- if ( String . IsNullOrWhiteSpace ( stringToClean ) )
219+ if ( string . IsNullOrWhiteSpace ( stringToClean ) )
214220 {
215- return String . Empty ;
221+ return string . Empty ;
216222 }
217223
218224 stringToClean = stringToClean . Replace ( "\n " , "" ) ;
0 commit comments