From c9d02f4c89f4d5622b248f71c178ccb8ea83aefe Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 10 Jul 2024 03:42:58 +0100 Subject: [PATCH 1/3] Implemented Get Account Prefs --- .../Clients/AccountClient.cs | 16 ++++++++++++++++ .../Clients/IAccountClient.cs | 10 +++++++++- .../Internals/IAccountApi.cs | 6 +++++- src/PinguApps.Appwrite.Playground/App.cs | 4 ++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs b/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs index 46e7339c..84e9a266 100644 --- a/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using PinguApps.Appwrite.Client.Clients; using PinguApps.Appwrite.Client.Internals; @@ -131,4 +132,19 @@ public async Task> UpdatePhone(UpdatePhoneRequest request) return e.GetExceptionResponse(); } } + + /// + public async Task>> GetAccountPreferences() + { + try + { + var result = await _accountApi.GetAccountPreferences(Session); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse>(); + } + } } diff --git a/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs b/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs index 190a6400..ec81f8d3 100644 --- a/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using PinguApps.Appwrite.Shared; using PinguApps.Appwrite.Shared.Requests; using PinguApps.Appwrite.Shared.Responses; @@ -59,4 +60,11 @@ public interface IAccountClient /// The request content /// The user Task> UpdatePhone(UpdatePhoneRequest request); + + /// + /// Get the preferences as a dictionary for the currently logged in user + /// Appwrite Docs + /// + /// A dictionary of the user preferences + Task>> GetAccountPreferences(); } diff --git a/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs b/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs index dfe51941..39fd4cd9 100644 --- a/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs +++ b/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using PinguApps.Appwrite.Shared.Requests; using PinguApps.Appwrite.Shared.Responses; using Refit; @@ -24,4 +25,7 @@ public interface IAccountApi : IBaseApi [Patch("/account/phone")] Task> UpdatePhone([Header("x-appwrite-session")] string? session, UpdatePhoneRequest name); + + [Get("/account/prefs")] + Task>> GetAccountPreferences([Header("x-appwrite-session")] string? session); } diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 7bf51bd8..ee0955a1 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -29,10 +29,10 @@ public async Task Run(string[] args) var f = request.IsValid(); - var result = await _client.Account.UpdatePhone(request); + var result = await _client.Account.GetAccountPreferences(); result.Result.Switch( - account => Console.WriteLine(account.Email), + account => Console.WriteLine(string.Join(',', account)), appwriteError => Console.WriteLine(appwriteError.Message), internalError => Console.WriteLine(internalError.Message) ); From 87cb993002362f43d5b3fe7bff6656b5434beb01 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 10 Jul 2024 03:48:58 +0100 Subject: [PATCH 2/3] added client tests --- ...ccountClientTests.GetAccountPreferences.cs | 61 +++++++++++++++++++ .../Constants.cs | 7 +++ 2 files changed, 68 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetAccountPreferences.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetAccountPreferences.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetAccountPreferences.cs new file mode 100644 index 00000000..51f3c482 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetAccountPreferences.cs @@ -0,0 +1,61 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Tests; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Client.Tests.Clients.Account; +public partial class AccountClientTests +{ + [Fact] + public async Task GetAccountPreferences_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/prefs") + .ExpectedHeaders(true) + .Respond(Constants.AppJson, Constants.PreferencesResponse); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetAccountPreferences(); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetAccountPreferences_ShouldHandleException_WhenApiCallFails() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/prefs") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetAccountPreferences(); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task GetAccountPreferences_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/prefs") + .ExpectedHeaders(true) + .Throw(new HttpRequestException("An error occurred")); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetAccountPreferences(); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} diff --git a/tests/PinguApps.Appwrite.Shared.Tests/Constants.cs b/tests/PinguApps.Appwrite.Shared.Tests/Constants.cs index 1fc5715f..34ea4d11 100644 --- a/tests/PinguApps.Appwrite.Shared.Tests/Constants.cs +++ b/tests/PinguApps.Appwrite.Shared.Tests/Constants.cs @@ -72,4 +72,11 @@ public static class Constants "accessedAt": "2020-10-15T06:38:00.000+00:00" } """; + + public const string PreferencesResponse = """ + { + "abc": "123", + "def": "456" + } + """; } From 099cb38e1837d1d77e4a4cf0057983f630fcc36c Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Wed, 10 Jul 2024 03:49:53 +0100 Subject: [PATCH 3/3] updated readme with progress --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e4485e6b..102d038f 100644 --- a/README.md +++ b/README.md @@ -139,11 +139,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## ⌛ Progress ### Server & Client -![7 / 298](https://progress-bar.dev/7/?scale=298&suffix=%20/%20298&width=500) +![8 / 298](https://progress-bar.dev/8/?scale=298&suffix=%20/%20298&width=500) ### Server Only ![1 / 195](https://progress-bar.dev/1/?scale=195&suffix=%20/%20195&width=300) ### Client Only -![6 / 93](https://progress-bar.dev/6/?scale=93&suffix=%20/%2093&width=300) +![7 / 93](https://progress-bar.dev/7/?scale=93&suffix=%20/%2093&width=300) ### 🔑 Key | Icon | Definition | @@ -153,7 +153,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | ❌ | There is currently no intention to implement the endpoint for the given SDK type (client or server) | ### Account -![7 / 52](https://progress-bar.dev/7/?scale=52&suffix=%20/%2052&width=120) +![8 / 52](https://progress-bar.dev/8/?scale=52&suffix=%20/%2052&width=120) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -177,7 +177,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Update Name](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateName) | ✅ | ❌ | | [Update Password](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePassword) | ✅ | ❌ | | [Update Phone](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePhone) | ✅ | ❌ | -| [Get Account Preferences](https://appwrite.io/docs/references/1.5.x/client-rest/account#getPrefs) | ⬛ | ❌ | +| [Get Account Preferences](https://appwrite.io/docs/references/1.5.x/client-rest/account#getPrefs) | ✅ | ❌ | | [Update Preferences](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePrefs) | ⬛ | ❌ | | [Create Password Recovery](https://appwrite.io/docs/references/1.5.x/client-rest/account#createRecovery) | ⬛ | ❌ | | [Create Password Recovery (Confirmation)](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateRecovery) | ⬛ | ❌ |