Skip to content

Commit

Permalink
Merge pull request #31 from PinguApps/26-add-client-createaccount-end…
Browse files Browse the repository at this point in the history
…point

26 add client CreateAccount endpoint
  • Loading branch information
pingu2k4 authored Jul 4, 2024
2 parents 3b1b528 + 12d206d commit 55074d7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ This is a work in progress. There are 2 SDK's - one for client and another for s

## Progress
### Server & Client
![2 / 298](https://progress-bar.dev/2/?scale=298&suffix=%20/%20298&width=500)
![3 / 298](https://progress-bar.dev/3/?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
![1 / 93](https://progress-bar.dev/1/?scale=93&suffix=%20/%2093&width=300)
![2 / 93](https://progress-bar.dev/2/?scale=93&suffix=%20/%2093&width=300)

## Key
| Icon | Definition |
Expand All @@ -19,12 +19,12 @@ This is a work in progress. There are 2 SDK's - one for client and another for s
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

## Account
![2 / 52](https://progress-bar.dev/2/?scale=52&suffix=%20/%2052&width=120)
![3 / 52](https://progress-bar.dev/3/?scale=52&suffix=%20/%2052&width=120)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
| [Get Account](https://appwrite.io/docs/references/1.5.x/client-rest/account#get) |||
| [Create Account](https://appwrite.io/docs/references/1.5.x/client-rest/account#create) | ||
| [Create Account](https://appwrite.io/docs/references/1.5.x/client-rest/account#create) | ||
| [Update Email](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateEmail) |||
| [List Identities](https://appwrite.io/docs/references/1.5.x/client-rest/account#listIdentities) |||
| [Delete Identity](https://appwrite.io/docs/references/1.5.x/client-rest/account#deleteIdentity) |||
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 @@ -4,6 +4,7 @@
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Responses;

namespace PinguApps.Appwrite.Client;
Expand Down Expand Up @@ -44,4 +45,18 @@ public async Task<AppwriteResult<User>> Get()
return e.GetExceptionResponse<User>();
}
}

public async Task<AppwriteResult<User>> Create(CreateAccountRequest request)
{
try
{
var result = await _accountApi.CreateAccount(request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<User>();
}
}
}
2 changes: 2 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Threading.Tasks;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Responses;

namespace PinguApps.Appwrite.Client;
public interface IAccountClient
{
Task<AppwriteResult<User>> Create(CreateAccountRequest request);
Task<AppwriteResult<User>> Get();
}
4 changes: 4 additions & 0 deletions 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 PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Responses;
using Refit;

Expand All @@ -8,4 +9,7 @@ public interface IAccountApi : IBaseApi
{
[Get("/account")]
Task<IApiResponse<User>> GetAccount([Header("x-appwrite-session")] string? session);

[Post("/account")]
Task<IApiResponse<User>> CreateAccount(CreateAccountRequest request);
}
27 changes: 17 additions & 10 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -20,21 +21,27 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var result = await _client.Account.Get();
//var result = await _client.Account.Get();

//result.Result.Switch(
// account => Console.WriteLine(account.Email),
// appwriteError => Console.WriteLine(appwriteError.Message),
// internalError => Console.WriteLine(internalError.Message)
//);

var request = new CreateAccountRequest
{
Email = "[email protected]",
Password = "ThisIsMyPassword",
Name = "Two Names"
};

var result = await _client.Account.Create(request);

result.Result.Switch(
account => Console.WriteLine(account.Email),
appwriteError => Console.WriteLine(appwriteError.Message),
internalError => Console.WriteLine(internalError.Message)
);

//var request = new CreateAccountRequest
//{
// Email = "[email protected]",
// Password = "ThisIsMyPassword",
// Name = "Two Names"
//};

//var result = await _server.Account.Create(request);
}
}
48 changes: 48 additions & 0 deletions tests/PinguApps.Appwrite.Client.Tests/AccountTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Tests.Shared;
using Refit;
using RichardSzalay.MockHttp;
Expand Down Expand Up @@ -59,6 +60,53 @@ public async Task Get_ShouldHandleException_WhenApiCallFails()
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task Create_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateAccountRequest()
{
Email = "[email protected]",
Password = "password",
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);

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

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

[Fact]
public async Task Create_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateAccountRequest()
{
Email = "[email protected]",
Password = "password",
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

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

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

public static class AccountTestsExtensions
Expand Down

0 comments on commit 55074d7

Please sign in to comment.