/// <summary> /// Adds a parameter to the request. /// </summary> /// <param name="param"><see cref="SteamRequestParameter"/> to add to the request.</param> /// <returns>This request</returns> public ISteamRequest AddParameter( SteamRequestParameter param ) { // If a parameter with this name and type already exists, delete it so we can add the newer version // (There's a tradeoff here, going from O(1) to O(n), but it saves headaches when not getting expected behavior // because the Steam endpoint is evaluating two of the same parameter) Parameters.RemoveAll( p => p.Name == param.Name && p.Type == param.Type ); Parameters.Add( param ); return this; }
/// <summary> /// Utility method for POST requests. We must produce an object array such that it can be serialized (non-Raw POST style requests). /// </summary> /// <param name="request">Request to be evaluated.</param> /// <param name="body">Current parameter intended for addition to the body of the request.</param> /// <returns>Content which can be sent over the HTTP Request, including all parameters (both body and GetOrPost).</returns> private string SerializeBodyWithParameters( ISteamRequest request, SteamRequestParameter body, PostDataFormat format ) { Dictionary<string, object> output = new Dictionary<string, object>(); IEnumerable<SteamRequestParameter> parameters = request.Parameters.Where( p => p.Type == ParameterType.GetOrPost ); foreach( var p in parameters ) { output.Add( p.Name, p.Value ); } if( format == PostDataFormat.Json ) { if( body != null && output.Count < 1 ) return JsonConvert.SerializeObject( body.Value ); else if( body != null ) { output.Add( body.Name, body.Value ); return JsonConvert.SerializeObject( output ); } else { return JsonConvert.SerializeObject( output ); } } else { var payload = new StringBuilder(); foreach( SteamRequestParameter p in parameters ) { if( payload.Length > 1 ) payload.Append( "&" ); string appendValue = ( p.IsUrlEncoded ) ? p.Value.ToString() : Uri.EscapeDataString( p.Value.ToString() ); payload.AppendFormat( "{0}={1}", Uri.EscapeDataString( p.Name ), appendValue ); } if( body != null && body.Value != null ) payload.AppendFormat( "{0}={1}", body.Name, body.Value.ToString() ); return payload.ToString(); } }