Skip to content

Commit

Permalink
exception free spotifyapi
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallet committed Jul 24, 2021
1 parent f222869 commit c453051
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions EspionSpotify/API/SpotifyAPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EspionSpotify.Enums;
using EspionSpotify.Extensions;
using EspionSpotify.Models;
using EspionSpotify.Properties;
using EspionSpotify.Spotify;
Expand Down Expand Up @@ -59,7 +60,7 @@ public SpotifyAPI(string clientId, string secretId, string redirectUrl = SPOTIFY

if (_api != null)
{
var playback = await _api.GetPlaybackAsync();
var playback = await _api.GetPlaybackWithoutExceptionAsync();
if (playback != null && !playback.HasError())
{
playing = playback.IsPlaying;
Expand Down Expand Up @@ -129,7 +130,7 @@ private async Task UpdateTrack(Track track, bool retry = false)

if (_api == null) return;

var playback = await _api.GetPlaybackAsync();
var playback = await _api.GetPlaybackWithoutExceptionAsync();
var hasNoPlayback = playback == null || playback.Item == null;

if (!retry && hasNoPlayback)
Expand Down Expand Up @@ -167,7 +168,7 @@ private async Task UpdateTrack(Track track, bool retry = false)

if (playback.Item.Album?.Id == null) return;

var album = await _api.GetAlbumAsync(playback.Item.Album.Id);
var album = await _api.GetAlbumWithoutExceptionAsync(playback.Item.Album.Id);

if (album.HasError()) return;

Expand Down
1 change: 1 addition & 0 deletions EspionSpotify/EspionSpotify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
<Compile Include="Extensions\NAudioExtensions.cs" />
<Compile Include="Extensions\ResourceManagerExtensions.cs" />
<Compile Include="Extensions\SettingExtensions.cs" />
<Compile Include="Extensions\SpotifyWebAPIExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Events\PlayStateEventArgs.cs" />
<Compile Include="Events\TrackChangeEventArgs.cs" />
Expand Down
35 changes: 35 additions & 0 deletions EspionSpotify/Extensions/SpotifyWebAPIExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using SpotifyAPI.Web;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EspionSpotify.Extensions
{
public static class SpotifyWebAPIExtensions
{
public static async Task<PlaybackContext> GetPlaybackWithoutExceptionAsync(this SpotifyWebAPI api)
{
PlaybackContext playback = null;
try
{
playback = await api.GetPlaybackAsync();
}
catch { }
return playback;
}

public static async Task<FullAlbum> GetAlbumWithoutExceptionAsync(this SpotifyWebAPI api, string id)
{
FullAlbum album = null;
try
{
album = await api.GetAlbumAsync(id);
}
catch { }
return album;
}
}
}

0 comments on commit c453051

Please sign in to comment.