mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 14:46:26 +00:00
netstandard2.0 support
This commit is contained in:
parent
f05b208f6a
commit
fabf49ef3a
@ -1,6 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.1</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||
<PackageId>SpotifyAPI.Web.Auth</PackageId>
|
||||
<Title>SpotifyAPI.Web.Auth</Title>
|
||||
<Authors>Jonas Dellinger</Authors>
|
||||
|
@ -212,9 +212,15 @@ namespace SpotifyAPI.Web.Http
|
||||
|
||||
private async Task ApplyAuthenticator(IRequest request)
|
||||
{
|
||||
#if NETSTANDARD2_0
|
||||
if (_authenticator != null
|
||||
&& !request.Endpoint.IsAbsoluteUri
|
||||
|| request.Endpoint.AbsoluteUri.Contains("https://api.spotify.com"))
|
||||
#else
|
||||
if (_authenticator != null
|
||||
&& !request.Endpoint.IsAbsoluteUri
|
||||
|| request.Endpoint.AbsoluteUri.Contains("https://api.spotify.com", StringComparison.InvariantCulture))
|
||||
#endif
|
||||
{
|
||||
await _authenticator.Apply(request, this).ConfigureAwait(false);
|
||||
}
|
||||
@ -267,11 +273,13 @@ namespace SpotifyAPI.Web.Http
|
||||
return;
|
||||
}
|
||||
|
||||
throw response.StatusCode switch
|
||||
switch (response.StatusCode)
|
||||
{
|
||||
HttpStatusCode.Unauthorized => new APIUnauthorizedException(response),
|
||||
_ => new APIException(response),
|
||||
};
|
||||
case HttpStatusCode.Unauthorized:
|
||||
throw new APIUnauthorizedException(response);
|
||||
default:
|
||||
throw new APIException(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,12 +30,14 @@ namespace SpotifyAPI.Web.Http
|
||||
{
|
||||
Ensure.ArgumentNotNull(request, nameof(request));
|
||||
|
||||
using HttpRequestMessage requestMsg = BuildRequestMessage(request);
|
||||
var responseMsg = await _httpClient
|
||||
.SendAsync(requestMsg, HttpCompletionOption.ResponseContentRead)
|
||||
.ConfigureAwait(false);
|
||||
using (HttpRequestMessage requestMsg = BuildRequestMessage(request))
|
||||
{
|
||||
var responseMsg = await _httpClient
|
||||
.SendAsync(requestMsg, HttpCompletionOption.ResponseContentRead)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return await BuildResponse(responseMsg).ConfigureAwait(false);
|
||||
return await BuildResponse(responseMsg).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<IResponse> BuildResponse(HttpResponseMessage responseMsg)
|
||||
@ -43,17 +45,19 @@ namespace SpotifyAPI.Web.Http
|
||||
Ensure.ArgumentNotNull(responseMsg, nameof(responseMsg));
|
||||
|
||||
// We only support text stuff for now
|
||||
using var content = responseMsg.Content;
|
||||
var headers = responseMsg.Headers.ToDictionary(header => header.Key, header => header.Value.First());
|
||||
var body = await responseMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
var contentType = content.Headers?.ContentType?.MediaType;
|
||||
|
||||
return new Response(headers)
|
||||
using (var content = responseMsg.Content)
|
||||
{
|
||||
ContentType = contentType,
|
||||
StatusCode = responseMsg.StatusCode,
|
||||
Body = body
|
||||
};
|
||||
var headers = responseMsg.Headers.ToDictionary(header => header.Key, header => header.Value.First());
|
||||
var body = await responseMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
var contentType = content.Headers?.ContentType?.MediaType;
|
||||
|
||||
return new Response(headers)
|
||||
{
|
||||
ContentType = contentType,
|
||||
StatusCode = responseMsg.StatusCode,
|
||||
Body = body
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpRequestMessage BuildRequestMessage(IRequest request)
|
||||
|
@ -25,8 +25,12 @@ namespace SpotifyAPI.Web.Http
|
||||
public void OnResponse(IResponse response)
|
||||
{
|
||||
Ensure.ArgumentNotNull(response, nameof(response));
|
||||
|
||||
#if NETSTANDARD2_0
|
||||
string body = response.Body?.ToString().Replace("\n", "");
|
||||
#else
|
||||
string body = response.Body?.ToString().Replace("\n", "", StringComparison.InvariantCulture);
|
||||
#endif
|
||||
|
||||
body = body?.Substring(0, Math.Min(50, body?.Length ?? 0));
|
||||
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ namespace SpotifyAPI.Web.Http
|
||||
|
||||
private static int? ParseTooManyRetriesToMs(IResponse response)
|
||||
{
|
||||
if (response.StatusCode != HttpStatusCode.TooManyRequests)
|
||||
if (response.StatusCode != (HttpStatusCode)429)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ namespace SpotifyAPI.Web
|
||||
public interface IPlayableItem
|
||||
{
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public ItemType Type { get; }
|
||||
ItemType Type { get; }
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.1</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||
<!-- <GenerateDocumentationFile>true</GenerateDocumentationFile> -->
|
||||
<PackageId>SpotifyAPI.Web</PackageId>
|
||||
<Title>SpotifyAPI.Web</Title>
|
||||
|
Loading…
Reference in New Issue
Block a user