Skip to content

Commit

Permalink
Bump Flurl.Http to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gitfool committed Nov 14, 2020
1 parent 7722865 commit b0d04ff
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="Flurl" Version="3.0.0" />
<PackageReference Include="Flurl.Http" Version="2.4.2" />
<PackageReference Include="Flurl.Http" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="PocketLogger.Subscribe" Version="0.7.0" />
<PackageReference Include="Polly" Version="7.2.1" />
Expand Down
32 changes: 5 additions & 27 deletions Application/Authenticator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using BoardGameGeek.Dungeon.Converters;
using BoardGameGeek.Dungeon.Services;
using Flurl.Http;
using Pocket;
using static Pocket.Logger<BoardGameGeek.Dungeon.Authenticator>;

Expand All @@ -21,43 +20,22 @@ public Authenticator(IBggService bggService)
public async Task AuthenticateUser(string userName, string password)
{
var fileName = $"BGG-{userName}-Auth.json"; // auth cache
var options = new JsonSerializerOptions { Converters = { new CookieConverter() }, WriteIndented = true };
if (password != null)
{
Log.Info("Authenticating user");
var cookies = await BggService.LoginUserAsync(userName, password);
var json = JsonSerializer.Serialize(cookies, new JsonSerializerOptions
{
Converters = { new CookieConverter() },
WriteIndented = true
});
var json = JsonSerializer.Serialize(cookies, options);
await File.WriteAllTextAsync(fileName, json);
}
else
{
var json = await File.ReadAllTextAsync(fileName);
var cookies = JsonSerializer.Deserialize<IDictionary<string, Cookie>>(json);
var cookies = JsonSerializer.Deserialize<IEnumerable<FlurlCookie>>(json, options);
BggService.LoginUser(cookies);
}
}

private class CookieConverter : JsonConverter<Cookie>
{
public override Cookie Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();

public override void Write(Utf8JsonWriter writer, Cookie value, JsonSerializerOptions options)
{
// select minimal properties for roundtrip
writer.WriteStartObject();
writer.WriteString("Domain", value.Domain);
writer.WriteString("Expires", value.Expires);
writer.WriteString("Name", value.Name);
writer.WriteString("Path", value.Path);
writer.WriteString("TimeStamp", value.TimeStamp);
writer.WriteString("Value", value.Value);
writer.WriteEndObject();
}
}

