-
Notifications
You must be signed in to change notification settings - Fork 314
Stability improvement: #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8dde236
Stability improvement:
jholzer 0130c96
- Changed Listener handling to asynchronious handling
jholzer d21e844
AuthenticationFactory
jholzer f0290fa
WebControl with new AuthenticationFactory
jholzer 234a522
Cleaning up after timeout
jholzer 0813742
Timer should be disabled and events detached if no longer needed.
jholzer 961f25a
- Changes from manual reading of header content to Linq-approach (pro…
jholzer f13ac73
restore original .gitignore
jholzer 1699519
Renamed AuthenticationFactory to WebApiFactory (better description of…
jholzer 4699246
Adapted code style
jholzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using SpotifyAPI.Web.Enums; | ||
| using SpotifyAPI.Web.Models; | ||
|
|
||
| namespace SpotifyAPI.Web.Auth | ||
| { | ||
| public class WebAPIFactory | ||
| { | ||
| 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) | ||
| { | ||
| _redirectUrl = redirectUrl; | ||
| _listeningPort = listeningPort; | ||
| _clientId = clientId; | ||
| _scope = scope; | ||
| _timeout = timeout; | ||
| } | ||
|
|
||
| public Task<SpotifyWebAPI> GetWebApi() | ||
| { | ||
| var authentication = new ImplicitGrantAuth | ||
| { | ||
| RedirectUri = $"{_redirectUrl}:{_listeningPort}", | ||
| ClientId = _clientId, | ||
| Scope = _scope, | ||
| State = "XSS" | ||
| }; | ||
|
|
||
| AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false); | ||
| SpotifyWebAPI spotifyWebApi = null; | ||
| authentication.OnResponseReceivedEvent += (token, state) => | ||
| { | ||
| spotifyWebApi = HandleSpotifyResponse(state, token); | ||
| authenticationWaitFlag.Set(); | ||
| }; | ||
|
|
||
| try | ||
| { | ||
| authentication.StartHttpServer(_listeningPort); | ||
|
|
||
| authentication.DoAuth(); | ||
|
|
||
| authenticationWaitFlag.WaitOne(_timeout); | ||
| if (spotifyWebApi == null) | ||
| throw new TimeoutException($"No valid response received for the last {_timeout.TotalSeconds} seconds"); | ||
| } | ||
| finally | ||
| { | ||
| 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) | ||
| { } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you restore this file? I guess VS accidently overwrote it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!