Spotify.NET/SpotifyAPI/ProxyConfig.cs
Alessandro Attard Barbini dd77be8a8a Added support for proxy configuration (#224)
* Create ProxyConfig class + Unit tests

* Use ProxyConfig in SpotifyLocalAPI

* Refactor WebProxy creation in ProxyConfig

* Use ProxyConfig in SpotifyWebAPI

* Add ProxyConfig parameter in GetAlbumArt methods

* Add ProxyConfig to WebAPIFactory

* Add proxy settings to Example app

* Fix ArgumentNullException in GetAlbumArtUrl when not using proxy

* Performed requested changes

- Removed redundant "this".
- Added necessary null checks.
- Added a ProxyConfig property to SpotifyLocalAPIConfig and removed constructor overloads with it in SpotifyLocalAPI and RemoteHandler.

* Updated Example app
2018-03-25 19:39:19 +02:00

77 lines
2.2 KiB
C#

using System;
using System.Net;
namespace SpotifyAPI
{
public class ProxyConfig
{
public string Host { get; set; }
public int Port { get; set; } = 80;
public string Username { get; set; }
public string Password { get; set; }
/// <summary>
/// Whether to bypass the proxy server for local addresses.
/// </summary>
public bool BypassProxyOnLocal { get; set; }
public void Set(ProxyConfig proxyConfig)
{
Host = proxyConfig?.Host;
Port = proxyConfig?.Port ?? 80;
Username = proxyConfig?.Username;
Password = proxyConfig?.Password;
BypassProxyOnLocal = proxyConfig?.BypassProxyOnLocal ?? false;
}
/// <summary>
/// Whether both <see cref="Host"/> and <see cref="Port"/> have valid values.
/// </summary>
/// <returns></returns>
public bool IsValid()
{
return !string.IsNullOrWhiteSpace(Host) && Port > 0;
}
/// <summary>
/// Create a <see cref="Uri"/> from the host and port number
/// </summary>
/// <returns>A URI</returns>
public Uri GetUri()
{
UriBuilder uriBuilder = new UriBuilder(Host)
{
Port = Port
};
return uriBuilder.Uri;
}
/// <summary>
/// Creates a <see cref="WebProxy"/> from the proxy details of this object.
/// </summary>
/// <returns>A <see cref="WebProxy"/> or <code>null</code> if the proxy details are invalid.</returns>
public WebProxy CreateWebProxy()
{
if (!IsValid())
return null;
WebProxy proxy = new WebProxy
{
Address = GetUri(),
UseDefaultCredentials = true,
BypassProxyOnLocal = BypassProxyOnLocal
};
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
proxy.UseDefaultCredentials = false;
proxy.Credentials = new NetworkCredential(Username, Password);
}
return proxy;
}
}
}