From 8dde2362561ced8d331186106005132f608daca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Tue, 19 Jul 2016 00:04:27 +0200 Subject: [PATCH 01/10] Stability improvement: Processing in separate threat may cause racing condition and timeouts in browser. By putting processing in original threat we get a more sequential processing and more stability. --- SpotifyAPI/Web/SimpleHttpServer.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/SpotifyAPI/Web/SimpleHttpServer.cs b/SpotifyAPI/Web/SimpleHttpServer.cs index ffc297b4..1f417b6c 100644 --- a/SpotifyAPI/Web/SimpleHttpServer.cs +++ b/SpotifyAPI/Web/SimpleHttpServer.cs @@ -4,7 +4,9 @@ using System.Collections.Specialized; using System.IO; using System.Net; using System.Net.Sockets; +using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Web; // offered to the public domain for any use with no restriction @@ -57,10 +59,8 @@ namespace SpotifyAPI.Web return data; } - public void Process(object tcpClient) + public void Process(TcpClient socket) { - TcpClient socket = tcpClient as TcpClient; - // we can't use a StreamReader for input, because it buffers up extra data on us inside it's // "processed" view of the world, and we want the data raw after the headers _inputStream = new BufferedStream(socket.GetStream()); @@ -80,7 +80,7 @@ namespace SpotifyAPI.Web HandlePostRequest(); } } - catch + catch (Exception ex) { WriteFailure(); } @@ -224,9 +224,7 @@ namespace SpotifyAPI.Web while (IsActive) { TcpClient s = _listener.AcceptTcpClient(); - Thread thread = new Thread(processor.Process); - thread.Start(s); - Thread.Sleep(1); + processor.Process(s); } } } From 0130c96c54327369db50c31a44898d0d14842b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Tue, 19 Jul 2016 21:14:42 +0200 Subject: [PATCH 02/10] - Changed Listener handling to asynchronious handling + New: AuthenticationFactory for awaitatble getting of fully initalized SpotifyWebApi - Changed demo-app to usage of AuthenticationFactory --- .gitignore | 115 ++--------------------------------------------------- 1 file changed, 4 insertions(+), 111 deletions(-) diff --git a/.gitignore b/.gitignore index b36b3008..23d1dcb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,112 +1,5 @@ -# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) -[Bb]in/ -[Oo]bj/ +################################################################################ +# Diese .gitignore-Datei wurde von Microsoft(R) Visual Studio automatisch erstellt. +################################################################################ -# mstest test results -TestResults - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results -[Dd]ebug/ -[Rr]elease/ -x64/ -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.log -*.vspscc -*.vssscc -.builds - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper* - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish - -# Publish Web Output -*.Publish.xml - -# NuGet Packages Directory -packages - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -[Bb]in -[Oo]bj -sql -TestResults -[Tt]est[Rr]esult* -*.Cache -ClientBin -[Ss]tyle[Cc]op.* -~$* -*.dbmdl -Generated_Code #added for RIA/Silverlight projects - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML - -# NuGet -/packages/ -*.nupkg +/SpotifyAPI.Example/bin/Debug From d21e84476c0d8689fcaa872cd5c7d4c5346678c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Tue, 19 Jul 2016 21:15:22 +0200 Subject: [PATCH 03/10] AuthenticationFactory --- SpotifyAPI/SpotifyAPI.csproj | 1 + SpotifyAPI/Web/Auth/AuthenticationFactory.cs | 85 ++++++++++++++++++++ SpotifyAPI/Web/SimpleHttpServer.cs | 16 +++- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 SpotifyAPI/Web/Auth/AuthenticationFactory.cs diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index 3f025e4b..e414942c 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -58,6 +58,7 @@ + diff --git a/SpotifyAPI/Web/Auth/AuthenticationFactory.cs b/SpotifyAPI/Web/Auth/AuthenticationFactory.cs new file mode 100644 index 00000000..2653ef11 --- /dev/null +++ b/SpotifyAPI/Web/Auth/AuthenticationFactory.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using SpotifyAPI.Web.Enums; +using SpotifyAPI.Web.Models; + +namespace SpotifyAPI.Web.Auth +{ + public class AuthenticationFactory + { + private readonly string m_RedirectUrl; + private readonly int m_ListeningPort; + private readonly string m_ClientId; + private readonly TimeSpan m_Timeout; + private Scope m_Scope; + + public AuthenticationFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) + { + m_RedirectUrl = redirectUrl; + m_ListeningPort = listeningPort; + m_ClientId = clientId; + m_Scope = scope; + m_Timeout = timeout; + } + + public Task GetWebApi() + { + var authentication = new ImplicitGrantAuth + { + RedirectUri = $"{m_RedirectUrl}:{m_ListeningPort}", + ClientId = m_ClientId, + Scope = m_Scope, + State = "XSS" + }; + + AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false); + SpotifyWebAPI spotifyWebApi = null; + authentication.OnResponseReceivedEvent += (token, state) => + { + spotifyWebApi = HandleSpotifyResponse(state, token); + authenticationWaitFlag.Set(); + }; + + authentication.StartHttpServer(m_ListeningPort); + + authentication.DoAuth(); + + authenticationWaitFlag.WaitOne(m_Timeout); + if (spotifyWebApi == null) + throw new TimeoutException($"No valid response received for the last {m_Timeout.TotalSeconds} seconds"); + + authentication.StopHttpServer(); + + return Task.FromResult(spotifyWebApi); + } + + private static SpotifyWebAPI HandleSpotifyResponse(string state, Token token) + { + if (state != "XSS") + throw new SpotifyWebApiException($"Wrong state '{state}' received."); + + if (token.Error != null) + throw new SpotifyWebApiException($"Error: {token.Error}"); + + var spotifyWebApi = new SpotifyWebAPI + { + UseAuth = true, + AccessToken = token.AccessToken, + TokenType = token.TokenType + }; + + return spotifyWebApi; + } + } + + [Serializable] + public class SpotifyWebApiException : Exception + { + public SpotifyWebApiException(string message) : base(message) + { } + } +} diff --git a/SpotifyAPI/Web/SimpleHttpServer.cs b/SpotifyAPI/Web/SimpleHttpServer.cs index 1f417b6c..369d7629 100644 --- a/SpotifyAPI/Web/SimpleHttpServer.cs +++ b/SpotifyAPI/Web/SimpleHttpServer.cs @@ -223,8 +223,20 @@ namespace SpotifyAPI.Web { while (IsActive) { - TcpClient s = _listener.AcceptTcpClient(); - processor.Process(s); + _listener.BeginAcceptTcpClient(ar => + { + try + { + TcpListener listener = (TcpListener)ar.AsyncState; + var tcpCLient = listener.EndAcceptTcpClient(ar); + processor.Process(tcpCLient); + + } + catch (ObjectDisposedException) + { + // Ignore + } + }, _listener); } } } From f0290fa03746843fb3681dc0c803c3bcca0683b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Tue, 19 Jul 2016 21:16:03 +0200 Subject: [PATCH 04/10] WebControl with new AuthenticationFactory --- SpotifyAPI.Example/WebControl.cs | 40 +++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/SpotifyAPI.Example/WebControl.cs b/SpotifyAPI.Example/WebControl.cs index 1c0f3a39..d871475a 100644 --- a/SpotifyAPI.Example/WebControl.cs +++ b/SpotifyAPI.Example/WebControl.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Threading.Tasks; using System.Windows.Forms; using Image = System.Drawing.Image; @@ -26,14 +27,7 @@ namespace SpotifyAPI.Example InitializeComponent(); _savedTracks = new List(); - _auth = new ImplicitGrantAuth - { - RedirectUri = "http://localhost:8000", - ClientId = "26d287105e31491889f3cd293d85bfea", - Scope = Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead, - State = "XSS" - }; - _auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent; + } private void _auth_OnResponseReceivedEvent(Token token, string state) @@ -57,7 +51,7 @@ namespace SpotifyAPI.Example AccessToken = token.AccessToken, TokenType = token.TokenType }; - InitialSetup(); + } private async void InitialSetup() @@ -129,8 +123,32 @@ namespace SpotifyAPI.Example private void authButton_Click(object sender, EventArgs e) { - _auth.StartHttpServer(8000); - _auth.DoAuth(); + Task.Run(() => RunAuthentication()); + } + + private async void RunAuthentication() + { + AuthenticationFactory authenticationFactory = new AuthenticationFactory( + "http://localhost", + 8000, + "26d287105e31491889f3cd293d85bfea", + Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | + Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead, + TimeSpan.FromSeconds(20)); + + try + { + _spotify = await authenticationFactory.GetWebApi(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + + if (_spotify == null) + return; + + InitialSetup(); } } } \ No newline at end of file From 234a522f8fb63f97b024c81f4627497de09faa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Holzer?= Date: Wed, 20 Jul 2016 09:56:05 +0200 Subject: [PATCH 05/10] Cleaning up after timeout Proper Re-Use is now possible --- SpotifyAPI/Web/Auth/AuthenticationFactory.cs | 19 +++++---- SpotifyAPI/Web/SimpleHttpServer.cs | 43 +++++++++++--------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/SpotifyAPI/Web/Auth/AuthenticationFactory.cs b/SpotifyAPI/Web/Auth/AuthenticationFactory.cs index 2653ef11..a8a52a2c 100644 --- a/SpotifyAPI/Web/Auth/AuthenticationFactory.cs +++ b/SpotifyAPI/Web/Auth/AuthenticationFactory.cs @@ -44,15 +44,20 @@ namespace SpotifyAPI.Web.Auth authenticationWaitFlag.Set(); }; - authentication.StartHttpServer(m_ListeningPort); + try + { + authentication.StartHttpServer(m_ListeningPort); - authentication.DoAuth(); + authentication.DoAuth(); - authenticationWaitFlag.WaitOne(m_Timeout); - if (spotifyWebApi == null) - throw new TimeoutException($"No valid response received for the last {m_Timeout.TotalSeconds} seconds"); - - authentication.StopHttpServer(); + authenticationWaitFlag.WaitOne(m_Timeout); + if (spotifyWebApi == null) + throw new TimeoutException($"No valid response received for the last {m_Timeout.TotalSeconds} seconds"); + } + finally + { + authentication.StopHttpServer(); + } return Task.FromResult(spotifyWebApi); } diff --git a/SpotifyAPI/Web/SimpleHttpServer.cs b/SpotifyAPI/Web/SimpleHttpServer.cs index 369d7629..26de79b2 100644 --- a/SpotifyAPI/Web/SimpleHttpServer.cs +++ b/SpotifyAPI/Web/SimpleHttpServer.cs @@ -87,7 +87,6 @@ namespace SpotifyAPI.Web OutputStream.Flush(); _inputStream = null; OutputStream = null; - socket.Close(); } public void ParseRequest() @@ -219,26 +218,8 @@ namespace SpotifyAPI.Web _listener = new TcpListener(IPAddress.Any, Port); _listener.Start(); - using (HttpProcessor processor = new HttpProcessor(this)) - { - while (IsActive) - { - _listener.BeginAcceptTcpClient(ar => - { - try - { - TcpListener listener = (TcpListener)ar.AsyncState; - var tcpCLient = listener.EndAcceptTcpClient(ar); - processor.Process(tcpCLient); + _listener.BeginAcceptTcpClient(AcceptTcpConnection, _listener); - } - catch (ObjectDisposedException) - { - // Ignore - } - }, _listener); - } - } } catch (SocketException e) { @@ -247,6 +228,28 @@ namespace SpotifyAPI.Web } } + private void AcceptTcpConnection(IAsyncResult ar) + { + TcpListener listener = (TcpListener)ar.AsyncState; + try + { + var tcpCLient = listener.EndAcceptTcpClient(ar); + using (HttpProcessor processor = new HttpProcessor(this)) + { + processor.Process(tcpCLient); + } + } + catch (ObjectDisposedException) + { + // Ignore + } + + if (!IsActive) + return; + //listener.Start(); + listener.BeginAcceptTcpClient(AcceptTcpConnection, listener); + } + public abstract void HandleGetRequest(HttpProcessor p); public abstract void HandlePostRequest(HttpProcessor p, StreamReader inputData); From 08137421ea344f1875fbfc9efb45c1e0e145f432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Holzer?= Date: Fri, 22 Jul 2016 21:38:58 +0200 Subject: [PATCH 06/10] Timer should be disabled and events detached if no longer needed. So IDisposable is introduced which handles the above... --- SpotifyAPI/Local/SpotifyLocalAPI.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 701bc8c7..2d639657 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -8,7 +8,7 @@ using System.Timers; namespace SpotifyAPI.Local { - public class SpotifyLocalAPI + public class SpotifyLocalAPI : IDisposable { [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); @@ -49,7 +49,7 @@ namespace SpotifyAPI.Local private const int KeyeventfKeyup = 0x2; private readonly RemoteHandler _rh; - private readonly Timer _eventTimer; + private Timer _eventTimer; private StatusResponse _eventStatusResponse; public event EventHandler OnTrackChange; @@ -64,9 +64,21 @@ namespace SpotifyAPI.Local { _rh = new RemoteHandler(); + AttachTimer(50); + } + + public SpotifyLocalAPI(int timerIntervall) + { + _rh = new RemoteHandler(); + + AttachTimer(timerIntervall); + } + + private void AttachTimer(int intervall) + { _eventTimer = new Timer { - Interval = 50, + Interval = intervall, AutoReset = false, Enabled = false }; @@ -333,5 +345,13 @@ namespace SpotifyAPI.Local Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"spotify\spotifywebhelper.exe")); } } + + public void Dispose() + { + if (_eventTimer == null) + return; + _eventTimer.Enabled = false; + _eventTimer.Elapsed -= ElapsedTick; + } } } \ No newline at end of file From 961f25a487097303ff0e02be0527b623594878c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Sat, 23 Jul 2016 19:53:02 +0200 Subject: [PATCH 07/10] - Changes from manual reading of header content to Linq-approach (pro: no more chance of deadlocks in StreamReadLine) - Flush output stream and close it --- SpotifyAPI/Web/SimpleHttpServer.cs | 50 +++++++++++++----------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/SpotifyAPI/Web/SimpleHttpServer.cs b/SpotifyAPI/Web/SimpleHttpServer.cs index 26de79b2..571fb771 100644 --- a/SpotifyAPI/Web/SimpleHttpServer.cs +++ b/SpotifyAPI/Web/SimpleHttpServer.cs @@ -1,7 +1,9 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Collections.Specialized; using System.IO; +using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; @@ -35,28 +37,13 @@ namespace SpotifyAPI.Web _srv = srv; } - private string StreamReadLine(Stream inputStream) + private string[] GetIncomingRequest(Stream inputStream) { - string data = ""; - while (_isActive) - { - var nextChar = inputStream.ReadByte(); - if (nextChar == '\n') - { - break; - } - if (nextChar == '\r') - { - continue; - } - if (nextChar == -1) - { - Thread.Sleep(1); - continue; - } - data += Convert.ToChar(nextChar); - } - return data; + var buffer = new byte[4096]; + var read = inputStream.Read(buffer, 0, buffer.Length); + + var inputData = Encoding.ASCII.GetString(buffer.Take(read).ToArray()); + return inputData.Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToArray(); } public void Process(TcpClient socket) @@ -69,8 +56,11 @@ namespace SpotifyAPI.Web OutputStream = new StreamWriter(new BufferedStream(socket.GetStream())); try { - ParseRequest(); - ReadHeaders(); + var requestLines = GetIncomingRequest(_inputStream); + + ParseRequest(requestLines.First()); + ReadHeaders(requestLines.Skip(1)); + if (HttpMethod.Equals("GET")) { HandleGetRequest(); @@ -89,9 +79,8 @@ namespace SpotifyAPI.Web OutputStream = null; } - public void ParseRequest() + public void ParseRequest(string request) { - string request = StreamReadLine(_inputStream); string[] tokens = request.Split(' '); if (tokens.Length < 2) { @@ -101,10 +90,9 @@ namespace SpotifyAPI.Web HttpUrl = tokens[1]; } - public void ReadHeaders() + public void ReadHeaders(IEnumerable requestLines) { - string line; - while ((line = StreamReadLine(_inputStream)) != null) + foreach(var line in requestLines) { if (string.IsNullOrEmpty(line)) { @@ -328,6 +316,8 @@ namespace SpotifyAPI.Web "window.location = hashes" + "" + "

