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">
<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>

View File

@ -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);
}
}
}
}

View File

@ -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)

View File

@ -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);
}

View File

@ -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;
}

View File

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

View File

@ -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>