Skip to content

Commit

Permalink
Upgrade Flurl to v4.0.2 (#171)
Browse files Browse the repository at this point in the history
**Summary:**
This pull request upgrades the Flurl library used in the project and
includes necessary adjustments to ensure compatibility with the new
version.
Because Flurl v4 move to `System.text.Json` we need to change here the
serializer.
  • Loading branch information
Riges authored Aug 25, 2024
1 parent 0faec4d commit 95cba90
Show file tree
Hide file tree
Showing 67 changed files with 415 additions and 429 deletions.
1 change: 0 additions & 1 deletion .idea/.idea.Netatmo/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Netatmo/AirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Netatmo;
public class AirClient(string baseUrl, ICredentialManager credentialManager) : IAirClient
{
public Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData(string deviceId = null) =>
baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/gethomecoachsdata")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new GetHomeCoachsDataRequest { DeviceId = deviceId })
Expand Down
13 changes: 6 additions & 7 deletions src/Netatmo/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Flurl.Http.Configuration;
using Netatmo.Converters;
using Newtonsoft.Json;

namespace Netatmo;

public static class Configuration
{
public static JsonSerializerSettings JsonSerializer() =>
new() { NullValueHandling = NullValueHandling.Ignore, Converters = [new TimestampToInstantConverter(), new StringToDateTimeZoneConverter()] };
public static JsonSerializerOptions JsonSerializerOptions =>
new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, Converters = { new TimestampToInstantConverter(), new StringToDateTimeZoneConverter() } };

public static void ConfigureRequest(FlurlHttpSettings settings)
{
var jsonSettings = JsonSerializer();

//jsonSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
settings.JsonSerializer = new NewtonsoftJsonSerializer(jsonSettings);
// something like maybe miss : jsonSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
settings.JsonSerializer = new DefaultJsonSerializer(JsonSerializerOptions);
}
}
20 changes: 5 additions & 15 deletions src/Netatmo/Converters/StringToDateTimeZoneConverter.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
using Flurl.Util;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using NodaTime;

namespace Netatmo.Converters;

public class StringToDateTimeZoneConverter : JsonConverter<DateTimeZone>
{
public override void WriteJson(JsonWriter writer, DateTimeZone value, JsonSerializer serializer)
{
writer.WriteValue(value?.ToInvariantString());
}
public override DateTimeZone Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
DateTimeZoneProviders.Tzdb.GetZoneOrNull(reader.GetString()!);

public override DateTimeZone ReadJson(JsonReader reader, Type objectType, DateTimeZone existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value == null)
{
return null;
}

return DateTimeZoneProviders.Tzdb[reader.Value.ToString()];
}
public override void Write(Utf8JsonWriter writer, DateTimeZone value, JsonSerializerOptions options) => writer.WriteStringValue(value?.Id);
}
25 changes: 5 additions & 20 deletions src/Netatmo/Converters/TimestampToInstantConverter.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using NodaTime;

namespace Netatmo.Converters;

public class TimestampToInstantConverter : JsonConverter<Instant?>
public class TimestampToInstantConverter : JsonConverter<Instant>
{
public override void WriteJson(JsonWriter writer, Instant? value, JsonSerializer serializer)
{
if (value.HasValue)
{
writer.WriteValue(value.Value.ToUnixTimeSeconds().ToString());
}
}
public override Instant Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => Instant.FromUnixTimeSeconds(reader.GetInt64());

public override Instant? ReadJson(JsonReader reader, Type objectType, Instant? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value == null)
{
return null;
}

var value = long.Parse(reader.Value.ToString());

return Instant.FromUnixTimeSeconds(value);
}
public override void Write(Utf8JsonWriter writer, Instant value, JsonSerializerOptions options) => writer.WriteNumberValue(value.ToUnixTimeSeconds());
}
17 changes: 17 additions & 0 deletions src/Netatmo/Converters/ZoneIdConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Netatmo.Converters;