Spotify Auth successful!
Please copy the URL and paste it into the application

"); + p.OutputStream.Flush(); + p.OutputStream.Close(); return; } string url = p.HttpUrl; @@ -358,8 +348,12 @@ namespace SpotifyAPI.Web State = col.Get(3) }); }); + p.OutputStream.Flush(); + p.OutputStream.Close(); } } + + t.Start(); } From f13ac736710260a7cfb49ddf86295d2fdedc59ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Sun, 24 Jul 2016 23:54:13 +0200 Subject: [PATCH 08/10] restore original .gitignore --- .gitignore | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 23d1dcb2..ece5322e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,112 @@ -################################################################################ -# Diese .gitignore-Datei wurde von Microsoft(R) Visual Studio automatisch erstellt. -################################################################################ +# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) +[Bb]in/ +[Oo]bj/ -/SpotifyAPI.Example/bin/Debug +# mstest test results +TestResults + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.log +*.vspscc +*.vssscc +.builds + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper* + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Publish Web Output +*.Publish.xml + +# NuGet Packages Directory +packages + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +[Bb]in +[Oo]bj +sql +TestResults +[Tt]est[Rr]esult* +*.Cache +ClientBin +[Ss]tyle[Cc]op.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + +# NuGet +/packages/ +*.nupkg From 1699519c95f76114af36aaa0f29c8b98e863b73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Sun, 24 Jul 2016 23:58:45 +0200 Subject: [PATCH 09/10] Renamed AuthenticationFactory to WebApiFactory (better description of what the class does...) --- SpotifyAPI.Example/WebControl.cs | 4 ++-- SpotifyAPI/SpotifyAPI.csproj | 2 +- .../Auth/{AuthenticationFactory.cs => WebApiFactory.cs} | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) rename SpotifyAPI/Web/Auth/{AuthenticationFactory.cs => WebApiFactory.cs} (91%) diff --git a/SpotifyAPI.Example/WebControl.cs b/SpotifyAPI.Example/WebControl.cs index d871475a..fffe0af5 100644 --- a/SpotifyAPI.Example/WebControl.cs +++ b/SpotifyAPI.Example/WebControl.cs @@ -128,7 +128,7 @@ namespace SpotifyAPI.Example private async void RunAuthentication() { - AuthenticationFactory authenticationFactory = new AuthenticationFactory( + WebApiFactory webApiFactory = new WebApiFactory( "http://localhost", 8000, "26d287105e31491889f3cd293d85bfea", @@ -138,7 +138,7 @@ namespace SpotifyAPI.Example try { - _spotify = await authenticationFactory.GetWebApi(); + _spotify = await webApiFactory.GetWebApi(); } catch (Exception ex) { diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index e414942c..9c5f2c17 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -58,7 +58,7 @@
- + diff --git a/SpotifyAPI/Web/Auth/AuthenticationFactory.cs b/SpotifyAPI/Web/Auth/WebApiFactory.cs similarity index 91% rename from SpotifyAPI/Web/Auth/AuthenticationFactory.cs rename to SpotifyAPI/Web/Auth/WebApiFactory.cs index a8a52a2c..b76ab436 100644 --- a/SpotifyAPI/Web/Auth/AuthenticationFactory.cs +++ b/SpotifyAPI/Web/Auth/WebApiFactory.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using SpotifyAPI.Web.Enums; @@ -9,7 +6,7 @@ using SpotifyAPI.Web.Models; namespace SpotifyAPI.Web.Auth { - public class AuthenticationFactory + public class WebApiFactory { private readonly string m_RedirectUrl; private readonly int m_ListeningPort; @@ -17,7 +14,7 @@ namespace SpotifyAPI.Web.Auth private readonly TimeSpan m_Timeout; private Scope m_Scope; - public AuthenticationFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) + public WebApiFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) { m_RedirectUrl = redirectUrl; m_ListeningPort = listeningPort; From 4699246068aab1c791df1a8a5f8016665c95658b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen?= Date: Sat, 30 Jul 2016 00:19:57 +0200 Subject: [PATCH 10/10] Adapted code style --- SpotifyAPI.Example/WebControl.cs | 2 +- SpotifyAPI/Local/SpotifyLocalAPI.cs | 10 +------- SpotifyAPI/SpotifyAPI.csproj | 2 +- SpotifyAPI/Web/Auth/WebApiFactory.cs | 36 ++++++++++++++-------------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/SpotifyAPI.Example/WebControl.cs b/SpotifyAPI.Example/WebControl.cs index fffe0af5..c7af858f 100644 --- a/SpotifyAPI.Example/WebControl.cs +++ b/SpotifyAPI.Example/WebControl.cs @@ -128,7 +128,7 @@ namespace SpotifyAPI.Example private async void RunAuthentication() { - WebApiFactory webApiFactory = new WebApiFactory( + WebAPIFactory webApiFactory = new WebAPIFactory( "http://localhost", 8000, "26d287105e31491889f3cd293d85bfea", diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 2d639657..b6ca9004 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -60,17 +60,9 @@ namespace SpotifyAPI.Local public event EventHandler OnTrackTimeChange; - public SpotifyLocalAPI() + public SpotifyLocalAPI(int timerIntervall = 50) { _rh = new RemoteHandler(); - - AttachTimer(50); - } - - public SpotifyLocalAPI(int timerIntervall) - { - _rh = new RemoteHandler(); - AttachTimer(timerIntervall); } diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index 9c5f2c17..ad08e1e3 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -58,7 +58,7 @@ - + diff --git a/SpotifyAPI/Web/Auth/WebApiFactory.cs b/SpotifyAPI/Web/Auth/WebApiFactory.cs index b76ab436..cf1ccbea 100644 --- a/SpotifyAPI/Web/Auth/WebApiFactory.cs +++ b/SpotifyAPI/Web/Auth/WebApiFactory.cs @@ -6,30 +6,30 @@ using SpotifyAPI.Web.Models; namespace SpotifyAPI.Web.Auth { - public class WebApiFactory + public class WebAPIFactory { - private readonly string m_RedirectUrl; - private readonly int m_ListeningPort; - private readonly string m_ClientId; - private readonly TimeSpan m_Timeout; - private Scope m_Scope; + private readonly string _redirectUrl; + private readonly int _listeningPort; + private readonly string _clientId; + private readonly TimeSpan _timeout; + private readonly Scope _scope; - public WebApiFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) + public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) { - m_RedirectUrl = redirectUrl; - m_ListeningPort = listeningPort; - m_ClientId = clientId; - m_Scope = scope; - m_Timeout = timeout; + _redirectUrl = redirectUrl; + _listeningPort = listeningPort; + _clientId = clientId; + _scope = scope; + _timeout = timeout; } public Task GetWebApi() { var authentication = new ImplicitGrantAuth { - RedirectUri = $"{m_RedirectUrl}:{m_ListeningPort}", - ClientId = m_ClientId, - Scope = m_Scope, + RedirectUri = $"{_redirectUrl}:{_listeningPort}", + ClientId = _clientId, + Scope = _scope, State = "XSS" }; @@ -43,13 +43,13 @@ namespace SpotifyAPI.Web.Auth try { - authentication.StartHttpServer(m_ListeningPort); + authentication.StartHttpServer(_listeningPort); authentication.DoAuth(); - authenticationWaitFlag.WaitOne(m_Timeout); + authenticationWaitFlag.WaitOne(_timeout); if (spotifyWebApi == null) - throw new TimeoutException($"No valid response received for the last {m_Timeout.TotalSeconds} seconds"); + throw new TimeoutException($"No valid response received for the last {_timeout.TotalSeconds} seconds"); } finally {