@@ -10,19 +10,22 @@ namespace SpotifyAPI.Web
10
10
{
11
11
public abstract class RequestParams
12
12
{
13
- private static readonly ConcurrentDictionary < PropertyInfo , BodyParamAttribute > _bodyParamsCache =
14
- new ConcurrentDictionary < PropertyInfo , BodyParamAttribute > ( ) ;
13
+ private static readonly ConcurrentDictionary < Type , List < ( PropertyInfo , BodyParamAttribute ) > > _bodyParamsCache =
14
+ new ConcurrentDictionary < Type , List < ( PropertyInfo , BodyParamAttribute ) > > ( ) ;
15
+
15
16
public JObject BuildBodyParams ( )
16
17
{
17
18
// Make sure everything is okay before building body params
18
19
CustomEnsure ( ) ;
19
20
20
21
var body = new JObject ( ) ;
21
- if ( ! _bodyParamsCache . IsEmpty )
22
+ var type = GetType ( ) ;
23
+
24
+ if ( ! _bodyParamsCache . IsEmpty && _bodyParamsCache . ContainsKey ( type ) )
22
25
{
23
- foreach ( var bodyParam in _bodyParamsCache )
26
+ foreach ( var ( info , attribute ) in _bodyParamsCache [ type ] )
24
27
{
25
- AddBodyParam ( body , bodyParam . Key , bodyParam . Value ) ;
28
+ AddBodyParam ( body , info , attribute ) ;
26
29
}
27
30
}
28
31
else
@@ -31,10 +34,11 @@ public JObject BuildBodyParams()
31
34
. GetProperties ( BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Public )
32
35
. Where ( prop => prop . GetCustomAttributes ( typeof ( BodyParamAttribute ) , true ) . Length > 0 ) ;
33
36
37
+ _bodyParamsCache [ type ] = new List < ( PropertyInfo , BodyParamAttribute ) > ( ) ;
34
38
foreach ( var prop in bodyProps )
35
39
{
36
40
var attribute = ( BodyParamAttribute ) prop . GetCustomAttribute ( typeof ( BodyParamAttribute ) ) ;
37
- _bodyParamsCache [ prop ] = attribute ;
41
+ _bodyParamsCache [ type ] . Add ( ( prop , attribute ) ) ;
38
42
AddBodyParam ( body , prop , attribute ) ;
39
43
}
40
44
}
@@ -51,31 +55,34 @@ private void AddBodyParam(JObject body, PropertyInfo prop, BodyParamAttribute at
51
55
}
52
56
}
53
57
54
- private static readonly ConcurrentDictionary < PropertyInfo , QueryParamAttribute > _queryParamsCache =
55
- new ConcurrentDictionary < PropertyInfo , QueryParamAttribute > ( ) ;
58
+ private static readonly ConcurrentDictionary < Type , List < ( PropertyInfo , QueryParamAttribute ) > > _queryParamsCache =
59
+ new ConcurrentDictionary < Type , List < ( PropertyInfo , QueryParamAttribute ) > > ( ) ;
60
+
56
61
public Dictionary < string , string > BuildQueryParams ( )
57
62
{
58
63
// Make sure everything is okay before building query params
59
64
CustomEnsure ( ) ;
60
65
61
66
var queryParams = new Dictionary < string , string > ( ) ;
67
+ var type = GetType ( ) ;
62
68
63
- if ( ! _queryParamsCache . IsEmpty )
69
+ if ( ! _queryParamsCache . IsEmpty && _queryParamsCache . ContainsKey ( type ) )
64
70
{
65
- foreach ( var queryParam in _queryParamsCache )
71
+ foreach ( var ( info , attribute ) in _queryParamsCache [ type ] )
66
72
{
67
- AddQueryParam ( queryParams , queryParam . Key , queryParam . Value ) ;
73
+ AddQueryParam ( queryParams , info , attribute ) ;
68
74
}
69
75
}
70
76
else
71
77
{
72
78
var queryProps = GetType ( ) . GetProperties ( BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Public )
73
79
. Where ( prop => prop . GetCustomAttributes ( typeof ( QueryParamAttribute ) , true ) . Length > 0 ) ;
74
80
81
+ _queryParamsCache [ type ] = new List < ( PropertyInfo , QueryParamAttribute ) > ( ) ;
75
82
foreach ( var prop in queryProps )
76
83
{
77
84
var attribute = ( QueryParamAttribute ) prop . GetCustomAttribute ( typeof ( QueryParamAttribute ) ) ;
78
- _queryParamsCache [ prop ] = attribute ;
85
+ _queryParamsCache [ type ] . Add ( ( prop , attribute ) ) ;
79
86
AddQueryParam ( queryParams , prop , attribute ) ;
80
87
}
81
88
}
@@ -95,6 +102,10 @@ private void AddQueryParam(Dictionary<string, string> queryParams, PropertyInfo
95
102
var str = string . Join ( "," , list ) ;
96
103
queryParams . Add ( attribute . Key ?? prop . Name , str ) ;
97
104
}
105
+ else if ( value is bool valueAsBool )
106
+ {
107
+ queryParams . Add ( attribute . Key ?? prop . Name , valueAsBool ? "true" : "false" ) ;
108
+ }
98
109
else if ( value is Enum valueAsEnum )
99
110
{
100
111
var enumType = valueAsEnum . GetType ( ) ;
0 commit comments