mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2025-01-11 14:07:47 +00:00
Added Reflection Cache for RequestParams #451
This commit is contained in:
parent
f105fcbd0e
commit
0ef07bed0b
@ -62,6 +62,7 @@ namespace Example.CLI.PersistentConfig
|
|||||||
var playlists = await spotify.PaginateAll(() => spotify.Playlists.CurrentUsers());
|
var playlists = await spotify.PaginateAll(() => spotify.Playlists.CurrentUsers());
|
||||||
Console.WriteLine($"Total Playlists in your Account: {playlists.Count}");
|
Console.WriteLine($"Total Playlists in your Account: {playlists.Count}");
|
||||||
|
|
||||||
|
_server.Dispose();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,20 @@ namespace SpotifyAPI.Web
|
|||||||
{
|
{
|
||||||
public interface IOAuthClient
|
public interface IOAuthClient
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Requests a new token using client_ids and client_secrets.
|
||||||
|
/// If the token is expired, simply call the funtion again to get a new token
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
Task<CredentialsTokenResponse> RequestToken(ClientCredentialsRequest request);
|
Task<CredentialsTokenResponse> RequestToken(ClientCredentialsRequest request);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Refresh an already received token via Authorization Code Auth
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<AuthorizationCodeRefreshResponse> RequestToken(AuthorizationCodeRefreshRequest request);
|
||||||
|
Task<AuthorizationCodeTokenResponse> RequestToken(AuthorizationCodeTokenRequest request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,69 +10,73 @@ namespace SpotifyAPI.Web
|
|||||||
{
|
{
|
||||||
public abstract class RequestParams
|
public abstract class RequestParams
|
||||||
{
|
{
|
||||||
|
private static readonly ConcurrentDictionary<PropertyInfo, BodyParamAttribute> _bodyParamsCache =
|
||||||
|
new ConcurrentDictionary<PropertyInfo, BodyParamAttribute>();
|
||||||
public JObject BuildBodyParams()
|
public JObject BuildBodyParams()
|
||||||
{
|
{
|
||||||
// Make sure everything is okay before building body params
|
// Make sure everything is okay before building body params
|
||||||
CustomEnsure();
|
CustomEnsure();
|
||||||
|
|
||||||
var bodyProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
var body = new JObject();
|
||||||
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
|
if (!_bodyParamsCache.IsEmpty)
|
||||||
|
|
||||||
var obj = new JObject();
|
|
||||||
foreach (var prop in bodyProps)
|
|
||||||
{
|
{
|
||||||
object value = prop.GetValue(this);
|
foreach (var bodyParam in _bodyParamsCache)
|
||||||
if (value != null && prop.GetCustomAttribute(typeof(BodyParamAttribute)) is BodyParamAttribute attribute)
|
|
||||||
{
|
{
|
||||||
obj[attribute.Key ?? prop.Name] = JToken.FromObject(value);
|
AddBodyParam(body, bodyParam.Key, bodyParam.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
else
|
||||||
|
{
|
||||||
|
var bodyProps = GetType()
|
||||||
|
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||||
|
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
|
||||||
|
|
||||||
|
foreach (var prop in bodyProps)
|
||||||
|
{
|
||||||
|
var attribute = (BodyParamAttribute)prop.GetCustomAttribute(typeof(BodyParamAttribute));
|
||||||
|
_bodyParamsCache[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<PropertyInfo, QueryParamAttribute> _queryParamsCache =
|
||||||
|
new ConcurrentDictionary<PropertyInfo, QueryParamAttribute>();
|
||||||
public Dictionary<string, string> BuildQueryParams()
|
public Dictionary<string, string> BuildQueryParams()
|
||||||
{
|
{
|
||||||
// Make sure everything is okay before building query params
|
// Make sure everything is okay before building query params
|
||||||
CustomEnsure();
|
CustomEnsure();
|
||||||
|
|
||||||
var queryProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
var queryParams = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
if (!_queryParamsCache.IsEmpty)
|
||||||
|
{
|
||||||
|
foreach (var queryParam in _queryParamsCache)
|
||||||
|
{
|
||||||
|
AddQueryParam(queryParams, queryParam.Key, queryParam.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var queryProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||||
.Where(prop => prop.GetCustomAttributes(typeof(QueryParamAttribute), true).Length > 0);
|
.Where(prop => prop.GetCustomAttributes(typeof(QueryParamAttribute), true).Length > 0);
|
||||||
|
|
||||||
var queryParams = new Dictionary<string, string>();
|
foreach (var prop in queryProps)
|
||||||
foreach (var prop in queryProps)
|
|
||||||
{
|
|
||||||
object value = prop.GetValue(this);
|
|
||||||
if (value != null && prop.GetCustomAttribute(typeof(QueryParamAttribute)) is QueryParamAttribute attribute)
|
|
||||||
{
|
{
|
||||||
if (value is IList<string> list && list.Count > 0)
|
var attribute = (QueryParamAttribute)prop.GetCustomAttribute(typeof(QueryParamAttribute));
|
||||||
{
|
_queryParamsCache[prop] = attribute;
|
||||||
var str = string.Join(",", list);
|
AddQueryParam(queryParams, prop, attribute);
|
||||||
queryParams.Add(attribute.Key ?? prop.Name, str);
|
|
||||||
}
|
|
||||||
else if (value is Enum valueAsEnum)
|
|
||||||
{
|
|
||||||
var enumType = valueAsEnum.GetType();
|
|
||||||
var valueList = new List<string>();
|
|
||||||
|
|
||||||
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
|
|
||||||
{
|
|
||||||
if (valueAsEnum.HasFlag(enumVal))
|
|
||||||
{
|
|
||||||
if (enumType
|
|
||||||
.GetMember(enumVal.ToString())[0]
|
|
||||||
.GetCustomAttributes(typeof(StringAttribute))
|
|
||||||
.FirstOrDefault() is StringAttribute stringAttr)
|
|
||||||
{
|
|
||||||
valueList.Add(stringAttr.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
queryParams.Add(attribute.Key ?? prop.Name, value.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,6 +85,43 @@ namespace SpotifyAPI.Web
|
|||||||
return 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 && list.Count > 0)
|
||||||
|
{
|
||||||
|
var str = string.Join(",", list);
|
||||||
|
queryParams.Add(attribute.Key ?? prop.Name, str);
|
||||||
|
}
|
||||||
|
else if (value is Enum valueAsEnum)
|
||||||
|
{
|
||||||
|
var enumType = valueAsEnum.GetType();
|
||||||
|
var valueList = new List<string>();
|
||||||
|
|
||||||
|
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
|
||||||
|
{
|
||||||
|
if (valueAsEnum.HasFlag(enumVal))
|
||||||
|
{
|
||||||
|
if (enumType
|
||||||
|
.GetMember(enumVal.ToString())[0]
|
||||||
|
.GetCustomAttributes(typeof(StringAttribute))
|
||||||
|
.FirstOrDefault() is StringAttribute stringAttr)
|
||||||
|
{
|
||||||
|
valueList.Add(stringAttr.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 CustomEnsure() { }
|
||||||
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
|
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user