Skip to content

Commit

Permalink
Dotnet 8 (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Riges committed Aug 17, 2024
1 parent 3daceed commit f1e0cc0
Show file tree
Hide file tree
Showing 36 changed files with 483 additions and 417 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ jobs:
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
global-json-file: global.json
- name: Restore packages
run: dotnet restore
- name: Test
Expand All @@ -43,11 +43,11 @@ jobs:
- name: Get the version
id: vars
run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
global-json-file: global.json
- name: Restore packages
run: dotnet restore
- name: Build and create package nuget
Expand Down
1 change: 0 additions & 1 deletion .idea/.idea.Netatmo/.idea/.name

This file was deleted.

6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.401",
"rollForward": "latestFeature"
}
}
23 changes: 4 additions & 19 deletions src/Netatmo/AirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@

namespace Netatmo;

public class AirClient : IAirClient
public class AirClient(string baseUrl, ICredentialManager credentialManager) : IAirClient
{
private readonly string baseUrl;
private readonly ICredentialManager credentialManager;

public AirClient(string baseUrl, ICredentialManager credentialManager)
{
this.baseUrl = baseUrl;
this.credentialManager = credentialManager;
}

public Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData(string deviceId = null)
{
return baseUrl
.ConfigureRequest(Configuration.ConfigureRequest)
public Task<DataResponse<GetHomeCoachsData>> GetHomeCoachsData(string deviceId = null) =>
baseUrl.ConfigureRequest(Configuration.ConfigureRequest)
.AppendPathSegment("/api/gethomecoachsdata")
.WithOAuthBearerToken(credentialManager.AccessToken)
.PostJsonAsync(new GetHomeCoachsDataRequest
{
DeviceId = deviceId
})
.PostJsonAsync(new GetHomeCoachsDataRequest { DeviceId = deviceId })
.ReceiveJson<DataResponse<GetHomeCoachsData>>();
}
}
18 changes: 8 additions & 10 deletions src/Netatmo/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ public Client(IClock clock, string baseUrl, string clientId, string clientSecret

public Task GenerateToken(string username, string password, Scope[] scopes = null)
{
Console.WriteLine("Client credentials grant type is deprecated since october 2022 and will not work!");return CredentialManager.GenerateToken(username, password, scopes);
Console.WriteLine("Client credentials grant type is deprecated since october 2022 and will not work!");
return CredentialManager.GenerateToken(username, password, scopes);
}

public void ProvideOAuth2Token(string accessToken)
{
CredentialManager.ProvideOAuth2Token(accessToken);
}

public void ProvideOAuth2Token(string accessToken, string refreshToken)
{
CredentialManager.ProvideOAuth2Token(accessToken, refreshToken);
CredentialManager.ProvideOAuth2Token(accessToken);
}

public Task RefreshToken()
public Task RefreshToken() => CredentialManager.RefreshToken();

public void ProvideOAuth2Token(string accessToken, string refreshToken)
{
return CredentialManager.RefreshToken();
CredentialManager.ProvideOAuth2Token(accessToken, refreshToken);
}
}
10 changes: 2 additions & 8 deletions src/Netatmo/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ namespace Netatmo;

public static class Configuration
{
public static JsonSerializerSettings JsonSerializer()
{
return new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter> {new TimestampToInstantConverter(), new StringToDateTimeZoneConverter()}
};
}
public static JsonSerializerSettings JsonSerializer() =>
new() { NullValueHandling = NullValueHandling.Ignore, Converters = [new TimestampToInstantConverter(), new StringToDateTimeZoneConverter()] };

public static void ConfigureRequest(FlurlHttpSettings settings)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Netatmo/Converters/StringToDateTimeZoneConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public override void WriteJson(JsonWriter writer, DateTimeZone value, JsonSerial

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

return DateTimeZoneProviders.Tzdb[reader.Value.ToString()];
}
Expand Down
14 changes: 10 additions & 4 deletions src/Netatmo/Converters/TimestampToInstantConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ public class TimestampToInstantConverter : JsonConverter<Instant?>
{
public override void WriteJson(JsonWriter writer, Instant? value, JsonSerializer serializer)
{
if (value.HasValue) writer.WriteValue(value.Value.ToUnixTimeSeconds().ToString());
if (value.HasValue)
{
writer.WriteValue(value.Value.ToUnixTimeSeconds().ToString());
}
}

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

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

return Instant.FromUnixTimeSeconds(value);
Expand Down
25 changes: 7 additions & 18 deletions src/Netatmo/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,8 @@

namespace Netatmo;

public class CredentialManager : ICredentialManager
public class CredentialManager(string baseUrl, string clientId, string clientSecret, IClock clock) : ICredentialManager
{
private readonly string baseUrl;
private readonly string clientId;
private readonly string clientSecret;
private readonly IClock clock;

public CredentialManager(string baseUrl, string clientId, string clientSecret, IClock clock)
{
this.baseUrl = baseUrl;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.clock = clock;
}

public CredentialToken CredentialToken { get; private set; }
public string AccessToken => CredentialToken?.AccessToken;

Expand All @@ -44,16 +31,18 @@ public async Task GenerateToken(string username, string password, Scope[] scopes

CredentialToken = new CredentialToken(token, clock);
}

public void ProvideOAuth2Token(string accessToken, string refreshToken)
{
CredentialToken = new CredentialToken(new Token(20, accessToken, refreshToken), clock);
}

public void ProvideOAuth2Token(string accessToken)
{
ProvideOAuth2Token(accessToken, null);
}public async Task RefreshToken()
{
ProvideOAuth2Token(accessToken, null);
}

public async Task RefreshToken()
{
// TODO : Handle not success status codes (rate limit exceeded, api down, ect)
var token = await baseUrl.AppendPathSegment("/oauth2/token")
Expand Down
Loading

0 comments on commit f1e0cc0

Please sign in to comment.