Skip to content

Commit dd77be8

Browse files
aleabJohnnyCrazy
authored andcommitted
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
1 parent 023520d commit dd77be8

15 files changed

+738
-149
lines changed

SpotifyAPI.Example/LocalControl.Designer.cs

+247-104
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpotifyAPI.Example/LocalControl.cs

+40-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ namespace SpotifyAPI.Example
1010
{
1111
public partial class LocalControl : UserControl
1212
{
13-
private readonly SpotifyLocalAPI _spotify;
13+
private readonly SpotifyLocalAPIConfig _config;
14+
private SpotifyLocalAPI _spotify;
1415
private Track _currentTrack;
1516

1617
public LocalControl()
1718
{
1819
InitializeComponent();
20+
21+
_config = new SpotifyLocalAPIConfig
22+
{
23+
ProxyConfig = new ProxyConfig()
24+
};
1925

20-
_spotify = new SpotifyLocalAPI();
26+
_spotify = new SpotifyLocalAPI(_config);
2127
_spotify.OnPlayStateChange += _spotify_OnPlayStateChange;
2228
_spotify.OnTrackChange += _spotify_OnTrackChange;
2329
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
@@ -99,8 +105,8 @@ public async void UpdateTrack(Track track)
99105

100106
trackInfoBox.Text = $@"Track Info - {uri?.Id}";
101107

102-
bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640) : null;
103-
smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160) : null;
108+
bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640, _config.ProxyConfig) : null;
109+
smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160, _config.ProxyConfig) : null;
104110
}
105111

106112
public void UpdatePlayingStatus(bool playing)
@@ -113,6 +119,36 @@ public void RefreshVolumeMixerVolume()
113119
volumeMixerLabel.Text = _spotify.GetSpotifyVolume().ToString(CultureInfo.InvariantCulture);
114120
}
115121

122+
private void applyProxyBtn_Click(object sender, EventArgs e)
123+
{
124+
_config.ProxyConfig.Host = proxyHostTextBox.Text;
125+
_config.ProxyConfig.Port = (int)proxyPortUpDown.Value;
126+
_config.ProxyConfig.Username = proxyUsernameTextBox.Text;
127+
_config.ProxyConfig.Password = proxyPasswordTextBox.Text;
128+
129+
bool connected = _spotify.ListenForEvents;
130+
if (connected)
131+
{
132+
// Reconnect using new proxy
133+
_spotify.ListenForEvents = false;
134+
_spotify.OnPlayStateChange -= _spotify_OnPlayStateChange;
135+
_spotify.OnTrackChange -= _spotify_OnTrackChange;
136+
_spotify.OnTrackTimeChange -= _spotify_OnTrackTimeChange;
137+
_spotify.OnVolumeChange -= _spotify_OnVolumeChange;
138+
139+
_spotify.Dispose();
140+
141+
_spotify = new SpotifyLocalAPI(_config);
142+
_spotify.OnPlayStateChange += _spotify_OnPlayStateChange;
143+
_spotify.OnTrackChange += _spotify_OnTrackChange;
144+
_spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange;
145+
_spotify.OnVolumeChange += _spotify_OnVolumeChange;
146+
147+
connectBtn.Text = @"Reconnecting...";
148+
Connect();
149+
}
150+
}
151+
116152
private void _spotify_OnVolumeChange(object sender, VolumeChangeEventArgs e)
117153
{
118154
if (InvokeRequired)

SpotifyAPI.Example/WebControl.Designer.cs

+151-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpotifyAPI.Example/WebControl.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace SpotifyAPI.Example
1515
{
1616
public partial class WebControl : UserControl
1717
{
18+
private readonly ProxyConfig _proxyConfig;
1819
private SpotifyWebAPI _spotify;
1920

2021
private PrivateProfile _profile;
@@ -25,8 +26,9 @@ public WebControl()
2526
{
2627
InitializeComponent();
2728

29+
_proxyConfig = new ProxyConfig();
30+
2831
_savedTracks = new List<FullTrack>();
29-
3032
}
3133

3234
private async void InitialSetup()
@@ -109,7 +111,8 @@ private async void RunAuthentication()
109111
"26d287105e31491889f3cd293d85bfea",
110112
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
111113
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
112-
Scope.UserReadRecentlyPlayed | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState);
114+
Scope.UserReadRecentlyPlayed | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState,
115+
_proxyConfig);
113116

114117
try
115118
{
@@ -125,5 +128,13 @@ private async void RunAuthentication()
125128

126129
InitialSetup();
127130
}
131+
132+
private void applyProxyBtn_Click(object sender, EventArgs e)
133+
{
134+
_proxyConfig.Host = proxyHostTextBox.Text;
135+
_proxyConfig.Port = (int)proxyPortUpDown.Value;
136+
_proxyConfig.Username = proxyUsernameTextBox.Text;
137+
_proxyConfig.Password = proxyPasswordTextBox.Text;
138+
}
128139
}
129140
}

0 commit comments

Comments
 (0)