Skip to content

Update playground examples #13

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

Merged
merged 7 commits into from
Jul 5, 2023
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
265 changes: 167 additions & 98 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,117 +1,107 @@
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;
using Newtonsoft.Json;

namespace playground_for_dotnet
{
class Program
public class Program
{
static void Main(string[] args)
public static async Task Main(string[] args)
{
Client client = new Client();
client.SetEndPoint("[ENDPOINT]");
client.SetEndpoint("[ENDPOINT]");
client.SetProject("[PROJECT_ID]");
client.SetKey("[API_KEY]");
client.SetSelfSigned(true);

string response;
string collection;
JObject parsed;

Database database = new Database(client);
Databases databases = new Databases(client);
Storage storage = new Storage(client);
Functions functions = new Functions(client);
Users users = new Users(client);

/**
Create User
*/
try
{
Console.WriteLine("Running Create Users API");
RunTask(users.Create($"{DateTime.Now.ToFileTime()}@example.com", "*******", "Lorem Ipsum")).GetAwaiter().GetResult();
Console.WriteLine("Done");
}
catch (System.Exception e)
{
Console.WriteLine($"Error: {e}");
throw;
}
Database database;
Collection collection;
Bucket bucket;

/**
List Documents
Create Database
*/
try
{
Console.WriteLine("Running List Documents API");
response = RunTask(users.List()).GetAwaiter().GetResult();
parsed = JObject.Parse(response);
foreach (dynamic element in parsed["users"])
{
Console.WriteLine($"- {element["name"]} ({element["email"]})");
}
Console.WriteLine("Running Create Database API");
database = await databases.Create(
databaseId: ID.Unique(),
name: "MoviesDB"
);
Console.WriteLine("Done");
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}

/**
Create Collection
*/
List<object> perms = new List<object>() {"*"};
List<object> rules = new List<object>();

Rule ruleName = new Rule();
ruleName.Label = "Name";
ruleName.Key = "name";
ruleName.Type = "text";
ruleName.Default = "Empty Name";
ruleName.Required = true;
ruleName.Array = false;
rules.Add(ruleName);

Rule ruleYear = new Rule();
ruleYear.Label = "Release Year";
ruleYear.Key = "release_year";
ruleYear.Type = "numeric";
ruleYear.Default = "1970";
ruleYear.Required = true;
ruleYear.Array = false;
rules.Add(ruleYear);

try
{
Console.WriteLine("Running Create Collection API");
response = RunTask(database.CreateCollection("Movies", perms, perms, rules)).GetAwaiter().GetResult();
parsed = JObject.Parse(response);
collection = (string) parsed["$id"];
collection = await databases.CreateCollection(
databaseId: database.Id,
collectionId: ID.Unique(),
name: "Movies",
permissions: new List<string> { Permission.Read(Role.Any()), Permission.Write(Role.Any()) }
);

Console.WriteLine("Creating Attribute \"name\"");

await databases.CreateStringAttribute(
databaseId: database.Id,
collectionId: collection.Id,
key: "name",
size: 255,
required: true
);

Console.WriteLine("Creating Attribute \"release_year\"");

await databases.CreateIntegerAttribute(
databaseId: database.Id,
collectionId: collection.Id,
key: "release_year",
required: true
);

Console.WriteLine("Done");
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}

/**
List Collection
List Collections
*/
try
{
Console.WriteLine("Running List Collection API");
response = RunTask(database.ListCollections()).GetAwaiter().GetResult();
parsed = JObject.Parse(response);
foreach (dynamic element in parsed["collections"])
var collectionsList = await databases.ListCollections(
databaseId: database.Id
);
foreach (var element in collectionsList.Collections)
{
Console.WriteLine($"- {element["name"]}");
Console.WriteLine($"- {element.Name}");
}
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}

Expand All @@ -123,13 +113,23 @@ Add Document
try
{
Console.WriteLine("Running Create Documents API");
RunTask(database.CreateDocument(collection, movie1, perms, perms)).GetAwaiter().GetResult();
RunTask(database.CreateDocument(collection, movie2, perms, perms)).GetAwaiter().GetResult();
await databases.CreateDocument(
databaseId: database.Id,
collectionId: collection.Id,
documentId: ID.Unique(),
data: movie1
);
await databases.CreateDocument(
databaseId: database.Id,
collectionId: collection.Id,
documentId: ID.Unique(),
data: movie2
);
Console.WriteLine("Done");
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}

Expand All @@ -139,55 +139,124 @@ List Documents
try
{
Console.WriteLine("Running List Documents API");
response = RunTask(database.ListDocuments(collection)).GetAwaiter().GetResult();
parsed = JObject.Parse(response);
foreach (dynamic element in parsed["documents"])
var documentsList = await databases.ListDocuments(
databaseId: database.Id,
collectionId: collection.Id
);
foreach (var element in documentsList.Documents)
{
Console.WriteLine($"- {element["name"]} ({element["release_year"]})");
var movie = JsonConvert.DeserializeObject<Movie>(JsonConvert.SerializeObject(element.Data));
Console.WriteLine($"- {movie.Name} ({movie.ReleaseYear})");
}
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}

/**
List Functions
Create Bucket
*/
Functions functions = new Functions(client);


try
{
Console.WriteLine("Running List Functions API");
response = RunTask(functions.List()).GetAwaiter().GetResult();
parsed = JObject.Parse(response);
foreach (dynamic element in parsed["functions"])
{
Console.WriteLine($"- {element["name"]} ({element["env"]})");
}
Console.WriteLine("Running Create Bucket API");

bucket = await storage.CreateBucket(
bucketId: ID.Unique(),
name: "Files",
permissions: new List<string> { Permission.Read(Role.Any()), Permission.Write(Role.Any()) }
);

Console.WriteLine("Done");
}
catch (System.Exception e)
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e}");
Console.WriteLine($"Error: {e.Message}");
throw;
}
}

