forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestParams.cs
More file actions
181 lines (159 loc) · 5.29 KB
/
RequestParams.cs
File metadata and controls
181 lines (159 loc) · 5.29 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
namespace SpotifyAPI.Web
{
public abstract class RequestParams
{
private static readonly ConcurrentDictionary<Type, List<(PropertyInfo, BodyParamAttribute)>> _bodyParamsCache =
new();
public JObject BuildBodyParams()
{
// Make sure everything is okay before building body params
CustomEnsure();
var body = new JObject();
var type = GetType();
if (!_bodyParamsCache.IsEmpty && _bodyParamsCache.TryGetValue(type, out var bodyParamsCached))
{
foreach (var (info, attribute) in bodyParamsCached)
{
AddBodyParam(body, info, attribute);
}
}
else
{
var bodyProps = GetType()
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
var cachedParams = new List<(PropertyInfo, BodyParamAttribute)>();
foreach (var prop in bodyProps)
{
var attribute = prop.GetCustomAttribute<BodyParamAttribute>();
if (attribute != null)
{
cachedParams.Add((prop, attribute));
AddBodyParam(body, prop, attribute);
}
}
_bodyParamsCache[type] = cachedParams;
}
return body;
}
private void AddBodyParam(JObject body, PropertyInfo prop, BodyParamAttribute attribute)
{
object? value = prop.GetValue(this);
if (value != null)
{
body[attribute.Key ?? prop.Name] = JToken.FromObject(value);
}
}
private static readonly ConcurrentDictionary<Type, List<(PropertyInfo, QueryParamAttribute)>> _queryParamsCache =
new();
public Dictionary<string, string> BuildQueryParams()
{
// Make sure everything is okay before building query params
CustomEnsure();
var queryParams = new Dictionary<string, string>();
var type = GetType();
if (!_queryParamsCache.IsEmpty && _queryParamsCache.TryGetValue(type, out var queryParamsCached))
{
foreach (var (info, attribute) in queryParamsCached)
{
AddQueryParam(queryParams, info, attribute);
}
}
else
{
var queryProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(prop => prop.GetCustomAttributes(typeof(QueryParamAttribute), true).Length > 0);
var cachedParams = new List<(PropertyInfo, QueryParamAttribute)>();
foreach (var prop in queryProps)
{
var attribute = prop.GetCustomAttribute<QueryParamAttribute>();
if (attribute != null)
{
cachedParams.Add((prop, attribute));
AddQueryParam(queryParams, prop, attribute);
}
}
_queryParamsCache[type] = cachedParams;
}
AddCustomQueryParams(queryParams);
return queryParams;
}
private void AddQueryParam(Dictionary<string, string> queryParams, PropertyInfo prop, QueryParamAttribute attribute)
{
object? value = prop.GetValue(this);
if (value != null)
{
if (value is IList<string> list)
{
if (list.Count > 0)
{
var str = string.Join(",", list);
queryParams.Add(attribute.Key ?? prop.Name, str);
}
}
else if (value is bool valueAsBool)
{
queryParams.Add(attribute.Key ?? prop.Name, valueAsBool ? "true" : "false");
}
else if (value is Enum valueAsEnum)
{
var enumType = valueAsEnum.GetType();
var valueList = new List<string>();
if (enumType.IsDefined(typeof(FlagsAttribute), false))
{
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
{
if (valueAsEnum.HasFlag(enumVal))
{
if (StringAttribute.GetValue(enumType, enumVal, out var stringVal))
{
// .netstandard2.0 requires !
valueList.Add(stringVal!);
}
}
}
}
else
{
if (StringAttribute.GetValue(enumType, valueAsEnum, out var stringVal))
{
// .netstandard2.0 requires !
valueList.Add(stringVal!);
}
}
queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
}
else
{
queryParams.Add(attribute.Key ?? prop.Name, value.ToString() ?? throw new APIException("ToString returned null for query parameter"));
}
}
}
protected virtual void CustomEnsure() { }
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class QueryParamAttribute : Attribute
{
public string Key { get; }
public QueryParamAttribute(string key)
{
Key = key;
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class BodyParamAttribute : Attribute
{
public string Key { get; }
public BodyParamAttribute(string key)
{
Key = key;
}
}
}