From 46ce3d658c77d83b136c76dd5d73a63807c0767e Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 16 Jul 2024 03:21:31 +0100 Subject: [PATCH 1/4] Added GetSession --- .../Clients/AccountClient.cs | 15 +++++++++++ .../Clients/IAccountClient.cs | 8 ++++++ .../Internals/IAccountApi.cs | 3 +++ src/PinguApps.Appwrite.Playground/App.cs | 25 +++---------------- src/PinguApps.Appwrite.Playground/Program.cs | 4 +-- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs b/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs index 50010aa1..81d12b05 100644 --- a/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/AccountClient.cs @@ -199,4 +199,19 @@ public async Task> CreateSession(CreateSessionRequest re return e.GetExceptionResponse(); } } + + /// + public async Task> GetSession(string sessionId = "current") + { + try + { + var result = await _accountApi.GetSession(Session, sessionId); + + 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 b2f63233..02fdafb9 100644 --- a/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs @@ -92,4 +92,12 @@ public interface IAccountClient /// The request content /// The session Task> CreateSession(CreateSessionRequest request); + + /// + /// Use this endpoint to get a logged in user's session using a Session ID. Inputting 'current' will return the current session being used + /// Appwrite Docs + /// + /// Session ID. Use the string 'current' to get the current device session + /// The session + Task> GetSession(string sessionId = "current"); } diff --git a/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs b/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs index 6fb39098..74a91453 100644 --- a/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs +++ b/src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs @@ -37,4 +37,7 @@ internal interface IAccountApi : IBaseApi [Post("/account/sessions/token")] Task> CreateSession(CreateSessionRequest request); + + [Get("/account/sessions/{sessionId}")] + Task> GetSession([Header("x-appwrite-session")] string? session, string sessionId); } diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index 6bc62e81..65ce91e2 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -1,7 +1,6 @@ ο»Ώusing Microsoft.Extensions.Configuration; using PinguApps.Appwrite.Client; using PinguApps.Appwrite.Server.Servers; -using PinguApps.Appwrite.Shared.Requests; namespace PinguApps.Appwrite.Playground; internal class App @@ -19,29 +18,11 @@ public App(IAppwriteClient client, IAppwriteServer server, IConfiguration config public async Task Run(string[] args) { - //_client.SetSession(_session); + _client.SetSession(_session); - var request = new CreateSessionRequest - { - UserId = "664aac1a00113f82e620", - Secret = "834938" - }; + Console.WriteLine("Getting Session..."); - Console.WriteLine($"Session: {_client.Session}"); - - var result = await _client.Account.CreateSession(request); - - Console.WriteLine($"Session: {_client.Session}"); - - result.Result.Switch( - account => Console.WriteLine(string.Join(',', account)), - appwriteError => Console.WriteLine(appwriteError.Message), - internalError => Console.WriteLine(internalError.Message) - ); - - Console.WriteLine("Getting Account..."); - - var account = await _client.Account.Get(); + var account = await _client.Account.GetSession("6695d717983fadf6ece1"); Console.WriteLine(account.Result.Match( account => account.ToString(), diff --git a/src/PinguApps.Appwrite.Playground/Program.cs b/src/PinguApps.Appwrite.Playground/Program.cs index 07824411..2f280220 100644 --- a/src/PinguApps.Appwrite.Playground/Program.cs +++ b/src/PinguApps.Appwrite.Playground/Program.cs @@ -7,8 +7,8 @@ var builder = Host.CreateApplicationBuilder(args); -//builder.Services.AddAppwriteClient(builder.Configuration.GetValue("ProjectId")!); -builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue("ProjectId")!); +builder.Services.AddAppwriteClient(builder.Configuration.GetValue("ProjectId")!); +//builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue("ProjectId")!); builder.Services.AddAppwriteServer(builder.Configuration.GetValue("ProjectId")!, builder.Configuration.GetValue("ApiKey")!); builder.Services.AddSingleton(); From f57c22f664f4b3fafd73266063038856931e523a Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 16 Jul 2024 03:21:38 +0100 Subject: [PATCH 2/4] added tests for client --- .../Account/AccountClientTests.GetSession.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetSession.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetSession.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetSession.cs new file mode 100644 index 00000000..c254bc53 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.GetSession.cs @@ -0,0 +1,79 @@ +ο»Ώ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 GetSession_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/sessions/current") + .ExpectedHeaders(true) + .Respond(Constants.AppJson, Constants.UserResponse); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetSession(); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetSession_ShouldRequestSession_WhenSessionProvided() + { + // Arrange + var specificSession = "123456"; + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/sessions/{specificSession}") + .ExpectedHeaders(true) + .Respond(Constants.AppJson, Constants.UserResponse); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetSession(specificSession); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetSession_ShouldHandleException_WhenApiCallFails() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/sessions/current") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetSession(); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task GetSession_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + _mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/sessions/current") + .ExpectedHeaders(true) + .Throw(new HttpRequestException("An error occurred")); + + _appwriteClient.SetSession(Constants.Session); + + // Act + var result = await _appwriteClient.Account.GetSession(); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From 7767d9857f1a8b77dee0a4a6a1798c6e3820a798 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 16 Jul 2024 03:22:39 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6b5a6bb9..f89f794e 100644 --- a/README.md +++ b/README.md @@ -139,11 +139,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## βŒ› Progress ### Server & Client -![12 / 298](https://progress-bar.dev/12/?scale=298&suffix=%20/%20298&width=500) +![13 / 298](https://progress-bar.dev/13/?scale=298&suffix=%20/%20298&width=500) ### Server Only ![2 / 195](https://progress-bar.dev/2/?scale=195&suffix=%20/%20195&width=300) ### Client Only -![10 / 93](https://progress-bar.dev/10/?scale=93&suffix=%20/%2093&width=300) +![11 / 93](https://progress-bar.dev/11/?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 -![12 / 52](https://progress-bar.dev/12/?scale=52&suffix=%20/%2052&width=120) +![13 / 52](https://progress-bar.dev/13/?scale=52&suffix=%20/%2052&width=120) | Endpoint | Client | Server | |:-:|:-:|:-:| @@ -189,7 +189,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Create OAuth2 Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createOAuth2Session) | ⬛ | ❌ | | [Update Phone Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePhoneSession) | ⬛ | ❌ | | [Create Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createSession) | βœ… | ❌ | -| [Get Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#getSession) | ⬛ | ❌ | +| [Get Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#getSession) | βœ… | ❌ | | [Update Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateSession) | ⬛ | ❌ | | [Delete Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteSession) | ⬛ | ❌ | | [Update Status](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateStatus) | ⬛ | ❌ | From de2527633b5145c535d2a214620ee928c0c76bb6 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 16 Jul 2024 03:37:02 +0100 Subject: [PATCH 4/4] fixed total on total progress bar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f89f794e..bd799bba 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## βŒ› Progress ### Server & Client -![13 / 298](https://progress-bar.dev/13/?scale=298&suffix=%20/%20298&width=500) +![13 / 288](https://progress-bar.dev/13/?scale=288&suffix=%20/%20288&width=500) ### Server Only ![2 / 195](https://progress-bar.dev/2/?scale=195&suffix=%20/%20195&width=300) ### Client Only