static async Task<string> RunTask(Task<HttpResponseMessage> task)
{
HttpResponseMessage response = await task;
return await response.Content.ReadAsStringAsync();
/**
Create File
*/

try
{
Console.WriteLine("Running Create File API");

var file = await storage.CreateFile(
bucketId: bucket.Id,
fileId: ID.Unique(),
file: InputFile.FromPath("[DIRECTORY_PATH]/appwrite-overview.png"),
permissions: new List<string> { Permission.Read(Role.Any()), Permission.Write(Role.Any()) }
);

Console.WriteLine("Done");
}
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e.Message}");
throw;
}

/**
Create Function Execution
*/

try
{
Console.WriteLine("Running Create Function Execution API");

var execution = await functions.CreateExecution(
functionId: "[FUNCTION_ID]"
);

Console.WriteLine($"Response Message: {execution.Response}");

Console.WriteLine("Done");
}
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e.Message}");
throw;
}

/**
Create User
*/

try
{
Console.WriteLine("Running Create User API");

var user = await users.Create(
userId: ID.Unique(),
email: "[email protected]",
password: "test12345",
name: "Walter O' Brian"
);

Console.WriteLine("Done");
}
catch (AppwriteException e)
{
Console.WriteLine($"Error: {e.Message}");
throw;
}
}
}
public class Movie {
public class Movie
{
public Movie(string name, int releaseYear)
{
Name = name;
release_year = releaseYear;
ReleaseYear = releaseYear;
}
[JsonProperty("name")]
public string Name { get; }
public int release_year { get; }
[JsonProperty("release_year")]
public int ReleaseYear { get; }

}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Restore all project dependencies:
```sh
dotnet restore
```
Replace `[ENDPOINT]`, `[PROJECT_ID]` and `[API_KEY]` with your credentials in *Program.cs*.
Replace `[ENDPOINT]`, `[PROJECT_ID]` and `[API_KEY]` with your credentials, `[FUNCTION_ID]` with the id of any Appwrite function you would like to test and `[DIRECTORY_PATH]` with the path of your project directory in *Program.cs*.

To run your Playground, run following command:

Expand Down
Binary file added appwrite-overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions playground-for-dotnet.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>playground_for_dotnet</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appwrite" Version="0.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Appwrite" Version="0.4.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be 0.3.0 as that will be the next version

<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
</Project>