mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2025-01-11 14:07:47 +00:00
Code cleanup/docs in SpotifyWebAPI.cs
This commit is contained in:
parent
eb469bd9a5
commit
8bee9f2127
@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SpotifyAPI.Web
|
namespace SpotifyAPI.Web
|
||||||
@ -33,11 +34,6 @@ namespace SpotifyAPI.Web
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public string TokenType { get; set; }
|
|
||||||
public string AccessToken { get; set; }
|
|
||||||
public bool UseAuth { get; set; }
|
|
||||||
public IClient WebClient { get; set; }
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
WebClient.Dispose();
|
WebClient.Dispose();
|
||||||
@ -45,6 +41,28 @@ namespace SpotifyAPI.Web
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region Configuration
|
#region Configuration
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The type of the <see cref="AccessToken"/>
|
||||||
|
/// </summary>
|
||||||
|
public string TokenType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A valid token issued by spotify. Used only when <see cref="UseAuth"/> is true
|
||||||
|
/// </summary>
|
||||||
|
public string AccessToken { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If true, an authorization header based on <see cref="TokenType"/> and <see cref="AccessToken"/> will be used
|
||||||
|
/// </summary>
|
||||||
|
public bool UseAuth { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A custom WebClient, used for Unit-Testing
|
||||||
|
/// </summary>
|
||||||
|
public IClient WebClient { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies after how many miliseconds should a failed request be retried.
|
/// Specifies after how many miliseconds should a failed request be retried.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -63,7 +81,8 @@ namespace SpotifyAPI.Web
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Error codes that will trigger auto-retry if <see cref="UseAutoRetry"/> is enabled.
|
/// Error codes that will trigger auto-retry if <see cref="UseAutoRetry"/> is enabled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<int> RetryErrorCodes { get; private set; } = new int[] { 500, 502, 503 };
|
public IEnumerable<int> RetryErrorCodes { get; set; } = new[] { 500, 502, 503 };
|
||||||
|
|
||||||
#endregion Configuration
|
#endregion Configuration
|
||||||
|
|
||||||
#region Search
|
#region Search
|
||||||
@ -1832,12 +1851,12 @@ namespace SpotifyAPI.Web
|
|||||||
private ListResponse<T> DownloadList<T>(string url)
|
private ListResponse<T> DownloadList<T>(string url)
|
||||||
{
|
{
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
ListResponse<T> data = null;
|
ListResponse<T> data = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (data != null) { System.Threading.Thread.Sleep(RetryAfter); }
|
if (data != null) { Thread.Sleep(RetryAfter); }
|
||||||
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(url);
|
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(url);
|
||||||
data = ExtractDataToListResponse<T>(res);
|
data = ExtractDataToListResponse<T>(res);
|
||||||
|
|
||||||
@ -1853,7 +1872,7 @@ namespace SpotifyAPI.Web
|
|||||||
private async Task<ListResponse<T>> DownloadListAsync<T>(string url)
|
private async Task<ListResponse<T>> DownloadListAsync<T>(string url)
|
||||||
{
|
{
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
ListResponse<T> data = null;
|
ListResponse<T> data = null;
|
||||||
do
|
do
|
||||||
@ -1873,7 +1892,7 @@ namespace SpotifyAPI.Web
|
|||||||
|
|
||||||
private static ListResponse<T> ExtractDataToListResponse<T>(Tuple<ResponseInfo, JToken> res)
|
private static ListResponse<T> ExtractDataToListResponse<T>(Tuple<ResponseInfo, JToken> res)
|
||||||
{
|
{
|
||||||
ListResponse<T> ret = null;
|
ListResponse<T> ret;
|
||||||
if (res.Item2 is JArray)
|
if (res.Item2 is JArray)
|
||||||
{
|
{
|
||||||
ret = new ListResponse<T>
|
ret = new ListResponse<T>
|
||||||
@ -1899,7 +1918,7 @@ namespace SpotifyAPI.Web
|
|||||||
if (!UseAuth)
|
if (!UseAuth)
|
||||||
throw new InvalidOperationException("Auth is required for all Upload-Actions");
|
throw new InvalidOperationException("Auth is required for all Upload-Actions");
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
Tuple<ResponseInfo, T> response = null;
|
Tuple<ResponseInfo, T> response = null;
|
||||||
do
|
do
|
||||||
@ -1907,7 +1926,7 @@ namespace SpotifyAPI.Web
|
|||||||
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
||||||
WebClient.SetHeader("Content-Type", "application/json");
|
WebClient.SetHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
if (response != null) { System.Threading.Thread.Sleep(RetryAfter); }
|
if (response != null) { Thread.Sleep(RetryAfter); }
|
||||||
response = WebClient.UploadJson<T>(url, uploadData, method);
|
response = WebClient.UploadJson<T>(url, uploadData, method);
|
||||||
|
|
||||||
response.Item2.AddResponseInfo(response.Item1);
|
response.Item2.AddResponseInfo(response.Item1);
|
||||||
@ -1926,7 +1945,7 @@ namespace SpotifyAPI.Web
|
|||||||
throw new InvalidOperationException("Auth is required for all Upload-Actions");
|
throw new InvalidOperationException("Auth is required for all Upload-Actions");
|
||||||
|
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
Tuple<ResponseInfo, T> response = null;
|
Tuple<ResponseInfo, T> response = null;
|
||||||
do
|
do
|
||||||
@ -1950,12 +1969,12 @@ namespace SpotifyAPI.Web
|
|||||||
public T DownloadData<T>(string url) where T : BasicModel
|
public T DownloadData<T>(string url) where T : BasicModel
|
||||||
{
|
{
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
Tuple<ResponseInfo, T> response = null;
|
Tuple<ResponseInfo, T> response = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if(response != null) { System.Threading.Thread.Sleep(RetryAfter); }
|
if(response != null) { Thread.Sleep(RetryAfter); }
|
||||||
response = DownloadDataAlt<T>(url);
|
response = DownloadDataAlt<T>(url);
|
||||||
|
|
||||||
response.Item2.AddResponseInfo(response.Item1);
|
response.Item2.AddResponseInfo(response.Item1);
|
||||||
@ -1972,7 +1991,7 @@ namespace SpotifyAPI.Web
|
|||||||
public async Task<T> DownloadDataAsync<T>(string url) where T : BasicModel
|
public async Task<T> DownloadDataAsync<T>(string url) where T : BasicModel
|
||||||
{
|
{
|
||||||
int triesLeft = RetryTimes + 1;
|
int triesLeft = RetryTimes + 1;
|
||||||
Error lastError = null;
|
Error lastError;
|
||||||
|
|
||||||
Tuple<ResponseInfo, T> response = null;
|
Tuple<ResponseInfo, T> response = null;
|
||||||
do
|
do
|
||||||
|
Loading…
Reference in New Issue
Block a user