Skip to content

Commit

Permalink
Merge pull request #75 from PinguApps/43-feat-account-get-session-imp…
Browse files Browse the repository at this point in the history
…lementation

Added get session implementation
  • Loading branch information
pingu2k4 authored Jul 16, 2024
2 parents 490e315 + de25276 commit aedb1c0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 / 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
![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 |
Expand All @@ -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 |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -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) |||
Expand Down
15 changes: 15 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,19 @@ public async Task<AppwriteResult<Session>> CreateSession(CreateSessionRequest re
return e.GetExceptionResponse<Session>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<Session>> GetSession(string sessionId = "current")
{
try
{
var result = await _accountApi.GetSession(Session, sessionId);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<Session>();
}
}
}
8 changes: 8 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public interface IAccountClient
/// <param name="request">The request content</param>
/// <returns>The session</returns>
Task<AppwriteResult<Session>> CreateSession(CreateSessionRequest request);

/// <summary>
/// Use this endpoint to get a logged in user's session using a Session ID. Inputting 'current' will return the current session being used
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#getSession">Appwrite Docs</see></para>
/// </summary>
/// <param name="sessionId">Session ID. Use the string 'current' to get the current device session</param>
/// <returns>The session</returns>
Task<AppwriteResult<Session>> GetSession(string sessionId = "current");
}
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ internal interface IAccountApi : IBaseApi

[Post("/account/sessions/token")]
Task<IApiResponse<Session>> CreateSession(CreateSessionRequest request);

[Get("/account/sessions/{sessionId}")]
Task<IApiResponse<Session>> GetSession([Header("x-appwrite-session")] string? session, string sessionId);
}
25 changes: 3 additions & 22 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

var builder = Host.CreateApplicationBuilder(args);

//builder.Services.AddAppwriteClient(builder.Configuration.GetValue<string>("ProjectId")!);
builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue<string>("ProjectId")!);
builder.Services.AddAppwriteClient(builder.Configuration.GetValue<string>("ProjectId")!);
//builder.Services.AddAppwriteClientForServer(builder.Configuration.GetValue<string>("ProjectId")!);
builder.Services.AddAppwriteServer(builder.Configuration.GetValue<string>("ProjectId")!, builder.Configuration.GetValue<string>("ApiKey")!);
builder.Services.AddSingleton<App>();

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit aedb1c0

Please sign in to comment.