public class ZoneIdConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.String => reader.GetString(),
JsonTokenType.Number => reader.GetInt32().ToString(),
_ => throw new JsonException("Zone.Id must be a string or an integer.")
};

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => writer.WriteNumberValue(int.Parse(value));
}
44 changes: 22 additions & 22 deletions src/Netatmo/EnergyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ namespace Netatmo;
public class EnergyClient(string baseUrl, ICredentialManager credentialManager) : IEnergyClient
{
public Task<DataResponse<GetHomesDataBody>> GetHomesData(string homeId = null, string gatewayTypes = null) =>
baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/homesdata")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new GetHomesDataRequest { HomeId = homeId, GatewayTypes = gatewayTypes })
.ReceiveJson<DataResponse<GetHomesDataBody>>();

public async Task<DataResponse<GetHomeStatusBody>> GetHomeStatus(string homeId, string[] deviceTypes = null) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/homestatus")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new GetHomeStatusRequest { HomeId = homeId, DeviceTypes = deviceTypes })
.ReceiveJson<DataResponse<GetHomeStatusBody>>();

public async Task<DataResponse> SetThermMode(string homeId, string mode, Instant? endTime = null) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/setthermmode")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new SetThermModeRequest { HomeId = homeId, Mode = mode, EndTime = endTime })
.ReceiveJson<DataResponse>();

public async Task<DataResponse> SetRoomThermPoint(string homeId, string roomId, string mode, double? temp = null, Instant? endTime = null) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/setroomthermpoint")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(
Expand All @@ -49,55 +49,55 @@ public async Task<DataResponse<T[]>> GetRoomMeasure<T>(GetRoomMeasureParameters
{
ValidateGetRoomMeasureParameters<T>(parameters);

return await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
return await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/getroommeasure")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(
new GetRoomMeasureRequest
.SetQueryParams(
new
{
HomeId = parameters.HomeId,
RoomId = parameters.RoomId,
Scale = parameters.Scale.Value,
Type = parameters.Type.Value,
BeginAt = parameters.BeginAt,
EndAt = parameters.EndAt,
Limit = parameters.Limit,
Optimize = parameters.Optimize,
RealTime = parameters.RealTime
home_id = parameters.HomeId,
room_id = parameters.RoomId,
scale = parameters.Scale.Value,
type = parameters.Type.Value,
date_begin = parameters.BeginAt?.ToUnixTimeSeconds(),
date_end = parameters.EndAt?.ToUnixTimeSeconds(),
limit = parameters.Limit,
optimize = parameters.Optimize,
real_time = parameters.RealTime
})
.ReceiveJson<DataResponse<T[]>>();
.GetJsonAsync<DataResponse<T[]>>();
}

public async Task<DataResponse> SwitchHomeSchedule(string homeId, string scheduleId) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/switchhomeschedule")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new SwitchHomeScheduleRequest { HomeId = homeId, ScheduleId = scheduleId })
.ReceiveJson<DataResponse>();

public async Task<DataResponse> RenameHomeSchedule(string homeId, string scheduleId, string name) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/renamehomeschedule")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new RenameHomeScheduleRequest { HomeId = homeId, ScheduleId = scheduleId, Name = name })
.ReceiveJson<DataResponse>();

public async Task<DataResponse> DeleteHomeSchedule(string homeId, string scheduleId) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/deletehomeschedule")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new DeleteHomeScheduleRequest { HomeId = homeId, ScheduleId = scheduleId })
.ReceiveJson<DataResponse>();

public async Task<DataResponse> SyncHomeSchedule(SyncHomeScheduleRequest requestParameters) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/synchomeschedule")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(requestParameters)
.ReceiveJson<DataResponse>();

public async Task<CreateHomeScheduleResponse> CreateHomeSchedule(CreateHomeScheduleRequest requestParameters) =>
await baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
await baseUrl.WithSettings(Configuration.ConfigureRequest)
.AppendPathSegment("/api/createnewhomeschedule")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(requestParameters)
Expand Down
6 changes: 3 additions & 3 deletions src/Netatmo/Models/Client/Air/GetHomeCoachsData.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Text.Json.Serialization;
using Netatmo.Models.Client.Air.HomesCoachs;
using Newtonsoft.Json;

namespace Netatmo.Models.Client.Air;

public class GetHomeCoachsData
{
[JsonProperty("devices")]
[JsonPropertyName("devices")]
public Devices[] Devices { get; set; }

[JsonProperty("user")]
[JsonPropertyName("user")]
public User User { get; set; }
}
26 changes: 13 additions & 13 deletions src/Netatmo/Models/Client/Air/HomesCoachs/DashBoardData.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using NodaTime;

namespace Netatmo.Models.Client.Air.HomesCoachs;

public class DashBoardData
{
[JsonProperty("time_utc")]
[JsonPropertyName("time_utc")]
public Instant TimeUtc { get; set; }

[JsonProperty("Temperature")]
[JsonPropertyName("Temperature")]
public double Temperature { get; set; }

[JsonProperty("CO2")]
[JsonPropertyName("CO2")]
public int CO2 { get; set; }

[JsonProperty("Humidity")]
[JsonPropertyName("Humidity")]
public int HumidityPercent { get; set; }

[JsonProperty("Noise")]
[JsonPropertyName("Noise")]
public double Noise { get; set; }

[JsonProperty("Pressure")]
[JsonPropertyName("Pressure")]
public double Pressure { get; set; }

[JsonProperty("AbsolutePressure")]
[JsonPropertyName("AbsolutePressure")]
public double AbsolutePressure { get; set; }

[JsonProperty("health_idx")]
[JsonPropertyName("health_idx")]
public HealthIdx HealthIdx { get; set; }

[JsonProperty("min_temp")]
[JsonPropertyName("min_temp")]
public decimal MinTemp { get; set; }

[JsonProperty("max_temp")]
[JsonPropertyName("max_temp")]
public decimal MaxTemp { get; set; }

[JsonProperty("date_min_temp")]
[JsonPropertyName("date_min_temp")]
public Instant DateMinTemp { get; set; }

[JsonProperty("date_max_temp")]
[JsonPropertyName("date_max_temp")]
public Instant DateMaxTemp { get; set; }
}
34 changes: 17 additions & 17 deletions src/Netatmo/Models/Client/Air/HomesCoachs/Devices.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
using System.Text.Json.Serialization;
using Netatmo.Enums;
using Newtonsoft.Json;
using NodaTime;

namespace Netatmo.Models.Client.Air.HomesCoachs;

public class Devices
{
[JsonProperty("_id")]
[JsonPropertyName("_id")]
public string Id { get; set; }

[JsonProperty("cipher_id")]
[JsonPropertyName("cipher_id")]
public string CipherId { get; set; }

[JsonProperty("last_status_store")]
[JsonPropertyName("last_status_store")]
public Instant LastStatusStore { get; set; }

[JsonProperty("place")]
[JsonPropertyName("place")]
public Place Place { get; set; }

[JsonProperty("type")]
[JsonPropertyName("type")]
public string Type { get; set; }

[JsonProperty("dashboard_data")]
[JsonPropertyName("dashboard_data")]
public DashBoardData DashboardData { get; set; }

[JsonProperty("data_type")]
[JsonPropertyName("data_type")]
public string[] DataType { get; set; }

[JsonProperty("co2_calibrating")]
[JsonPropertyName("co2_calibrating")]
public bool Co2Calibrating { get; set; }

[JsonProperty("reachable")]
[JsonPropertyName("reachable")]
public bool Reachable { get; set; }

[JsonProperty("date_setup")]
[JsonPropertyName("date_setup")]
public Instant DateSetup { get; set; }

[JsonProperty("last_setup")]
[JsonPropertyName("last_setup")]
public Instant LastSetup { get; set; }

[JsonProperty("module_name")]
[JsonPropertyName("module_name")]
public string ModuleName { get; set; }

[JsonProperty("firmware")]
[JsonPropertyName("firmware")]
public int Firmware { get; set; }

[JsonProperty("last_upgrade")]
[JsonPropertyName("last_upgrade")]
public Instant LastUpgrade { get; set; }

[JsonProperty("station_name")]
[JsonPropertyName("station_name")]
public string Name { get; set; }

[JsonProperty("wifi_status")]
[JsonPropertyName("wifi_status")]
public int WifiStatus { get; set; }

public WifiStrengthEnum WifiStrength
Expand Down
Loading

0 comments on commit 95cba90

Please sign in to comment.