Skip to content

Commit 8bee9f2

Browse files
author
Johnny @PC
committed
Code cleanup/docs in SpotifyWebAPI.cs
1 parent eb469bd commit 8bee9f2

1 file changed

Lines changed: 39 additions & 20 deletions

File tree

‎SpotifyAPI/Web/SpotifyWebAPI.cs‎

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Runtime.InteropServices;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011

1112
namespace SpotifyAPI.Web
@@ -33,37 +34,55 @@ public SpotifyWebAPI()
3334
};
3435
}
3536

36-
public string TokenType { get; set; }
37-
public string AccessToken { get; set; }
38-
public bool UseAuth { get; set; }
39-
public IClient WebClient { get; set; }
40-
4137
public void Dispose()
4238
{
4339
WebClient.Dispose();
4440
GC.SuppressFinalize(this);
4541
}
4642

4743
#region Configuration
44+
45+
/// <summary>
46+
/// The type of the <see cref="AccessToken"/>
47+
/// </summary>
48+
public string TokenType { get; set; }
49+
50+
/// <summary>
51+
/// A valid token issued by spotify. Used only when <see cref="UseAuth"/> is true
52+
/// </summary>
53+
public string AccessToken { get; set; }
54+
55+
/// <summary>
56+
/// If true, an authorization header based on <see cref="TokenType"/> and <see cref="AccessToken"/> will be used
57+
/// </summary>
58+
public bool UseAuth { get; set; }
59+
4860
/// <summary>
49-
/// Specifies after how many miliseconds should a failed request be retried.
61+
/// A custom WebClient, used for Unit-Testing
62+
/// </summary>
63+
public IClient WebClient { get; set; }
64+
65+
66+
/// <summary>
67+
/// Specifies after how many miliseconds should a failed request be retried.
5068
/// </summary>
5169
public int RetryAfter { get; set; } = 50;
5270

5371
/// <summary>
54-
/// Should a failed request (specified by <see cref="RetryErrorCodes"/> be automatically retried or not.
72+
/// Should a failed request (specified by <see cref="RetryErrorCodes"/> be automatically retried or not.
5573
/// </summary>
5674
public bool UseAutoRetry { get; set; } = false;
5775

5876
/// <summary>
59-
/// Maximum number of tries for one failed request.
77+
/// Maximum number of tries for one failed request.
6078
/// </summary>
6179
public int RetryTimes { get; set; } = 10;
6280

6381
/// <summary>
64-
/// Error codes that will trigger auto-retry if <see cref="UseAutoRetry"/> is enabled.
82+
/// Error codes that will trigger auto-retry if <see cref="UseAutoRetry"/> is enabled.
6583
/// </summary>
66-
public IEnumerable<int> RetryErrorCodes { get; private set; } = new int[] { 500, 502, 503 };
84+
public IEnumerable<int> RetryErrorCodes { get; set; } = new[] { 500, 502, 503 };
85+
6786
#endregion Configuration
6887

6988
#region Search
@@ -1832,12 +1851,12 @@ public async Task<Paging<T>> GetPreviousPageAsync<T>(Paging<T> paging)
18321851
private ListResponse<T> DownloadList<T>(string url)
18331852
{
18341853
int triesLeft = RetryTimes + 1;
1835-
Error lastError = null;
1854+
Error lastError;
18361855

18371856
ListResponse<T> data = null;
18381857
do
18391858
{
1840-
if (data != null) { System.Threading.Thread.Sleep(RetryAfter); }
1859+
if (data != null) { Thread.Sleep(RetryAfter); }
18411860
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(url);
18421861
data = ExtractDataToListResponse<T>(res);
18431862

@@ -1853,7 +1872,7 @@ private ListResponse<T> DownloadList<T>(string url)
18531872
private async Task<ListResponse<T>> DownloadListAsync<T>(string url)
18541873
{
18551874
int triesLeft = RetryTimes + 1;
1856-
Error lastError = null;
1875+
Error lastError;
18571876

18581877
ListResponse<T> data = null;
18591878
do
@@ -1873,7 +1892,7 @@ private async Task<ListResponse<T>> DownloadListAsync<T>(string url)
18731892

18741893
private static ListResponse<T> ExtractDataToListResponse<T>(Tuple<ResponseInfo, JToken> res)
18751894
{
1876-
ListResponse<T> ret = null;
1895+
ListResponse<T> ret;
18771896
if (res.Item2 is JArray)
18781897
{
18791898
ret = new ListResponse<T>
@@ -1899,15 +1918,15 @@ public T UploadData<T>(string url, string uploadData, string method = "POST") wh
18991918
if (!UseAuth)
19001919
throw new InvalidOperationException("Auth is required for all Upload-Actions");
19011920
int triesLeft = RetryTimes + 1;
1902-
Error lastError = null;
1921+
Error lastError;
19031922

19041923
Tuple<ResponseInfo, T> response = null;
19051924
do
19061925
{
19071926
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
19081927
WebClient.SetHeader("Content-Type", "application/json");
19091928

1910-
if (response != null) { System.Threading.Thread.Sleep(RetryAfter); }
1929+
if (response != null) { Thread.Sleep(RetryAfter); }
19111930
response = WebClient.UploadJson<T>(url, uploadData, method);
19121931

19131932
response.Item2.AddResponseInfo(response.Item1);
@@ -1926,7 +1945,7 @@ public async Task<T> UploadDataAsync<T>(string url, string uploadData, string me
19261945
throw new InvalidOperationException("Auth is required for all Upload-Actions");
19271946

19281947
int triesLeft = RetryTimes + 1;
1929-
Error lastError = null;
1948+
Error lastError;
19301949

19311950
Tuple<ResponseInfo, T> response = null;
19321951
do
@@ -1950,12 +1969,12 @@ public async Task<T> UploadDataAsync<T>(string url, string uploadData, string me
19501969
public T DownloadData<T>(string url) where T : BasicModel
19511970
{
19521971
int triesLeft = RetryTimes + 1;
1953-
Error lastError = null;
1972+
Error lastError;
19541973

19551974
Tuple<ResponseInfo, T> response = null;
19561975
do
19571976
{
1958-
if(response != null) { System.Threading.Thread.Sleep(RetryAfter); }
1977+
if(response != null) { Thread.Sleep(RetryAfter); }
19591978
response = DownloadDataAlt<T>(url);
19601979

19611980
response.Item2.AddResponseInfo(response.Item1);
@@ -1972,7 +1991,7 @@ public T DownloadData<T>(string url) where T : BasicModel
19721991
public async Task<T> DownloadDataAsync<T>(string url) where T : BasicModel
19731992
{
19741993
int triesLeft = RetryTimes + 1;
1975-
Error lastError = null;
1994+
Error lastError;
19761995

19771996
Tuple<ResponseInfo, T> response = null;
19781997
do

0 commit comments

Comments
 (0)