Skip to content

Commit a70bc5f

Browse files
philippdrebesJohnnyCrazy
authored andcommitted
Refactored SpotifyUri Parsing (#194)
* refactored SpotifyUri parsing * unit tests for SpotifyUri class * added missing file reference in SpotifyAPI.csproj * updated NuGet packages in SpotifyAPI.Tests and reverted inline TryParse variable declarations in SpotifyUri.cs
1 parent c6d5ebe commit a70bc5f

8 files changed

Lines changed: 129 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TestResults
1212
*.suo
1313
*.user
1414
*.sln.docstates
15+
.vs/
1516

1617
# Build results
1718
[Dd]ebug/

SpotifyAPI.Tests/SpotifyAPI.Tests.csproj

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
36-
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
37-
<Private>True</Private>
35+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
36+
<HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
3837
</Reference>
39-
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
40-
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
41-
<Private>True</Private>
38+
<Reference Include="Moq, Version=4.7.145.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Moq.4.7.145\lib\net45\Moq.dll</HintPath>
4240
</Reference>
43-
<Reference Include="nunit.framework, Version=3.0.5797.27534, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
44-
<HintPath>..\packages\NUnit.3.0.0\lib\net45\nunit.framework.dll</HintPath>
45-
<Private>True</Private>
41+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
43+
</Reference>
44+
<Reference Include="nunit.framework, Version=3.8.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
45+
<HintPath>..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll</HintPath>
4646
</Reference>
4747
<Reference Include="System" />
48+
<Reference Include="System.Configuration" />
4849
<Reference Include="System.Core" />
4950
<Reference Include="System.Xml.Linq" />
5051
<Reference Include="System.Data.DataSetExtensions" />
@@ -53,6 +54,7 @@
5354
<Reference Include="System.Xml" />
5455
</ItemGroup>
5556
<ItemGroup>
57+
<Compile Include="SpotifyUriTest.cs" />
5658
<Compile Include="TestClass.cs" />
5759
<Compile Include="Properties\AssemblyInfo.cs" />
5860
</ItemGroup>
@@ -67,6 +69,9 @@
6769
<None Include="fixtures\public-user.json" />
6870
<None Include="packages.config" />
6971
</ItemGroup>
72+
<ItemGroup>
73+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
74+
</ItemGroup>
7075
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7176
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
7277
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -76,4 +81,4 @@
7681
<Target Name="AfterBuild">
7782
</Target>
7883
-->
79-
</Project>
84+
</Project>

SpotifyAPI.Tests/SpotifyUriTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Moq;
2+
using Newtonsoft.Json;
3+
using NUnit.Framework;
4+
using SpotifyAPI.Local;
5+
using SpotifyAPI.Local.Models;
6+
using SpotifyAPI.Local.Enums;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Linq;
11+
12+
namespace SpotifyAPI.Tests
13+
{
14+
public class SpotifyUriTest
15+
{
16+
[Test]
17+
public void ShouldThrowArgumentExceptionForInvalidUri()
18+
{
19+
Assert.Throws<ArgumentException>(() => SpotifyUri.Parse("asdafadfgsrsegqejfa"));
20+
}
21+
22+
[Test]
23+
public void ShouldCorrectlyParseTrackUri()
24+
{
25+
string testUri = "spotify:track:3QOruXa2lvqIFvOOa2rYyJ";
26+
SpotifyUri uri = SpotifyUri.Parse(testUri);
27+
28+
Assert.AreEqual(uri.Base, "spotify");
29+
30+
Assert.AreEqual(uri.Type, UriType.track);
31+
Assert.AreEqual(uri.Id, "3QOruXa2lvqIFvOOa2rYyJ");
32+
Assert.AreEqual(uri.ToString(), testUri);
33+
}
34+
35+
[Test]
36+
public void ShouldCorrectlyParsePlaylistUri()
37+
{
38+
string testUri = "spotify:user:spotifycharts:playlist:37i9dQZEVXbMDoHDwVN2tF";
39+
SpotifyUri uri = SpotifyUri.Parse(testUri);
40+
41+
Assert.AreEqual(uri.Base, "spotify");
42+
43+
Assert.AreEqual(uri.Type, UriType.playlist);
44+
Assert.AreEqual(uri.Id, "37i9dQZEVXbMDoHDwVN2tF");
45+
46+
Assert.AreEqual(uri.GetUriPropValue(UriType.user), "spotifycharts");
47+
Assert.AreEqual(uri.ToString(), testUri);
48+
}
49+
50+
[Test]
51+
public void ShouldHandleNotExistingUriProperty()
52+
{
53+
string testUri = "spotify:track:3QOruXa2lvqIFvOOa2rYyJ";
54+
SpotifyUri uri = SpotifyUri.Parse(testUri);
55+
Assert.DoesNotThrow(() => uri.GetUriPropValue(UriType.user));
56+
Assert.IsNull(uri.GetUriPropValue(UriType.user));
57+
}
58+
}
59+
}

SpotifyAPI.Tests/packages.config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
4-
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
5-
<package id="NUnit" version="3.0.0" targetFramework="net45" />
3+
<package id="Castle.Core" version="4.2.1" targetFramework="net45" />
4+
<package id="Moq" version="4.7.145" targetFramework="net45" />
5+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
6+
<package id="NUnit" version="3.8.1" targetFramework="net45" />
67
</packages>

SpotifyAPI/Local/Enums/UriType.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SpotifyAPI.Local.Enums
2+
{
3+
public enum UriType
4+
{
5+
none,
6+
track,
7+
album,
8+
artist,
9+
playlist,
10+
user
11+
}
12+
}
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using SpotifyAPI.Local.Enums;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -8,32 +9,56 @@ namespace SpotifyAPI.Local.Models
89
{
910
public class SpotifyUri
1011
{
12+
internal Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
13+
1114
public string Base { get; internal set; }
12-
public string Type { get; internal set; }
13-
public string Id { get; internal set; }
15+
public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none;
16+
public string Id => _properties?.LastOrDefault().Value;
17+
18+
public SpotifyUri(string uriBase, Dictionary<UriType, string> properties)
19+
{
20+
Base = uriBase;
21+
_properties = properties;
22+
}
1423

15-
public SpotifyUri(string uriBase, string uriType, string uriId)
24+
public SpotifyUri(string uriBase, UriType uriType, string uriId)
1625
{
1726
Base = uriBase;
18-
Type = uriType;
19-
Id = uriId;
27+
_properties.Add(uriType, uriId);
28+
}
29+
30+
public string GetUriPropValue(UriType type)
31+
{
32+
if (!_properties.ContainsKey(type))
33+
return null;
34+
return _properties[type];
2035
}
2136

2237
public static SpotifyUri Parse(string uri)
2338
{
2439
if (string.IsNullOrEmpty(uri))
2540
throw new ArgumentNullException("Uri");
2641

42+
UriType uriType = UriType.none;
2743
string[] props = uri.Split(':');
28-
if (props.Length != 3)
44+
if (props.Length < 3 || !Enum.TryParse(props[1], out uriType))
2945
throw new ArgumentException("Unexpected Uri");
3046

31-
return new SpotifyUri(props[0], props[1], props[2]);
47+
Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } };
48+
49+
for (int index = 3; index < props.Length; index += 2)
50+
{
51+
UriType type = UriType.none;
52+
if (Enum.TryParse(props[index], out type))
53+
properties.Add(type, props[index + 1]);
54+
}
55+
56+
return new SpotifyUri(props[0], properties);
3257
}
3358

3459
public override string ToString()
3560
{
36-
return $"{Base}:{Type}:{Id}";
61+
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}";
3762
}
3863
}
3964
}

SpotifyAPI/SpotifyAPI.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737
<NoWarn>1591</NoWarn>
3838
</PropertyGroup>
3939
<ItemGroup>
40-
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
42-
<Private>True</Private>
40+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
4342
</Reference>
4443
<Reference Include="System" />
4544
<Reference Include="System.ComponentModel.DataAnnotations" />
@@ -55,6 +54,7 @@
5554
<Reference Include="System.Xml" />
5655
</ItemGroup>
5756
<ItemGroup>
57+
<Compile Include="Local\Enums\UriType.cs" />
5858
<Compile Include="Local\ExtendedWebClient.cs">
5959
<SubType>Component</SubType>
6060
</Compile>

SpotifyAPI/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
3+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
44
</packages>

0 commit comments

Comments
 (0)