Spotify.NET/SpotifyAPI.Web/Models/Request/RequestParams.cs

163 lines
4.9 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
2020-05-01 19:05:28 +01:00
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
2020-05-02 21:48:21 +01:00
using System.Collections;
using Newtonsoft.Json.Linq;
2020-05-01 19:05:28 +01:00
namespace SpotifyAPI.Web
{
public abstract class RequestParams
{
private static readonly ConcurrentDictionary<Type, List<(PropertyInfo, BodyParamAttribute)>> _bodyParamsCache =
new ConcurrentDictionary<Type, List<(PropertyInfo, BodyParamAttribute)>>();
2020-05-02 21:48:21 +01:00
public JObject BuildBodyParams()
{
2020-05-07 12:48:31 +01:00
// Make sure everything is okay before building body params
2020-05-02 21:48:21 +01:00
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
2020-05-02 21:48:21 +01:00
{
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)
2020-05-02 21:48:21 +01:00
{
var attribute = (BodyParamAttribute)prop.GetCustomAttribute(typeof(BodyParamAttribute));
_bodyParamsCache[type].Add((prop, attribute));
AddBodyParam(body, prop, attribute);
2020-05-02 21:48:21 +01:00
}
}
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);
}
2020-05-02 21:48:21 +01:00
}
private static readonly ConcurrentDictionary<Type, List<(PropertyInfo, QueryParamAttribute)>> _queryParamsCache =
new ConcurrentDictionary<Type, List<(PropertyInfo, QueryParamAttribute)>>();
2020-05-01 19:05:28 +01:00
public Dictionary<string, string> BuildQueryParams()
{
// Make sure everything is okay before building query params
2020-05-02 21:48:21 +01:00
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)
2020-05-01 19:05:28 +01:00
.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)
2020-05-01 19:05:28 +01:00
{
if (value is IList<string> list && list.Count > 0)
2020-05-01 19:05:28 +01:00
{
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>();
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);
}
}
2020-05-02 21:48:21 +01:00
}
queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
}
else
{
queryParams.Add(attribute.Key ?? prop.Name, value.ToString());
2020-05-01 19:05:28 +01:00
}
}
}
2020-05-02 21:48:21 +01:00
protected virtual void CustomEnsure() { }
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
2020-05-01 19:05:28 +01:00
}
[AttributeUsage(AttributeTargets.Property)]
2020-05-01 19:05:28 +01:00
public class QueryParamAttribute : Attribute
{
2020-05-02 13:58:11 +01:00
public string Key { get; }
2020-05-01 19:05:28 +01:00
public QueryParamAttribute(string key)
{
Key = key;
}
}
[AttributeUsage(AttributeTargets.Property)]
2020-05-01 19:05:28 +01:00
public class BodyParamAttribute : Attribute
{
2020-05-02 21:48:21 +01:00
public string Key { get; }
public BodyParamAttribute(string key)
{
Key = key;
}
2020-05-01 19:05:28 +01:00
}
}
2020-05-25 17:00:38 +01:00