Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented create anonymous session #133

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
<!-- ![32 / 288](https://progress-bar.dev/32/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 32 / 288](https://img.shields.io/badge/Server_&_Client-32%20%2F%20288-red?style=for-the-badge)
<!-- ![33 / 288](https://progress-bar.dev/33/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 33 / 288](https://img.shields.io/badge/Server_&_Client-33%20%2F%20288-red?style=for-the-badge)

<!-- ![2 / 195](https://progress-bar.dev/2/?scale=195&suffix=%20/%20195&width=300) -->
![Server - 2 / 195](https://img.shields.io/badge/Server-2%20%2F%20195-red?style=for-the-badge)

<!-- ![30 / 93](https://progress-bar.dev/30/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 30 / 93](https://img.shields.io/badge/Client-30%20%2F%2093-red?style=for-the-badge)
<!-- ![31 / 93](https://progress-bar.dev/31/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 31 / 93](https://img.shields.io/badge/Client-31%20%2F%2093-red?style=for-the-badge)

### 🔑 Key
| Icon | Definition |
Expand All @@ -155,8 +155,8 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| ❌ | There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
<!-- ![32 / 52](https://progress-bar.dev/32/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 32 / 52](https://img.shields.io/badge/Account-32%20%2F%2052-yellow?style=for-the-badge)
<!-- ![33 / 52](https://progress-bar.dev/33/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 33 / 52](https://img.shields.io/badge/Account-33%20%2F%2052-yellow?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -186,7 +186,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Password Recovery (Confirmation)](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateRecovery) | ✅ | ❌ |
| [List Sessions](https://appwrite.io/docs/references/1.5.x/client-rest/account#listSessions) | ✅ | ❌ |
| [Delete Sessions](https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteSessions) | ✅ | ❌ |
| [Create Anonymous Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createAnonymousSession) | | ❌ |
| [Create Anonymous Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createAnonymousSession) | | ❌ |
| [Create Email Password Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createEmailPasswordSession) | ⬛ | ❌ |
| [Update Magic URL Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMagicURLSession) | ⬛ | ❌ |
| [Create OAuth2 Session](https://appwrite.io/docs/references/1.5.x/client-rest/account#createOAuth2Session) | ⬛ | ❌ |
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 @@ -529,4 +529,19 @@ public async Task<AppwriteResult> DeleteSessions()
return e.GetExceptionResponse();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<Session>> CreateAnonymousSession()
{
try
{
var result = await _accountApi.CreateAnonymousSession();

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 @@ -245,7 +245,15 @@ public interface IAccountClient

/// <summary>
/// Delete all sessions from the user account and remove any sessions cookies from the end client
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteSessions">Appwrite Docs</see></para>
/// </summary>
/// <returns>The result</returns>
Task<AppwriteResult> DeleteSessions();

/// <summary>
/// Use this endpoint to allow a new user to register an anonymous account in your project. This route will also create a new session for the user. To allow the new user to convert an anonymous account to a normal account, you need to call <see cref="UpdateEmail(UpdateEmailRequest)"/> or create an OAuth2 session
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#createAnonymousSession">Appwrite Docs</see></para>
/// </summary>
/// <returns>The Session</returns>
Task<AppwriteResult<Session>> CreateAnonymousSession();
}
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 @@ -98,4 +98,7 @@ internal interface IAccountApi : IBaseApi

[Delete("/account/sessions")]
Task<IApiResponse> DeleteSessions([Header("x-appwrite-session")] string session);

[Post("/account/sessions/anonymous")]
Task<IApiResponse<Session>> CreateAnonymousSession();
}
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public App(IAppwriteClient client, IAppwriteServer server, IConfiguration config

public async Task Run(string[] args)
{
_client.SetSession(_session);
//_client.SetSession(_session);

Console.WriteLine(_client.Session);

var response = await _client.Account.DeleteSessions();
var response = await _client.Account.CreateAnonymousSession();

Console.WriteLine(response.Result.Match(
account => account.ToString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 CreateAnonymousSession_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous")
.ExpectedHeaders(false)
.Respond(Constants.AppJson, Constants.SessionResponse);

// Act
var result = await _appwriteClient.Account.CreateAnonymousSession();

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task CreateAnonymousSession_ShouldHandleException_WhenApiCallFails()
{
// Arrange
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous")
.ExpectedHeaders(false)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Account.CreateAnonymousSession();

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task CreateAnonymousSession_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/anonymous")
.ExpectedHeaders(false)
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Account.CreateAnonymousSession();

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}