Skip to content

Commit

Permalink
Merge pull request #542 from PinguApps/388-create-core-for-databases-api
Browse files Browse the repository at this point in the history
create core for databases api
  • Loading branch information
pingu2k4 authored Nov 23, 2024
2 parents 5145639 + 273f1fa commit 32aad36
Show file tree
Hide file tree
Showing 279 changed files with 15,040 additions and 100 deletions.
1 change: 1 addition & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
close-issue-message: 'Closing issue as it has been marked as stale for 3 days.'
close-pr-message: 'Closing PR as it has been marked as stale for 3 days.'
exempt-all-milestones: true
exempt-pr-labels: 'dependencies'
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
Expand All @@ -17,9 +16,9 @@ public class AccountClient : SessionAwareClientBase, IAccountClient
private readonly IAccountApi _accountApi;
private readonly Config _config;

public AccountClient(IServiceProvider services, Config config)
internal AccountClient(IAccountApi accountApi, Config config)
{
_accountApi = services.GetRequiredService<IAccountApi>();
_accountApi = accountApi;
_config = config;
}

Expand Down
9 changes: 8 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/AppwriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ public class AppwriteClient : IAppwriteClient, ISessionAware
{
/// <inheritdoc/>
public IAccountClient Account { get; }

/// <inheritdoc/>
public ITeamsClient Teams { get; }

public AppwriteClient(IAccountClient accountClient, ITeamsClient teams)
/// <inheritdoc/>
public IDatabasesClient Databases { get; }

public AppwriteClient(IAccountClient accountClient, ITeamsClient teams, IDatabasesClient databasesClient)
{
Account = accountClient;
Teams = teams;
Databases = databasesClient;
}

string? ISessionAware.Session { get; set; }
Expand All @@ -36,5 +42,6 @@ public void SetSession(string? session)
(this as ISessionAware).UpdateSession(session);
(Account as ISessionAware)!.UpdateSession(session);
(Teams as ISessionAware)!.UpdateSession(session);
(Databases as ISessionAware)!.UpdateSession(session);
}
}
40 changes: 40 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/DatabasesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Databases;
using PinguApps.Appwrite.Shared.Responses;

namespace PinguApps.Appwrite.Client.Clients;

/// <inheritdoc/>
public class DatabasesClient : SessionAwareClientBase, IDatabasesClient
{
private readonly IDatabasesApi _databasesApi;

internal DatabasesClient(IDatabasesApi databasesApi)
{
_databasesApi = databasesApi;
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<DocumentsList>> ListDocuments(ListDocumentsRequest request) => throw new NotImplementedException();

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<Document>> CreateDocument(CreateDocumentRequest request) => throw new NotImplementedException();

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult> DeleteDocument(DeleteDocumentRequest request) => throw new NotImplementedException();

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<Document>> GetDocument(GetDocumentRequest request) => throw new NotImplementedException();

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<Document>> UpdateDocument(UpdateDocumentRequest request) => throw new NotImplementedException();
}
17 changes: 16 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/IAppwriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@ public interface IAppwriteClient
void SetSession(string? session);

/// <summary>
/// The sessio of the currently logged in user
/// The session of the currently logged in user
/// </summary>
string? Session { get; }

/// <summary>
/// <para>The Teams service allows you to group users of your project and to enable them to share <see href="https://appwrite.io/docs/advanced/platform/permissions">read and write</see> access to your project resources, such as database documents or storage files.</para>
/// <para>Each user who creates a team becomes the team owner and can delegate the ownership role by inviting a new team member. Only team owners can invite new users to their team.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/teams">Appwrite Docs</see></para>
/// </summary>
ITeamsClient Teams { get; }

/// <summary>
/// <para>The Databases service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access permissions.</para>
/// <para>All data returned by the Databases service are represented as structured JSON documents.</para>
/// <para>The Databases service can contain multiple databases, each database can contain multiple collections. A collection is a group of similarly structured documents. The accepted structure of documents is defined by <see href="https://appwrite.io/docs/products/databases/collections#attributes">collection attributes</see>. The collection attributes help you ensure all your user-submitted data is validated and stored according to the collection structure.</para>
/// <para>Using Appwrite permissions architecture, you can assign read or write access to each collection or document in your project for either a specific user, team, user role, or even grant it with public access (any). You can learn more about <see href="https://appwrite.io/docs/products/databases/permissions">how Appwrite handles permissions and access control</see>.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/databases">Appwrite Docs</see></para>
/// </summary>
IDatabasesClient Databases { get; }
}
32 changes: 32 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IDatabasesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using PinguApps.Appwrite.Shared;
using PinguApps.Appwrite.Shared.Requests.Databases;
using PinguApps.Appwrite.Shared.Responses;

