Skip to content

Commit

Permalink
Merge pull request #8 from LuccaSA/fix.slack.notifications
Browse files Browse the repository at this point in the history
bump dependencies and switch back to newtonsoft
  • Loading branch information
rducom authored Jul 13, 2022
2 parents 2779229 + a87196b commit a33d6a4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
41 changes: 27 additions & 14 deletions src/SqlBackupTools/Notification/SlackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@

namespace SqlBackupTools.Notification
{
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(SlackMessage))]
[JsonSerializable(typeof(SlackResponse))]
internal partial class SlackJsonContext : JsonSerializerContext
{

}

public class SlackClient
{
private readonly ILogger _logger;
private static string _slackUri = "https://slack.com";

private static JsonSerializerOptions _opt = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public SlackClient(ILogger logger)
{
_logger = logger;
Expand All @@ -34,19 +29,32 @@ public async Task<SlackResponse> SendSlackMessageAsync(SlackMessage message, str
var client = new HttpClient { BaseAddress = new Uri(_slackUri) };
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", slackSecret);
var json = JsonSerializer.Serialize(message, SlackJsonContext.Default.SlackMessage);

var json = JsonSerializer.Serialize(message, _opt);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var ok = await client.PostAsync("api/chat.postMessage", content);
if (!ok.IsSuccessStatusCode)
{
throw new NotificationException(ok.StatusCode.ToString());
}
var response = JsonSerializer.Deserialize<SlackResponse>(await ok.Content.ReadAsStringAsync(), SlackJsonContext.Default.SlackResponse);
if (!response.Ok)

var resp = await ok.Content.ReadAsStringAsync();
SlackResponse response;
try
{
throw new NotificationException(response.Error);
response = JsonSerializer.Deserialize<SlackResponse>(resp, _opt);
if (!response.Ok)
{
throw new NotificationException(response.Error);
}
}
catch (Exception ee)
{
_logger.Error(ee, json);
_logger.Error(ee, resp);
throw;
}

return response;
}
catch (Exception e)
Expand Down Expand Up @@ -113,6 +121,8 @@ public static string ToSlackColor(this AlertLevel alertLevel)
}
}



public class SlackResponse
{
public bool Ok { get; set; }
Expand All @@ -132,4 +142,7 @@ public class Message
public string Subtype { get; set; }
public string Ts { get; set; }
}

// ------------------------

}
8 changes: 4 additions & 4 deletions src/SqlBackupTools/SqlBackupTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="MailKit" Version="3.1.1" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="MailKit" Version="3.3.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions tests/SqlBackupTools.Tests/SqlBackupTools.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit a33d6a4

Please sign in to comment.