From d279b491c448744e16f969dc804ee676a35b1be7 Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Tue, 6 Apr 2021 00:16:35 +0200 Subject: [PATCH] Add analyzers and fix bad code Fixes #297 --- Directory.Build.props | 10 +++++----- TMDbLib/.editorconfig | 4 ++++ TMDbLib/Client/TMDbClient.cs | 6 +++--- TMDbLib/Client/TMDbClientChanges.cs | 2 +- TMDbLib/Client/TMDbClientPeople.cs | 2 +- TMDbLib/Rest/RestRequest.cs | 5 +++-- TMDbLib/TMDbLib.csproj | 9 +++++++++ TMDbLibTests/ClientTests.cs | 8 ++++---- TestApplication/Program.cs | 2 +- 9 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 TMDbLib/.editorconfig diff --git a/Directory.Build.props b/Directory.Build.props index c188d79b..a5e46bc4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,7 +3,7 @@ latest - + LordMike @@ -21,17 +21,17 @@ portable true snupkg - + v - + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - + - + true diff --git a/TMDbLib/.editorconfig b/TMDbLib/.editorconfig new file mode 100644 index 00000000..4540e5d8 --- /dev/null +++ b/TMDbLib/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CAC001: ConfigureAwaitChecker +dotnet_diagnostic.CAC001.severity = error \ No newline at end of file diff --git a/TMDbLib/Client/TMDbClient.cs b/TMDbLib/Client/TMDbClient.cs index ee3a9ae0..eaee6e15 100644 --- a/TMDbLib/Client/TMDbClient.cs +++ b/TMDbLib/Client/TMDbClient.cs @@ -158,7 +158,7 @@ private void AddSessionId(RestRequest req, SessionType targetType = SessionType. public async Task GetConfigAsync() { - TMDbConfig config = await _client.Create("configuration").GetOfT(CancellationToken.None); + TMDbConfig config = await _client.Create("configuration").GetOfT(CancellationToken.None).ConfigureAwait(false); if (config == null) throw new Exception("Unable to retrieve configuration"); @@ -180,10 +180,10 @@ public async Task GetImageBytes(string size, string filePath, bool useSs { Uri url = GetImageUrl(size, filePath, useSsl); - HttpResponseMessage response = await _client.HttpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead, token); + using HttpResponseMessage response = await _client.HttpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false); response.EnsureSuccessStatusCode(); - return await response.Content.ReadAsByteArrayAsync(); + return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); } private void Initialize(string baseUrl, bool useSsl, string apiKey) diff --git a/TMDbLib/Client/TMDbClientChanges.cs b/TMDbLib/Client/TMDbClientChanges.cs index 0973fbbd..56d4cc80 100644 --- a/TMDbLib/Client/TMDbClientChanges.cs +++ b/TMDbLib/Client/TMDbClientChanges.cs @@ -84,7 +84,7 @@ public async Task> GetTvChangesAsync(int page = public async Task> GetMovieChangesAsync(int movieId, int page = 0, DateTime? startDate = null, DateTime? endDate = null, CancellationToken cancellationToken = default) { - ChangesContainer changesContainer = await GetChangesInternal("movie", page, movieId, startDate, endDate, cancellationToken); + ChangesContainer changesContainer = await GetChangesInternal("movie", page, movieId, startDate, endDate, cancellationToken).ConfigureAwait(false); return changesContainer.Changes; } diff --git a/TMDbLib/Client/TMDbClientPeople.cs b/TMDbLib/Client/TMDbClientPeople.cs index 8853b122..9b40f3f2 100644 --- a/TMDbLib/Client/TMDbClientPeople.cs +++ b/TMDbLib/Client/TMDbClientPeople.cs @@ -56,7 +56,7 @@ public async Task GetLatestPersonAsync(CancellationToken cancellationTok public async Task GetPersonAsync(int personId, PersonMethods extraMethods = PersonMethods.Undefined, CancellationToken cancellationToken = default) { - return await GetPersonAsync(personId, DefaultLanguage, extraMethods, cancellationToken); + return await GetPersonAsync(personId, DefaultLanguage, extraMethods, cancellationToken).ConfigureAwait(false); } public async Task GetPersonAsync(int personId, string language, PersonMethods extraMethods = PersonMethods.Undefined, CancellationToken cancellationToken = default) diff --git a/TMDbLib/Rest/RestRequest.cs b/TMDbLib/Rest/RestRequest.cs index ec3e38c5..81784e7c 100644 --- a/TMDbLib/Rest/RestRequest.cs +++ b/TMDbLib/Rest/RestRequest.cs @@ -159,7 +159,8 @@ private HttpRequestMessage PrepRequest(HttpMethod method) // Body if (method == HttpMethod.Post && _bodyObj != null) { - MemoryStream ms = new MemoryStream(); + using MemoryStream ms = new MemoryStream(); + using (StreamWriter sw = new StreamWriter(ms, _client.Encoding, 4096, true)) using (JsonTextWriter tw = new JsonTextWriter(sw)) { @@ -199,7 +200,7 @@ private async Task SendInternal(HttpMethod method, Cancella return resp; if (isJson) - statusMessage = JsonConvert.DeserializeObject(await resp.Content.ReadAsStringAsync()); + statusMessage = JsonConvert.DeserializeObject(await resp.Content.ReadAsStringAsync().ConfigureAwait(false)); else statusMessage = null; diff --git a/TMDbLib/TMDbLib.csproj b/TMDbLib/TMDbLib.csproj index c587118e..10613c67 100644 --- a/TMDbLib/TMDbLib.csproj +++ b/TMDbLib/TMDbLib.csproj @@ -8,6 +8,15 @@ .NET Client library for The Movie Database (https://www.themoviedb.org/) + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + diff --git a/TMDbLibTests/ClientTests.cs b/TMDbLibTests/ClientTests.cs index f60e64b1..dda27029 100644 --- a/TMDbLibTests/ClientTests.cs +++ b/TMDbLibTests/ClientTests.cs @@ -59,16 +59,16 @@ public void SetConfigTest() [Fact] public async Task ClientConstructorUrlTest() { - TMDbClient clientA = new TMDbClient(TestConfig.APIKey, false, "http://api.themoviedb.org") { MaxRetryCount = 2 }; + using TMDbClient clientA = new TMDbClient(TestConfig.APIKey, false, "http://api.themoviedb.org") { MaxRetryCount = 2 }; await clientA.GetConfigAsync(); - TMDbClient clientB = new TMDbClient(TestConfig.APIKey, true, "http://api.themoviedb.org") { MaxRetryCount = 2 }; + using TMDbClient clientB = new TMDbClient(TestConfig.APIKey, true, "http://api.themoviedb.org") { MaxRetryCount = 2 }; await clientB.GetConfigAsync(); - TMDbClient clientC = new TMDbClient(TestConfig.APIKey, false, "https://api.themoviedb.org") { MaxRetryCount = 2 }; + using TMDbClient clientC = new TMDbClient(TestConfig.APIKey, false, "https://api.themoviedb.org") { MaxRetryCount = 2 }; await clientC.GetConfigAsync(); - TMDbClient clientD = new TMDbClient(TestConfig.APIKey, true, "https://api.themoviedb.org") { MaxRetryCount = 2 }; + using TMDbClient clientD = new TMDbClient(TestConfig.APIKey, true, "https://api.themoviedb.org") { MaxRetryCount = 2 }; await clientD.GetConfigAsync(); } diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index 3bfb1c6c..a735e4ea 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -18,7 +18,7 @@ private static async Task Main(string[] args) { // Instantiate a new client, all that's needed is an API key, but it's possible to // also specify if SSL should be used, and if another server address should be used. - TMDbClient client = new TMDbClient("c6b31d1cdad6a56a23f0c913e2482a31"); + using TMDbClient client = new TMDbClient("c6b31d1cdad6a56a23f0c913e2482a31"); // We need the config from TMDb in case we want to get stuff like images // The config needs to be fetched for each new client we create, but we can cache it to a file (as in this example).