Skip to content

Commit

Permalink
Merge branch 'iwate_master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/ODataHttpClient/Models/Response.cs
  • Loading branch information
psmolich79 committed Aug 6, 2021
2 parents 7762759 + 597337a commit 899cce6
Showing 1 changed file with 16 additions and 42 deletions.
58 changes: 16 additions & 42 deletions src/ODataHttpClient/Models/Response.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using ODataHttpClient.Serializers;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;

namespace ODataHttpClient.Models
Expand All @@ -17,10 +14,8 @@ public class Response
public string MediaType { get; private set; }
public string ErrorMessage { get; private set; }
public byte[] Binary { get; private set; }
public string Body { get => GetStringFromUTF8(Binary); }
public HttpResponseHeaders Headers { get; private set; }

private Response() { }
public string Body { get => Binary != null ? Encoding.UTF8.GetString(Binary) : null; }
private Response(){}

public T ReadAs<T>(string jsonPath = null)
{
Expand Down Expand Up @@ -58,13 +53,10 @@ public T ReadAs<T>(string jsonPath, IJsonSerializer serializer)
return (T)(object)Convert.ToInt64(Body);

if (type == typeof(double) || type == typeof(double?))
return (T)(object)Convert.ToDouble(Body, CultureInfo.InvariantCulture);

if (type == typeof(float) || type == typeof(float?))
return (T)(object)Convert.ToSingle(Body, CultureInfo.InvariantCulture);
return (T)(object)Convert.ToDouble(Body);

if (type == typeof(decimal) || type == typeof(decimal?))
return (T)(object)Convert.ToDecimal(Body, CultureInfo.InvariantCulture);
return (T)(object)Convert.ToDecimal(Body);

if (type == typeof(DateTime) || type == typeof(DateTime?))
return (T)(object)Convert.ToDateTime(Body);
Expand All @@ -85,48 +77,30 @@ public T ReadAs<T>(string jsonPath, IJsonSerializer serializer)
throw new NotSupportedException();
}

private static readonly byte[] _utf8Preamble = Encoding.UTF8.GetPreamble();
private static bool IsUTF8WithBOM(byte[] binary) => binary.Take(_utf8Preamble.Length).SequenceEqual(_utf8Preamble);
private static (int start, int len) GetUTF8StartAndLength(byte[] binary)
{
if (IsUTF8WithBOM(binary))
return (_utf8Preamble.Length, binary.Length - _utf8Preamble.Length);

return (0, binary.Length);
}
public static string GetStringFromUTF8(byte[] binary)
{
if (binary == null)
return null;

var (start, len) = GetUTF8StartAndLength(binary);
return Encoding.UTF8.GetString(binary, start, len);
}

public static Response CreateError(HttpStatusCode code, byte[] body, HttpResponseHeaders headers)
public static Response CreateError(HttpStatusCode code, byte[] body)
{
return CreateError(code, GetStringFromUTF8(body), headers);
return CreateError(code, body != null ? Encoding.UTF8.GetString(body) : null);
}
public static Response CreateError(HttpStatusCode code, string message, HttpResponseHeaders headers)
public static Response CreateError(HttpStatusCode code, string message)
{
return new Response { Success = false, StatusCode = code, ErrorMessage = message, Headers = headers };
return new Response { Success = false, StatusCode = code, ErrorMessage = message };
}

public static Response CreateSuccess(HttpStatusCode code, string mime, string body, HttpResponseHeaders headers = null)
public static Response CreateSuccess(HttpStatusCode code, string mime, string body)
{
return CreateSuccess(code, mime, body, JsonSerializer.Default, headers);
return CreateSuccess(code, mime, body, JsonSerializer.Default);
}
public static Response CreateSuccess(HttpStatusCode code, string mime, byte[] body, HttpResponseHeaders headers = null)
public static Response CreateSuccess(HttpStatusCode code, string mime, byte[] body)
{
return CreateSuccess(code, mime, body, JsonSerializer.Default, headers);
return CreateSuccess(code, mime, body, JsonSerializer.Default);
}
public static Response CreateSuccess(HttpStatusCode code, string mime, string body, IJsonSerializer serializer, HttpResponseHeaders headers)
public static Response CreateSuccess(HttpStatusCode code, string mime, string body, IJsonSerializer serializer)
{
return CreateSuccess(code, mime, body != null ? Encoding.UTF8.GetBytes(body) : null, serializer, headers);
return CreateSuccess(code, mime, body != null ? Encoding.UTF8.GetBytes(body) : null, serializer);
}
public static Response CreateSuccess(HttpStatusCode code, string mime, byte[] body, IJsonSerializer serializer, HttpResponseHeaders headers)
public static Response CreateSuccess(HttpStatusCode code, string mime, byte[] body, IJsonSerializer serializer)
{
return new Response { Success = true, StatusCode = code, MediaType = mime, Binary = body, _serializer = serializer, Headers = headers };
return new Response { Success = true, StatusCode = code, MediaType = mime, Binary = body, _serializer = serializer };
}
}
}

0 comments on commit 899cce6

Please sign in to comment.