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
174 lines (152 loc) · 5.2 KB
/
RequestParams.cs
File metadata and controls
174 lines (152 loc) · 5.2 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
using System.Collections.Concurrent;
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json.Linq;
namespace SpotifyAPI.Web
{
public abstract class RequestParams
{
private static readonly ConcurrentDictionary<Type, List<(PropertyInfo, BodyParamAttribute)>> _bodyParamsCache =
new ConcurrentDictionary<Type, List<(PropertyInfo, BodyParamAttribute)>>();
public JObject BuildBodyParams()
{
// Make sure everything is okay before building body params
CustomEnsure();
var body = new JObject();
var type = GetType();
if (!_bodyParamsCache.IsEmpty && _bodyParamsCache.ContainsKey(type))
{
foreach (var (info, attribute) in _bodyParamsCache[type])
{
AddBodyParam(body, info, attribute);
}
}
else
{
var bodyProps = GetType()
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
_bodyParamsCache[type] = new List<(PropertyInfo, BodyParamAttribute)>();
foreach (var prop in bodyProps)
{
var attribute = (BodyParamAttribute)prop.GetCustomAttribute(typeof(BodyParamAttribute));
_bodyParamsCache[type].Add((prop, attribute));
AddBodyParam(body, prop, attribute);
}
}
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 ConcurrentDictionary<Type, List<(PropertyInfo, QueryParamAttribute)>>();
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.ContainsKey(type))
{
foreach (var (info, attribute) in _queryParamsCache[type])
{
AddQueryParam(queryParams, info, attribute);
}
}
else
{
var queryProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
.Where(prop => prop.GetCustomAttributes(typeof(QueryParamAttribute), true).Length > 0);
_queryParamsCache[type] = new List<(PropertyInfo, QueryParamAttribute)>();
foreach (var prop in queryProps)
{
var attribute = (QueryParamAttribute)prop.GetCustomAttribute(typeof(QueryParamAttribute));
_queryParamsCache[type].Add((prop, attribute));
AddQueryParam(queryParams, prop, attribute);
}
}
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());
}
}
}
protected virtual void CustomEnsure() { }
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
}
[AttributeUsage(AttributeTargets.Property)]
public class QueryParamAttribute : Attribute
{
public string Key { get; }
public QueryParamAttribute(string key)
{
Key = key;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class BodyParamAttribute : Attribute
{
public string Key { get; }
public BodyParamAttribute(string key)
{
Key = key;
}
}
}