Skip to content

Commit

Permalink
Merge pull request #66 from PinguApps/40-get-account-preferences
Browse files Browse the repository at this point in the history
Added get account preferences
  • Loading branch information
pingu2k4 authored Jul 10, 2024
2 parents 62d4c69 + 099cb38 commit 1a976f4
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 8 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
![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 |
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
![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 |
|:-:|:-:|:-:|
Expand All @@ -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) |||
Expand Down
16 changes: 16 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Client.Internals;
Expand Down Expand Up @@ -131,4 +132,19 @@ public async Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest request)
return e.GetExceptionResponse<User>();
}
}

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

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<IReadOnlyDictionary<string, string>>();
}
}
}
10 changes: 9 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -59,4 +60,11 @@ public interface IAccountClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest request);

/// <summary>
/// Get the preferences as a dictionary for the currently logged in user
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#getPrefs">Appwrite Docs</see></para>
/// </summary>
/// <returns>A dictionary of the user preferences</returns>
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetAccountPreferences();
}
6 changes: 5 additions & 1 deletion src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,4 +25,7 @@ public interface IAccountApi : IBaseApi

[Patch("/account/phone")]
Task<IApiResponse<User>> UpdatePhone([Header("x-appwrite-session")] string? session, UpdatePhoneRequest name);

[Get("/account/prefs")]
Task<IApiResponse<IReadOnlyDictionary<string, string>>> GetAccountPreferences([Header("x-appwrite-session")] string? session);
}
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/PinguApps.Appwrite.Shared.Tests/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
""";
}

0 comments on commit 1a976f4

Please sign in to comment.