-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryParametersHelper.cs
More file actions
53 lines (42 loc) · 1.41 KB
/
Copy pathQueryParametersHelper.cs
File metadata and controls
53 lines (42 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace HttpsRichardy.Federation.Sdk.Helpers;
public static class QueryParametersParser
{
public static string ToQueryString<TParameters>(TParameters instance)
{
if (instance is null) return string.Empty;
var properties = typeof(TParameters).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var stringBuilder = new StringBuilder();
bool first = true;
foreach (var property in properties)
{
var value = property.GetValue(instance);
if (value is null)
continue;
string name = ToCamelCase(property.Name);
string stringValue = value switch
{
bool builder => builder
.ToString()
.ToLowerInvariant(),
_ => value?.ToString() ?? string.Empty
};
stringValue = Uri.EscapeDataString(stringValue);
if (!first)
{
stringBuilder.Append('&');
}
else
{
first = false;
}
stringBuilder.Append($"{name}={stringValue}");
}
return stringBuilder.ToString();
}
private static string ToCamelCase(string value)
{
if (string.IsNullOrEmpty(value) || char.IsLower(value[0]))
return value;
return char.ToLowerInvariant(value[0]) + value[1..];
}
}