-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
o.nadymov
committed
Mar 22, 2024
1 parent
a440cfa
commit 5807333
Showing
2 changed files
with
59 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters