Skip to content

Commit

Permalink
HttpHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
o.nadymov committed Mar 22, 2024
1 parent a440cfa commit 5807333
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
59 changes: 59 additions & 0 deletions src/Spoleto.Common/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using System.Threading.Tasks;
using System.Web;

namespace Spoleto.Common.Helpers
{
/// <summary>
/// The HTTP helper.
/// </summary>
public static class HttpHelper
{
/// <summary>
/// Converts the given object to HTTP query string.
/// </summary>
public static string ToQueryString<T>(T body)
{
var bodyJson = JsonHelper.ToJson(body);
var dictionaryAsObjectValues = JsonHelper.FromJson<Dictionary<string, object>>(bodyJson);

var args = new List<string>();
foreach (var key in dictionaryAsObjectValues.Keys)
{
var jsonValue = (JsonElement)dictionaryAsObjectValues[key];
var objValue = FlattenJsonValue(jsonValue);
if (objValue is string str)
{
args.Add($"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(str)}");
}
else if (objValue is IEnumerable enumerable)
{
foreach (string item in enumerable)
{
args.Add($"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(item)}");
}
}
}

return string.Join("&", args);
}

private static object FlattenJsonValue(JsonElement objValue)
{
return objValue.ValueKind switch
{
JsonValueKind.String => objValue.GetString(),
JsonValueKind.Array => objValue.EnumerateArray().Select(FlattenJsonValue),
_ => objValue.GetRawText()
};
}
}
}
39 changes: 0 additions & 39 deletions src/Spoleto.Common/Helpers/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,5 @@ public static async Task<T> FromJsonStreamAsync<T>(Stream jsonStream)

return body;
}

/// <summary>
/// Converts to a query string.
/// </summary>
public static string ToQueryString<T>(T body)
{
var bodyJson = ToJson(body);
var dictionaryAsObjectValues = FromJson<Dictionary<string, object>>(bodyJson);

var args = new List<string>();
foreach (var key in dictionaryAsObjectValues.Keys)
{
var jsonValue = (JsonElement)dictionaryAsObjectValues[key];
var objValue = FlattenJsonValue(jsonValue);
if (objValue is string str)
{
args.Add($"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(str)}");
}
else if (objValue is IEnumerable enumerable)
{
foreach (string item in enumerable)
{
args.Add($"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(item)}");
}
}
}

return string.Join("&", args);
}

private static object FlattenJsonValue(JsonElement objValue)
{
return objValue.ValueKind switch
{
JsonValueKind.String => objValue.GetString(),
JsonValueKind.Array => objValue.EnumerateArray().Select(FlattenJsonValue),
_ => objValue.GetRawText()
};
}
}
}

0 comments on commit 5807333

Please sign in to comment.