-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e77740
Update .NET and SDK version
adityaoberai 6dc4541
Add create attribute functions
adityaoberai 7752348
Add create bucket and create file API examples
adityaoberai a530b7a
Add create function execution and create user API examples
adityaoberai d2b2439
Fix database examples
adityaoberai d0b5ef1
Update error handling blocks to have AppwriteException
adityaoberai 7db9820
Bump up Appwrite SDK version
adityaoberai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; } | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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