private IBggService BggService { get; }
}
}
4 changes: 2 additions & 2 deletions Application/CommandLine/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class Bootstrap
{
static Bootstrap()
{
var userNameArgument = new Argument<string>{ Name = "username", Description = "Geek username." };
var passwordArgument = new Argument<string>{ Name = "password", Description = "Geek password." };
var userNameArgument = new Argument<string> { Name = "username", Description = "Geek username." };
var passwordArgument = new Argument<string> { Name = "password", Description = "Geek password." };

var passwordOption = new Option<string>(new[] { "--password", "-p" }, "Geek password. Defaults to last specified.");

Expand Down
56 changes: 56 additions & 0 deletions Application/Converters/CookieConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using BoardGameGeek.Dungeon.Extensions;
using Flurl.Http;

namespace BoardGameGeek.Dungeon.Converters
{
public sealed class CookieConverter : JsonConverter<FlurlCookie>
{
public override FlurlCookie Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
reader.CheckStartObject();
var originUrl = reader.ReadString("OriginUrl");
var dateReceived = reader.ReadDateTimeOffset("DateReceived");
var name = reader.ReadString("Name");
var value = reader.ReadString("Value");
var expires = reader.ReadNullableDateTimeOffset("Expires");
var maxAge = reader.ReadNullableInt32("MaxAge");
var domain = reader.ReadString("Domain");
var path = reader.ReadString("Path");
var secure = reader.ReadBoolean("Secure");
var httpOnly = reader.ReadBoolean("HttpOnly");
var sameSite = reader.ReadNullableEnum<SameSite>("SameSite");
reader.ReadEndObject();

return new FlurlCookie(name, value, originUrl, dateReceived)
{
Expires = expires,
MaxAge = maxAge,
Domain = domain,
Path = path,
Secure = secure,
HttpOnly = httpOnly,
SameSite = sameSite
};
}

public override void Write(Utf8JsonWriter writer, FlurlCookie value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString("OriginUrl", value.OriginUrl);
writer.WriteDateTimeOffset("DateReceived", value.DateReceived);
writer.WriteString("Name", value.Name);
writer.WriteString("Value", value.Value);
writer.WriteNullableDateTimeOffset("Expires", value.Expires);
writer.WriteNullableNumber("MaxAge", value.MaxAge);
writer.WriteString("Domain", value.Domain);
writer.WriteString("Path", value.Path);
writer.WriteBoolean("Secure", value.Secure);
writer.WriteBoolean("HttpOnly", value.HttpOnly);
writer.WriteNullableEnum("SameSite", value.SameSite);
writer.WriteEndObject();
}
}
}
92 changes: 92 additions & 0 deletions Application/Extensions/Utf8JsonReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Globalization;
using System.Text.Json;

namespace BoardGameGeek.Dungeon.Extensions
{
public static class Utf8JsonReaderExtensions
{
public static void CheckStartObject(this ref Utf8JsonReader reader)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}
}

public static void CheckEndObject(this ref Utf8JsonReader reader)
{
if (reader.TokenType != JsonTokenType.EndObject)
{
throw new JsonException();
}
}

public static T GetEnum<T>(this ref Utf8JsonReader reader) where T : struct, Enum => Enum.Parse<T>(reader.GetString()!);

public static DateTime GetDateTime(this ref Utf8JsonReader reader) => DateTime.Parse(reader.GetString()!);

public static DateTimeOffset GetDateTimeOffset(this ref Utf8JsonReader reader) => DateTimeOffset.Parse(reader.GetString()!);

public static TimeSpan GetTimeSpan(this ref Utf8JsonReader reader) => TimeSpan.Parse(reader.GetString()!, CultureInfo.InvariantCulture);

public static bool? GetNullableBoolean(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetBoolean() : (bool?)null;

public static int? GetNullableInt32(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetInt32() : (int?)null;

public static T? GetNullableEnum<T>(this ref Utf8JsonReader reader) where T : struct, Enum => reader.TokenType != JsonTokenType.Null ? reader.GetEnum<T>() : (T?)null;

public static DateTime? GetNullableDateTime(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetDateTime() : (DateTime?)null;

public static DateTimeOffset? GetNullableDateTimeOffset(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetDateTimeOffset() : (DateTimeOffset?)null;

public static TimeSpan? GetNullableTimeSpan(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetTimeSpan() : (TimeSpan?)null;

public static void ReadStartObject(this ref Utf8JsonReader reader)
{
reader.Read();
reader.CheckStartObject();
}

public static void ReadEndObject(this ref Utf8JsonReader reader)
{
reader.Read();
reader.CheckEndObject();
}

public static bool ReadBoolean(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetBoolean();

public static string ReadString(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetString();

public static T ReadEnum<T>(this ref Utf8JsonReader reader, string propertyName) where T : struct, Enum => reader.ReadProperty(propertyName).GetEnum<T>();

public static DateTime ReadDateTime(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetDateTime();

public static DateTimeOffset ReadDateTimeOffset(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetDateTimeOffset();

public static TimeSpan ReadTimeSpan(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetTimeSpan();

public static bool? ReadNullableBoolean(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableBoolean();

public static int? ReadNullableInt32(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableInt32();

public static T? ReadNullableEnum<T>(this ref Utf8JsonReader reader, string propertyName) where T : struct, Enum => reader.ReadProperty(propertyName).GetNullableEnum<T>();

public static DateTime? ReadNullableDateTime(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableDateTime();

public static DateTimeOffset? ReadNullableDateTimeOffset(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableDateTimeOffset();

public static TimeSpan? ReadNullableTimeSpan(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableTimeSpan();

private static ref Utf8JsonReader ReadProperty(this ref Utf8JsonReader reader, string propertyName)
{
reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName || reader.GetString() != propertyName)
{
throw new JsonException();
}
reader.Read();
return ref reader;
}
}
}
41 changes: 41 additions & 0 deletions Application/Extensions/Utf8JsonWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;

namespace BoardGameGeek.Dungeon.Extensions
{
public static class Utf8JsonWriterExtensions
{
public static void WriteEnum<T>(this Utf8JsonWriter writer, string propertyName, T value) where T : struct, Enum => writer.WriteString(propertyName, value.ToString());

public static void WriteDateTime(this Utf8JsonWriter writer, string propertyName, DateTime value) => writer.WriteString(propertyName, value);

public static void WriteDateTimeOffset(this Utf8JsonWriter writer, string propertyName, DateTimeOffset value) => writer.WriteString(propertyName, value);

public static void WriteTimeSpan(this Utf8JsonWriter writer, string propertyName, TimeSpan value) => writer.WriteString(propertyName, value.ToString(null, CultureInfo.InvariantCulture));

public static void WriteNullableBoolean(this Utf8JsonWriter writer, string propertyName, bool? value) => writer.WriteNull(propertyName, value)?.WriteBoolean(propertyName, value!.Value);

public static void WriteNullableNumber(this Utf8JsonWriter writer, string propertyName, int? value) => writer.WriteNull(propertyName, value)?.WriteNumber(propertyName, value!.Value);

public static void WriteNullableEnum<T>(this Utf8JsonWriter writer, string propertyName, T? value) where T : struct, Enum => writer.WriteNull(propertyName, value)?.WriteEnum(propertyName, value!.Value);

public static void WriteNullableDateTime(this Utf8JsonWriter writer, string propertyName, DateTime? value) => writer.WriteNull(propertyName, value)?.WriteDateTime(propertyName, value!.Value);

public static void WriteNullableDateTimeOffset(this Utf8JsonWriter writer, string propertyName, DateTimeOffset? value) => writer.WriteNull(propertyName, value)?.WriteDateTimeOffset(propertyName, value!.Value);

public static void WriteNullableTimeSpan(this Utf8JsonWriter writer, string propertyName, TimeSpan? value) => writer.WriteNull(propertyName, value)?.WriteTimeSpan(propertyName, value!.Value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Utf8JsonWriter WriteNull<T>(this Utf8JsonWriter writer, string propertyName, T? value) where T : struct
{
if (!value.HasValue)
{
writer.WriteNull(propertyName);
return null;
}
return writer;
}
}
}
5 changes: 3 additions & 2 deletions Application/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
using BoardGameGeek.Dungeon.CommandLine;
using Pocket;

namespace BoardGameGeek.Dungeon
{
public class Program
public static class Program
{
private static Task<int> Main(string[] args)
{
Expand All @@ -15,7 +16,7 @@ private static Task<int> Main(string[] args)
Console.WriteLine($"{entry.TimestampUtc.ToLocalTime():HH:mm:ss} {message}");
});

return CommandLine.Bootstrap.Parser.InvokeAsync(args);
return Bootstrap.Parser.InvokeAsync(args);
}
}
}
15 changes: 3 additions & 12 deletions Application/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,11 @@ private static string Players(IEnumerable<Player> players)
return players != null ? string.Join(",", players.OrderByDescending(player => player.Score).Select(player => player.Name)) : string.Empty;
}

private static string Highlight(string text, bool isHighlight = true)
{
return isHighlight ? $"[bgcolor=gold]{text}[/bgcolor]" : text;
}
private static string Highlight(string text, bool isHighlight = true) => isHighlight ? $"[bgcolor=gold]{text}[/bgcolor]" : text;

private static string Pluralize(int count)
{
return count != 1 ? "s" : string.Empty;
}
private static string Pluralize(int count) => count != 1 ? "s" : string.Empty;

private static string Star(int count)
{
return count >= 100 ? ":star:" : count >= 10 ? ":halfstar:" : ":nostar:";
}
private static string Star(int count) => count >= 100 ? ":star:" : count >= 10 ? ":halfstar:" : ":nostar:";

private static string Suffix(Game game)
{
Expand Down
Loading

0 comments on commit b0d04ff

Please sign in to comment.