Skip to content

Commit

Permalink
WebAPI auth (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTheGr8 committed Jun 14, 2017
1 parent 0bd78fd commit bac96a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 11 additions & 6 deletions SpotifyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;

Expand All @@ -16,7 +17,7 @@ namespace Wox.Plugin.Spotify
public class SpotifyApi
{
private readonly SpotifyLocalAPI _localSpotify;
private readonly SpotifyWebAPI _spotifyApi;
private SpotifyWebAPI _spotifyApi;
private readonly object _lock = new object();


Expand All @@ -34,13 +35,9 @@ public SpotifyApi(string pluginDir = null)
_localSpotify.OnPlayStateChange += (o, e) => IsPlaying = e.Playing;
ConnectToSpotify();

_spotifyApi = new SpotifyWebAPI
{
UseAuth = false
};
_spotifyApi = new SpotifyWebAPI();
}


public bool IsPlaying { get; set; }

public bool IsMuted => _localSpotify.IsSpotifyMuted();
Expand All @@ -51,6 +48,8 @@ public SpotifyApi(string pluginDir = null)

public bool IsConnected { get; private set; }

public bool IsWebApiCOnnected => !string.IsNullOrEmpty(_spotifyApi.AccessToken);

public bool IsRunning => SpotifyLocalAPI.IsSpotifyRunning() && SpotifyLocalAPI.IsSpotifyWebHelperRunning();

public void Play()
Expand Down Expand Up @@ -91,6 +90,12 @@ public void RunSpotify()
SpotifyLocalAPI.RunSpotify();
}

public async Task ConnectWebApi()
{
var webApiFactory = new WebAPIFactory("http://localhost", 8000, "3e271cd3f0634b92a991f60601f9db44", Scope.None, TimeSpan.FromSeconds(40));
_spotifyApi = await webApiFactory.GetWebApi();
}

public IEnumerable<FullArtist> GetArtists(string s)
{
lock (_lock)
Expand Down
25 changes: 25 additions & 0 deletions SpotifyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public List<Result> Query(Query query)

private List<Result> SearchTrack(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;

if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
Expand All @@ -240,6 +242,8 @@ private List<Result> SearchTrack(string param)

private List<Result> SearchAlbum(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;

if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
Expand All @@ -264,6 +268,8 @@ private List<Result> SearchAlbum(string param)

private List<Result> SearchArtist(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;

if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
Expand All @@ -286,5 +292,24 @@ private List<Result> SearchArtist(string param)
Task.WaitAll(results);
return results.Select(x => x.Result).ToList();
}

private List<Result> AuthenticateResult => new List<Result>()
{
new Result()
{
Title = "Authentication required to search the Spotify library",
SubTitle = "Click this to authenticate",
IcoPath = SpotifyIcon,
Action = _ => {
// This will prompt the user to authenticate
var t = new System.Threading.Thread(async () => {
await _api.ConnectWebApi();
});
t.Start();

return true;
}
}
};
}
}

0 comments on commit bac96a4

Please sign in to comment.