Skip to content

Commit

Permalink
Implemented update status (#157)
Browse files Browse the repository at this point in the history
* Implemented update status

* added client tests

* Update README.md
  • Loading branch information
pingu2k4 authored Sep 16, 2024
1 parent d966892 commit 6df1534
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 23 deletions.
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
<!-- ![39 / 293](https://progress-bar.dev/39/?scale=293&suffix=%20/%20288&width=500) -->
![Server & Client - 39 / 293](https://img.shields.io/badge/Server_&_Client-39%20%2F%20293-red?style=for-the-badge)
<!-- ![10 / 293](https://progress-bar.dev/40/?scale=293&suffix=%20/%20288&width=500) -->
![Server & Client - 40 / 293](https://img.shields.io/badge/Server_&_Client-40%20%2F%20293-red?style=for-the-badge)

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

<!-- ![34 / 93](https://progress-bar.dev/34/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 34 / 93](https://img.shields.io/badge/Client-34%20%2F%2093-gold?style=for-the-badge)
<!-- ![35 / 93](https://progress-bar.dev/35/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 35 / 93](https://img.shields.io/badge/Client-35%20%2F%2093-gold?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
<!-- ![39 / 57](https://progress-bar.dev/39/?scale=57&suffix=%20/%2052&width=120) -->
![Account - 39 / 57](https://img.shields.io/badge/Account-39%20%2F%2057-forestgreen?style=for-the-badge)
<!-- ![40 / 57](https://progress-bar.dev/40/?scale=57&suffix=%20/%2052&width=120) -->
![Account - 40 / 57](https://img.shields.io/badge/Account-40%20%2F%2057-forestgreen?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -195,7 +195,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Get Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#getSession) |||
| [Update Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#updateSession) |||
| [Delete Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#deleteSession) |||
| [Update Status](https://appwrite.io/docs/references/1.6.x/client-rest/account#updateStatus) | ||
| [Update Status](https://appwrite.io/docs/references/1.6.x/client-rest/account#updateStatus) | ||
| [Create Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#createPushTarget) |||
| [Update Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePushTarget) |||
| [Delete Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#deletePushTarget) |||
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 @@ -597,4 +597,19 @@ public async Task<AppwriteResult> DeleteSession(DeleteSessionRequest request)
return e.GetExceptionResponse();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<User>> UpdateStatus()
{
try
{
var result = await _accountApi.UpdateStatus(GetCurrentSessionOrThrow());

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<User>();
}
}
}
6 changes: 6 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,10 @@ public interface IAccountClient
/// <param name="request">The request content</param>
/// <returns>code 204 for success</returns>
Task<AppwriteResult> DeleteSession(DeleteSessionRequest request);

/// <summary>
/// Block the currently logged in user account. Behind the scene, the user record is not deleted but permanently blocked from any access. To completely delete a user, use the Users API instead.
/// </summary>
/// <returns>The User</returns>
Task<AppwriteResult<User>> UpdateStatus();
}
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 @@ -107,4 +107,7 @@ internal interface IAccountApi : IBaseApi

[Delete("/account/sessions/{sessionId}")]
Task<IApiResponse> DeleteSession([Header("x-appwrite-session")] string session, string sessionId);

[Patch("/account/status")]
Task<IApiResponse<User>> UpdateStatus([Header("x-appwrite-session")] string session);
}
17 changes: 1 addition & 16 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 @@ -21,25 +20,11 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var response = await _client.Account.Get();
var response = await _client.Account.UpdateStatus();

Console.WriteLine(response.Result.Match(
account => account.ToString(),
appwriteError => appwriteError.Message,
internalERror => internalERror.Message));

var r2 = await _client.Account.DeleteSession(new DeleteSessionRequest());

Console.WriteLine(r2.Result.Match(
account => account.ToString(),
appwriteError => appwriteError.Message,
internalERror => internalERror.Message));

var r3 = await _client.Account.Get();

Console.WriteLine(r3.Result.Match(
account => account.ToString(),
appwriteError => appwriteError.Message,
internalERror => internalERror.Message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Net;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task UpdateStatus_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/status")
.ExpectedHeaders(true)
.Respond(Constants.AppJson, Constants.UserResponse);

_appwriteClient.SetSession(Constants.Session);

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

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

[Fact]
public async Task UpdateStatus_ShouldReturnError_WhenSessionIsNull()
{
// Act
var result = await _appwriteClient.Account.UpdateStatus();

// Assert
Assert.True(result.IsError);
Assert.True(result.IsInternalError);
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message);
}

[Fact]
public async Task UpdateStatus_ShouldHandleException_WhenApiCallFails()
{
// Arrange
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/status")
.ExpectedHeaders(true)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);

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

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

[Fact]
public async Task UpdateStatus_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/status")
.ExpectedHeaders(true)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);

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

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

0 comments on commit 6df1534

Please sign in to comment.