Spotify.NET/SpotifyAPI.Web/Models/Request/LoginRequest.cs
Jonas Dellinger cb2e0cccc8 run formatter
2022-11-27 13:29:35 +01:00

68 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;
namespace SpotifyAPI.Web
{
public class LoginRequest
{
public LoginRequest(Uri redirectUri, string clientId, ResponseType responseType)
{
Ensure.ArgumentNotNull(redirectUri, nameof(redirectUri));
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
RedirectUri = redirectUri;
ClientId = clientId;
ResponseTypeParam = responseType;
}
public Uri RedirectUri { get; }
public ResponseType ResponseTypeParam { get; }
public string ClientId { get; }
public string? State { get; set; }
public ICollection<string>? Scope { get; set; }
public bool? ShowDialog { get; set; }
public string? CodeChallengeMethod { get; set; }
public string? CodeChallenge { get; set; }
public Uri ToUri()
{
var builder = new StringBuilder(SpotifyUrls.Authorize.ToString());
builder.Append($"?client_id={ClientId}");
builder.Append($"&response_type={ResponseTypeParam.ToString().ToLower(CultureInfo.InvariantCulture)}");
builder.Append($"&redirect_uri={HttpUtility.UrlEncode(RedirectUri.ToString())}");
if (!string.IsNullOrEmpty(State))
{
builder.Append($"&state={HttpUtility.UrlEncode(State)}");
}
if (Scope != null)
{
builder.Append($"&scope={HttpUtility.UrlEncode(string.Join(" ", Scope))}");
}
if (ShowDialog != null)
{
builder.Append($"&show_dialog={ShowDialog.Value}");
}
if (CodeChallenge != null)
{
builder.Append($"&code_challenge={CodeChallenge}");
}
if (CodeChallengeMethod != null)
{
builder.Append($"&code_challenge_method={CodeChallengeMethod}");
}
return new Uri(builder.ToString());
}
public enum ResponseType
{
Code,
Token
}
}
}