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
|
|
|
|
{
|
2020-05-02 21:48:21 +01:00
|
|
|
public JObject BuildBodyParams()
|
|
|
|
{
|
|
|
|
// Make sure everything is okay before building query params
|
|
|
|
CustomEnsure();
|
|
|
|
|
|
|
|
var bodyProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
|
|
|
.Where(prop => prop.GetCustomAttributes(typeof(BodyParamAttribute), true).Length > 0);
|
|
|
|
|
|
|
|
var obj = new JObject();
|
|
|
|
foreach (var prop in bodyProps)
|
|
|
|
{
|
|
|
|
var attribute = prop.GetCustomAttribute(typeof(BodyParamAttribute)) as BodyParamAttribute;
|
|
|
|
object value = prop.GetValue(this);
|
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
obj[attribute.Key ?? prop.Name] = JToken.FromObject(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-05-01 19:05:28 +01:00
|
|
|
public Dictionary<string, string> BuildQueryParams()
|
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
// Make sure everything is okay before building query params
|
2020-05-02 21:48:21 +01:00
|
|
|
CustomEnsure();
|
2020-05-02 12:04:26 +01:00
|
|
|
|
2020-05-02 13:58:11 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
var queryParams = new Dictionary<string, string>();
|
|
|
|
foreach (var prop in queryProps)
|
|
|
|
{
|
|
|
|
var attribute = prop.GetCustomAttribute(typeof(QueryParamAttribute)) as QueryParamAttribute;
|
2020-05-02 12:04:26 +01:00
|
|
|
object value = prop.GetValue(this);
|
2020-05-01 19:05:28 +01:00
|
|
|
if (value != null)
|
|
|
|
{
|
2020-05-02 21:48:21 +01:00
|
|
|
if (value is List<string>)
|
|
|
|
{
|
2020-05-03 21:34:03 +01:00
|
|
|
var list = value as List<string>;
|
2020-05-02 21:48:21 +01:00
|
|
|
var str = string.Join(",", list);
|
|
|
|
queryParams.Add(attribute.Key ?? prop.Name, str);
|
|
|
|
}
|
2020-05-03 21:34:03 +01:00
|
|
|
else if (value is Enum)
|
|
|
|
{
|
|
|
|
var valueAsEnum = value as Enum;
|
|
|
|
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));
|
|
|
|
}
|
2020-05-02 21:48:21 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
queryParams.Add(attribute.Key ?? prop.Name, value.ToString());
|
|
|
|
}
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:04:26 +01:00
|
|
|
AddCustomQueryParams(queryParams);
|
|
|
|
|
2020-05-01 19:05:28 +01:00
|
|
|
return queryParams;
|
|
|
|
}
|
2020-05-02 12:04:26 +01:00
|
|
|
|
2020-05-02 21:48:21 +01:00
|
|
|
protected virtual void CustomEnsure() { }
|
2020-05-02 12:04:26 +01:00
|
|
|
protected virtual void AddCustomQueryParams(Dictionary<string, string> queryParams) { }
|
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() { }
|
|
|
|
public QueryParamAttribute(string key)
|
|
|
|
{
|
|
|
|
Key = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class BodyParamAttribute : Attribute
|
|
|
|
{
|
2020-05-02 21:48:21 +01:00
|
|
|
public string Key { get; }
|
|
|
|
|
2020-05-01 19:05:28 +01:00
|
|
|
public BodyParamAttribute() { }
|
2020-05-02 21:48:21 +01:00
|
|
|
public BodyParamAttribute(string key)
|
|
|
|
{
|
|
|
|
Key = key;
|
|
|
|
}
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|