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; } /// /// Whether to bypass the proxy server for local addresses. /// 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; } /// /// Whether both and have valid values. /// /// public bool IsValid() { return !string.IsNullOrWhiteSpace(Host) && Port > 0; } /// /// Create a from the host and port number /// /// A URI public Uri GetUri() { UriBuilder uriBuilder = new UriBuilder(Host) { Port = Port }; return uriBuilder.Uri; } /// /// Creates a from the proxy details of this object. /// /// A or null if the proxy details are invalid. 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; } } }