/// <summary> /// Returns an object of type T created from the deserialization of the JSON response to the passed interface/method/version with the included parameters. /// </summary> /// <typeparam name="T">Type to deserialize into</typeparam> /// <param name="httpMethod">Determines GET or POST request</param> /// <param name="interfaceName">Name of web API interface to call</param> /// <param name="methodName">Name of web API method to call</param> /// <param name="methodVersion">Name of web API method version</param> /// <param name="parameters">List of parameters to append to the web API call</param> /// <returns>Deserialized object from JSON response</returns> private async Task <ISteamWebResponse <T> > SendWebRequestAsync <T>(HttpMethod httpMethod, string interfaceName, string methodName, int methodVersion, IList <SteamWebRequestParameter> parameters = null) { Debug.Assert(!String.IsNullOrWhiteSpace(interfaceName)); Debug.Assert(!String.IsNullOrWhiteSpace(methodName)); Debug.Assert(methodVersion > 0); if (parameters == null) { parameters = new List <SteamWebRequestParameter>(); } parameters.Insert(0, new SteamWebRequestParameter("key", steamWebApiKey)); HttpResponseMessage httpResponse = null; if (httpMethod == HttpMethod.GET) { string command = BuildRequestCommand(interfaceName, methodName, methodVersion, parameters); httpResponse = await httpClient.GetAsync(command).ConfigureAwait(false); } else if (httpMethod == HttpMethod.POST) { // Null is passed instead of the parameters so that they are not appended to the URL. string command = BuildRequestCommand(interfaceName, methodName, methodVersion, null); // Instead, parameters are passed through this container. FormUrlEncodedContent content = BuildRequestContent(parameters); httpResponse = await httpClient.PostAsync(command, content).ConfigureAwait(false); } var headers = httpResponse.Content?.Headers; // extract http headers that we care about SteamWebResponse <T> steamWebResponse = new SteamWebResponse <T>() { ContentLength = headers?.ContentLength, ContentType = headers?.ContentType?.MediaType, ContentTypeCharSet = headers?.ContentType?.CharSet, Expires = headers?.Expires, LastModified = headers?.LastModified, }; // deserialize the content if we have any as indicated by the response code if (httpResponse.StatusCode != HttpStatusCode.NoContent) { string responseContent = await httpResponse.Content.ReadAsStringAsync(); responseContent = CleanupResponseString(responseContent); steamWebResponse.Data = JsonConvert.DeserializeObject <T>(responseContent); } return(steamWebResponse); }
/// <summary> /// Returns an object of type T created from the deserialization of the JSON response to the passed interface/method/version with the included parameters. /// </summary> /// <typeparam name="T">Type to deserialize into</typeparam> /// <param name="httpMethod">Determines GET or POST request</param> /// <param name="interfaceName">Name of web API interface to call</param> /// <param name="methodName">Name of web API method to call</param> /// <param name="methodVersion">Name of web API method version</param> /// <param name="parameters">List of parameters to append to the web API call</param> /// <returns>Deserialized object from JSON response</returns> private async Task <ISteamWebResponse <T> > SendWebRequestAsync <T>(HttpMethod httpMethod, string interfaceName, string methodName, int methodVersion, IList <SteamWebRequestParameter> parameters = null) { if (string.IsNullOrWhiteSpace(interfaceName)) { throw new ArgumentNullException(nameof(interfaceName)); } if (string.IsNullOrWhiteSpace(methodName)) { throw new ArgumentNullException(nameof(methodName)); } if (methodVersion <= 0) { throw new ArgumentOutOfRangeException(nameof(methodVersion)); } if (parameters == null) { parameters = new List <SteamWebRequestParameter>(); } parameters.Insert(0, new SteamWebRequestParameter("key", steamWebApiKey)); //parameters.Insert(0, new SteamWebRequestParameter("feeds", "SteamDB")); -- Demonstration of Implementing Feeds Parameter //parameters.Insert(0, new SteamWebRequestParameter("tags", "halloween")); -- Demonstration of Implementing Tags Parameter HttpResponseMessage httpResponse = null; if (httpMethod == HttpMethod.GET) { string command = BuildRequestCommand(interfaceName, methodName, methodVersion, parameters); httpResponse = await httpClient.GetAsync(command).ConfigureAwait(false); httpResponse.EnsureSuccessStatusCode(); if (httpResponse.Content == null) { httpResponse = new HttpResponseMessage(HttpStatusCode.NoContent); } } else if (httpMethod == HttpMethod.POST) { // Null is passed instead of the parameters so that they are not appended to the URL. string command = BuildRequestCommand(interfaceName, methodName, methodVersion, null); // Instead, parameters are passed through this container. FormUrlEncodedContent content = BuildRequestContent(parameters); httpResponse = await httpClient.PostAsync(command, content).ConfigureAwait(false); httpResponse.EnsureSuccessStatusCode(); if (httpResponse.Content == null) { httpResponse = new HttpResponseMessage(HttpStatusCode.NoContent); } } var headers = httpResponse.Content?.Headers; // extract http headers that we care about SteamWebResponse <T> steamWebResponse = new SteamWebResponse <T>() { ContentLength = headers?.ContentLength, ContentType = headers?.ContentType?.MediaType, ContentTypeCharSet = headers?.ContentType?.CharSet, Expires = headers?.Expires, LastModified = headers?.LastModified, }; // deserialize the content if we have any as indicated by the response code if (httpResponse.StatusCode != HttpStatusCode.NoContent && httpResponse.Content != null) { string responseContent = await httpResponse.Content.ReadAsStringAsync(); responseContent = CleanupResponseString(responseContent); steamWebResponse.Data = JsonConvert.DeserializeObject <T>(responseContent); } return(steamWebResponse); }