netstandard2.0 support

This commit is contained in:
Jonas Dellinger 2020-05-16 18:48:32 +02:00
parent f05b208f6a
commit fabf49ef3a
7 changed files with 40 additions and 24 deletions

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks> <TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<PackageId>SpotifyAPI.Web.Auth</PackageId> <PackageId>SpotifyAPI.Web.Auth</PackageId>
<Title>SpotifyAPI.Web.Auth</Title> <Title>SpotifyAPI.Web.Auth</Title>
<Authors>Jonas Dellinger</Authors> <Authors>Jonas Dellinger</Authors>

View File

@ -212,9 +212,15 @@ namespace SpotifyAPI.Web.Http
private async Task ApplyAuthenticator(IRequest request) 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 if (_authenticator != null
&& !request.Endpoint.IsAbsoluteUri && !request.Endpoint.IsAbsoluteUri
|| request.Endpoint.AbsoluteUri.Contains("https://api.spotify.com", StringComparison.InvariantCulture)) || request.Endpoint.AbsoluteUri.Contains("https://api.spotify.com", StringComparison.InvariantCulture))
#endif
{ {
await _authenticator.Apply(request, this).ConfigureAwait(false); await _authenticator.Apply(request, this).ConfigureAwait(false);
} }
@ -267,11 +273,13 @@ namespace SpotifyAPI.Web.Http
return; return;
} }
throw response.StatusCode switch switch (response.StatusCode)
{ {
HttpStatusCode.Unauthorized => new APIUnauthorizedException(response), case HttpStatusCode.Unauthorized:
_ => new APIException(response), throw new APIUnauthorizedException(response);
}; default:
throw new APIException(response);
}
} }
} }
} }

View File

@ -30,12 +30,14 @@ namespace SpotifyAPI.Web.Http
{ {
Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(request, nameof(request));
using HttpRequestMessage requestMsg = BuildRequestMessage(request); using (HttpRequestMessage requestMsg = BuildRequestMessage(request))
var responseMsg = await _httpClient {
.SendAsync(requestMsg, HttpCompletionOption.ResponseContentRead) var responseMsg = await _httpClient
.ConfigureAwait(false); .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) private static async Task<IResponse> BuildResponse(HttpResponseMessage responseMsg)
@ -43,17 +45,19 @@ namespace SpotifyAPI.Web.Http
Ensure.ArgumentNotNull(responseMsg, nameof(responseMsg)); Ensure.ArgumentNotNull(responseMsg, nameof(responseMsg));
// We only support text stuff for now // We only support text stuff for now
using var content = responseMsg.Content; 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)
{ {
ContentType = contentType, var headers = responseMsg.Headers.ToDictionary(header => header.Key, header => header.Value.First());
StatusCode = responseMsg.StatusCode, var body = await responseMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
Body = body var contentType = content.Headers?.ContentType?.MediaType;
};
return new Response(headers)
{
ContentType = contentType,
StatusCode = responseMsg.StatusCode,
Body = body
};
}
} }
private static HttpRequestMessage BuildRequestMessage(IRequest request) private static HttpRequestMessage BuildRequestMessage(IRequest request)

View File

@ -25,8 +25,12 @@ namespace SpotifyAPI.Web.Http
public void OnResponse(IResponse response) public void OnResponse(IResponse response)
{ {
Ensure.ArgumentNotNull(response, nameof(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); string body = response.Body?.ToString().Replace("\n", "", StringComparison.InvariantCulture);
#endif
body = body?.Substring(0, Math.Min(50, body?.Length ?? 0)); body = body?.Substring(0, Math.Min(50, body?.Length ?? 0));
Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body); Console.WriteLine("--> {0} {1} {2}\n", response.StatusCode, response.ContentType, body);
} }

View File

@ -52,7 +52,7 @@ namespace SpotifyAPI.Web.Http
private static int? ParseTooManyRetriesToMs(IResponse response) private static int? ParseTooManyRetriesToMs(IResponse response)
{ {
if (response.StatusCode != HttpStatusCode.TooManyRequests) if (response.StatusCode != (HttpStatusCode)429)
{ {
return null; return null;
} }

View File

@ -12,6 +12,6 @@ namespace SpotifyAPI.Web
public interface IPlayableItem public interface IPlayableItem
{ {
[JsonConverter(typeof(StringEnumConverter))] [JsonConverter(typeof(StringEnumConverter))]
public ItemType Type { get; } ItemType Type { get; }
} }
} }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks> <TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
<!-- <GenerateDocumentationFile>true</GenerateDocumentationFile> --> <!-- <GenerateDocumentationFile>true</GenerateDocumentationFile> -->
<PackageId>SpotifyAPI.Web</PackageId> <PackageId>SpotifyAPI.Web</PackageId>
<Title>SpotifyAPI.Web</Title> <Title>SpotifyAPI.Web</Title>