Skip to content

Commit 020b60b

Browse files
committed
Added blazor example
1 parent 2d1c1d2 commit 020b60b

6 files changed

Lines changed: 63 additions & 85 deletions

File tree

‎SpotifyAPI.Web.Examples/Example.ASPBlazor/Example.ASPBlazor.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<ProjectReference Include="..\..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
9+
</ItemGroup>
10+
711
</Project>

‎SpotifyAPI.Web.Examples/Example.ASPBlazor/Pages/Counter.razor‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎SpotifyAPI.Web.Examples/Example.ASPBlazor/Pages/FetchData.razor‎

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,61 @@
1-
@page "/"
1+
@page "/"
2+
@inject NavigationManager navManager
3+
@using SpotifyAPI.Web
24

35
<h1>Hello, world!</h1>
46

5-
Welcome to your new app.
7+
@if (_isAuthed && _me != null)
8+
{
9+
<h2>Welcome @_me.DisplayName!</h2>
10+
<p>
11+
You have a grant toal of @_totalPlaylistCount playlists!
12+
</p>
13+
}
14+
else
15+
{
16+
<span>To get started:</span>
17+
<a href="@_authUri">
18+
Login via Spotify
19+
</a>
20+
}
621

7-
<SurveyPrompt Title="How is Blazor working for you?" />
22+
@code {
23+
private Uri _authUri;
24+
25+
private bool _isAuthed;
26+
27+
private PrivateUser _me;
28+
29+
private int _totalPlaylistCount;
30+
31+
protected override void OnInitialized()
32+
{
33+
var clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
34+
var baseUri = navManager.ToAbsoluteUri(navManager.BaseUri);
35+
36+
var loginRequest = new LoginRequest(baseUri, clientId, LoginRequest.ResponseType.Token)
37+
{
38+
Scope = new[] { Scopes.PlaylistReadPrivate, Scopes.PlaylistReadCollaborative }
39+
};
40+
_authUri = loginRequest.ToUri();
41+
}
42+
43+
protected override async Task OnInitializedAsync()
44+
{
45+
var uri = new Uri(navManager.Uri);
46+
var maxLen = Math.Min(1, uri.Fragment.Length);
47+
Dictionary<string, string> fragmentParams = uri.Fragment.Substring(maxLen)
48+
?.Split("&", StringSplitOptions.RemoveEmptyEntries)
49+
?.Select(param => param.Split("=", StringSplitOptions.RemoveEmptyEntries))
50+
?.ToDictionary(param => param[0], param => param[1]) ?? new Dictionary<string, string>();
51+
52+
_isAuthed = fragmentParams.ContainsKey("access_token");
53+
if (_isAuthed)
54+
{
55+
var spotify = new SpotifyClient(fragmentParams["access_token"]);
56+
57+
_me = await spotify.UserProfile.Current();
58+
_totalPlaylistCount = (await spotify.Playlists.CurrentUsers()).Total;
59+
}
60+
}
61+
}

‎SpotifyAPI.Web.Examples/Example.ASPBlazor/Program.cs‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore;
71
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
92
using Microsoft.Extensions.Hosting;
10-
using Microsoft.Extensions.Logging;
113

124
namespace Example.ASPBlazor
135
{
14-
public class Program
6+
public class Program
157
{
168
public static void Main(string[] args)
179
{

‎SpotifyAPI.Web.Examples/Example.ASPBlazor/Shared/NavMenu.razor‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="top-row pl-4 navbar navbar-dark">
1+
<div class="top-row pl-4 navbar navbar-dark">
22
<a class="navbar-brand" href="">Example.ASPBlazor</a>
33
<button class="navbar-toggler" @onclick="ToggleNavMenu">
44
<span class="navbar-toggler-icon"></span>
@@ -12,16 +12,6 @@
1212
<span class="oi oi-home" aria-hidden="true"></span> Home
1313
</NavLink>
1414
</li>
15-
<li class="nav-item px-3">
16-
<NavLink class="nav-link" href="counter">
17-
<span class="oi oi-plus" aria-hidden="true"></span> Counter
18-
</NavLink>
19-
</li>
20-
<li class="nav-item px-3">
21-
<NavLink class="nav-link" href="fetchdata">
22-
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
23-
</NavLink>
24-
</li>
2515
</ul>
2616
</div>
2717

0 commit comments

Comments
 (0)