namespace PinguApps.Appwrite.Client.Clients;

/// <summary>
/// <para>The Databases service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access permissions.</para>
/// <para>All data returned by the Databases service are represented as structured JSON documents.</para>
/// <para>The Databases service can contain multiple databases, each database can contain multiple collections. A collection is a group of similarly structured documents. The accepted structure of documents is defined by <see href="https://appwrite.io/docs/products/databases/collections#attributes">collection attributes</see>. The collection attributes help you ensure all your user-submitted data is validated and stored according to the collection structure.</para>
/// <para>Using Appwrite permissions architecture, you can assign read or write access to each collection or document in your project for either a specific user, team, user role, or even grant it with public access (any). You can learn more about <see href="https://appwrite.io/docs/products/databases/permissions">how Appwrite handles permissions and access control</see>.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/databases">Appwrite Docs</see></para>
/// </summary>
public interface IDatabasesClient
{
[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Document>> CreateDocument(CreateDocumentRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult> DeleteDocument(DeleteDocumentRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Document>> GetDocument(GetDocumentRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<DocumentsList>> ListDocuments(ListDocumentsRequest request);

[Obsolete("Endpoint not yet implemented.")]
Task<AppwriteResult<Document>> UpdateDocument(UpdateDocumentRequest request);
}
1 change: 1 addition & 0 deletions src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace PinguApps.Appwrite.Client.Clients;
/// <summary>
/// <para>The Teams service allows you to group users of your project and to enable them to share <see href="https://appwrite.io/docs/advanced/platform/permissions">read and write</see> access to your project resources, such as database documents or storage files.</para>
/// <para>Each user who creates a team becomes the team owner and can delegate the ownership role by inviting a new team member. Only team owners can invite new users to their team.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/teams">Appwrite Docs</see></para>
/// </summary>
public interface ITeamsClient
{
Expand Down
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Internals;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -17,9 +16,9 @@ public class TeamsClient : SessionAwareClientBase, ITeamsClient
private readonly ITeamsApi _teamsApi;
private readonly Config _config;

public TeamsClient(IServiceProvider services, Config config)
internal TeamsClient(ITeamsApi teamsApi, Config config)
{
_teamsApi = services.GetRequiredService<ITeamsApi>();
_teamsApi = teamsApi;
_config = config;
}

Expand Down
27 changes: 27 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IDatabasesApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PinguApps.Appwrite.Shared.Requests.Databases;
using PinguApps.Appwrite.Shared.Responses;
using Refit;

namespace PinguApps.Appwrite.Client.Internals;
internal interface IDatabasesApi : IBaseApi
{
[Get("/databases/{databaseId}/collections/{collectionId}/documents")]
[QueryUriFormat(UriFormat.Unescaped)]
Task<IApiResponse<DocumentsList>> ListDocuments(string databaseId, string collectionId, [Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries);

[Post("/databases/{databaseId}/collections/{collectionId}/documents")]
Task<IApiResponse<Document>> CreateDocument(string databaseId, string collectionId, CreateDocumentRequest request);

[Delete("/databases/{databaseId}/collections/{collectionId}/documents/{documentId}")]
Task<IApiResponse> DeleteDocument(string databaseId, string collectionId, string documentId);

[Get("/databases/{databaseId}/collections/{collectionId}/documents/{documentId}")]
[QueryUriFormat(UriFormat.Unescaped)]
Task<IApiResponse<Document>> GetDocument(string databaseId, string collectionId, string documentId, [Query(CollectionFormat.Multi), AliasAs("queries[]")] IEnumerable<string> queries);

[Patch("/databases/{databaseId}/collections/{collectionId}/documents/{documentId}")]
Task<IApiResponse<Document>> UpdateDocument(string databaseId, string collectionId, string documentId, UpdateDocumentRequest request);
}
48 changes: 44 additions & 4 deletions src/PinguApps.Appwrite.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,28 @@ public static IServiceCollection AddAppwriteClient(this IServiceCollection servi
.AddHttpMessageHandler<HeaderHandler>()
.AddHttpMessageHandler<ClientCookieSessionHandler>();

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<ITeamsClient, TeamsClient>();
services.AddRefitClient<IDatabasesApi>(customRefitSettings)
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.AddHttpMessageHandler<ClientCookieSessionHandler>();

services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
{
var api = sp.GetRequiredService<IDatabasesApi>();
return new DatabasesClient(api);
});
services.AddSingleton<IAppwriteClient, AppwriteClient>();
services.AddSingleton(x => new Lazy<IAppwriteClient>(() => x.GetRequiredService<IAppwriteClient>()));

Expand Down Expand Up @@ -77,8 +97,28 @@ public static IServiceCollection AddAppwriteClientForServer(this IServiceCollect
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient, AccountClient>();
services.AddSingleton<ITeamsClient, TeamsClient>();
services.AddRefitClient<IDatabasesApi>(customRefitSettings)
.ConfigureHttpClient(x => ConfigureHttpClient(x, endpoint))
.AddHttpMessageHandler<HeaderHandler>()
.ConfigurePrimaryHttpMessageHandler(ConfigurePrimaryHttpMessageHandler);

services.AddSingleton<IAccountClient>(sp =>
{
var api = sp.GetRequiredService<IAccountApi>();
var config = sp.GetRequiredService<Config>();
return new AccountClient(api, config);
});
services.AddSingleton<ITeamsClient>(sp =>
{
var api = sp.GetRequiredService<ITeamsApi>();
var config = sp.GetRequiredService<Config>();
return new TeamsClient(api, config);
});
services.AddSingleton<IDatabasesClient>(sp =>
{
var api = sp.GetRequiredService<IDatabasesApi>();
return new DatabasesClient(api);
});
services.AddSingleton<IAppwriteClient, AppwriteClient>();

return services;
Expand Down
67 changes: 46 additions & 21 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Microsoft.Extensions.Configuration;
using PinguApps.Appwrite.Shared.Requests.Teams;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Configuration;
using PinguApps.Appwrite.Shared.Converters;
using PinguApps.Appwrite.Shared.Responses;
using PinguApps.Appwrite.Shared.Responses.Interfaces;
using Attribute = PinguApps.Appwrite.Shared.Responses.Attribute;

namespace PinguApps.Appwrite.Playground;
internal class App
Expand All @@ -17,31 +22,51 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

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

var request = new UpdatePreferencesRequest()
var attributes = new List<Attribute>
{
TeamId = "67142b78001c379958cb",
Preferences = new Dictionary<string, string>()
{
{"key", "value"}
}
new AttributeBoolean("a", "boolean", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, null),
new AttributeDatetime("b", "datetime", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, "datetime", null),
new AttributeEmail("c", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, "email", null),
new AttributeEnum("d", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, ["some", "elements"], "enum", null),
new AttributeFloat("e", "float", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, null, null, null),
new AttributeInteger("f", "integer", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, null, null, null),
new AttributeIp("g", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, "ip", null),
new AttributeRelationship("h", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, "collection", Shared.Enums.RelationType.OneToMany, false, "a string", Shared.Enums.OnDelete.Restrict, Shared.Enums.RelationshipSide.Parent),
new AttributeString("i", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, 128, null),
new AttributeUrl("j", "string", Shared.Enums.DatabaseElementStatus.Available, null, false, false, DateTime.UtcNow, DateTime.UtcNow, "url", null)
};

//var clientResponse = await _client.Teams.UpdatePreferences(request);
var visitor = new AttributeVisitor();

foreach (var attribute in attributes)
{
attribute.Accept(visitor);
}

//Console.WriteLine(clientResponse.Result.Match(
// result => result.ToString(),
// appwriteError => appwriteError.Message,
// internalError => internalError.Message));
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

Console.WriteLine("############################################################################");
options.Converters.Add(new IgnoreSdkExcludedPropertiesConverterFactory());

var serverResponse = await _server.Teams.UpdatePreferences(request);
var json = JsonSerializer.Serialize(attributes[0]);

Console.WriteLine(serverResponse.Result.Match(
result => result.ToString(),
appwriteError => appwriteError.Message,
internalError => internalError.Message));
Console.WriteLine(json);
}
}

internal class AttributeVisitor : IAttributeVisitor
{
public void Visit(AttributeBoolean attribute) => Console.WriteLine($"Boolean attribute Key: {attribute.Key}");
public void Visit(AttributeInteger attribute) => Console.WriteLine($"Integer attribute Key: {attribute.Key}");
public void Visit(AttributeFloat attribute) => Console.WriteLine($"Float attribute Key: {attribute.Key}");
public void Visit(AttributeString attribute) => Console.WriteLine($"String attribute Key: {attribute.Key}");
public void Visit(AttributeEmail attribute) => Console.WriteLine($"Email attribute Key: {attribute.Key}");
public void Visit(AttributeUrl attribute) => Console.WriteLine($"Url attribute Key: {attribute.Key}");
public void Visit(AttributeIp attribute) => Console.WriteLine($"Ip attribute Key: {attribute.Key}");
public void Visit(AttributeDatetime attribute) => Console.WriteLine($"Datetime attribute Key: {attribute.Key}");
public void Visit(AttributeEnum attribute) => Console.WriteLine($"Enum attribute Key: {attribute.Key}");
public void Visit(AttributeRelationship attribute) => Console.WriteLine($"Relationship attribute Key: {attribute.Key}");
}
5 changes: 2 additions & 3 deletions src/PinguApps.Appwrite.Server/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared;
Expand All @@ -16,9 +15,9 @@ public class AccountClient : IAccountClient

private readonly Config _config;

public AccountClient(IServiceProvider services, Config config)
internal AccountClient(IAccountApi accountApi, Config config)
{
_accountApi = services.GetRequiredService<IAccountApi>();
_accountApi = accountApi;
_config = config;
}

Expand Down
11 changes: 10 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/AppwriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
/// <inheritdoc/>
public class AppwriteClient : IAppwriteClient
{
/// <inheritdoc/>
public IAccountClient Account { get; }

/// <inheritdoc/>
public IUsersClient Users { get; }

/// <inheritdoc/>
public ITeamsClient Teams { get; }

public AppwriteClient(IAccountClient accountClient, IUsersClient usersClient, ITeamsClient teamsClient)
/// <inheritdoc/>
public IDatabasesClient Databases { get; }

public AppwriteClient(IAccountClient accountClient, IUsersClient usersClient, ITeamsClient teamsClient, IDatabasesClient databasesClient)
{
Account = accountClient;
Users = usersClient;
Teams = teamsClient;
Databases = databasesClient;
}
}
Loading

0 comments on commit 32aad36

Please sign in to comment.