Skip to content

Commit 24ea9d3

Browse files
committed
Update icon and add readme to package
1 parent 93ee3ff commit 24ea9d3

20 files changed

+4482
-27
lines changed

README.md

+26-24
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,62 @@ Appwrite is an open-source backend as a service server that abstract and simplif
1717
Add this reference to your project's `.csproj` file:
1818

1919
```xml
20-
<PackageReference Include="Appwrite" Version="0.4.0" />
20+
<PackageReference Include="Appwrite" Version="0.4.1" />
2121
```
2222

2323
You can install packages from the command line:
2424

2525
```powershell
2626
# Package Manager
27-
Install-Package Appwrite -Version 0.4.0
27+
Install-Package Appwrite -Version 0.4.1
2828
2929
# or .NET CLI
30-
dotnet add package Appwrite --version 0.4.0
30+
dotnet add package Appwrite --version 0.4.1
3131
```
3232

3333

3434

3535
## Getting Started
3636

3737
### Initialize & Make API Request
38-
Once you have installed the package, it is extremely easy to get started with the SDK; all you need to do is import the package in your code, set your Appwrite credentials, and start making API calls. Below is a simple example:
38+
Once you add the dependencies, its extremely easy to get started with the SDK; All you need to do is import the package in your code, set your Appwrite credentials, and start making API calls. Below is a simple example:
3939

4040
```csharp
4141
using Appwrite;
4242

43-
var client = new Client()
44-
.SetEndpoint("http://cloud.appwrite.io/v1") // Make sure your endpoint is accessible
45-
.SetProject("5ff3379a01d25") // Your project ID
46-
.SetKey("cd868db89") // Your secret API key
47-
.SetSelfSigned(); // Use only on dev mode with a self-signed SSL cert
43+
static async Task Main(string[] args)
44+
{
45+
var client = Client();
4846

49-
var users = new Users(client);
47+
client
48+
.setEndpoint('http://[HOSTNAME_OR_IP]/v1') // Make sure your endpoint is accessible
49+
.setProject('5ff3379a01d25') // Your project ID
50+
.setKey('cd868c7af8bdc893b4...93b7535db89')
51+
.setSelfSigned() // Use only on dev mode with a self-signed SSL cert
52+
;
5053

51-
var user = await users.Create(
52-
userId: ID.Unique(),
53-
54-
password: "password",
55-
name: "name");
54+
var users = Users(client);
5655

57-
Console.WriteLine(user.ToMap());
56+
try {
57+
var user = await users.Create(ID.Unique(), '[email protected]', 'password', 'name');
58+
Console.WriteLine(user.ToMap());
59+
} catch (AppwriteException e) {
60+
Console.WriteLine(e.Message);
61+
}
62+
}
5863
```
5964

6065
### Error Handling
61-
The Appwrite .NET SDK raises an `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.
66+
The Appwrite .NET SDK raises `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.
6267

6368
```csharp
64-
var users = new Users(client);
69+
var users = Users(client);
6570

6671
try {
67-
var user = await users.Create(
68-
userId: ID.Unique(),
69-
70-
password: "password",
71-
name: "name");
72+
var user = await users.Create(ID.Unique(), '[email protected]', 'password', 'name');
73+
Console.WriteLine(user.ToMap());
7274
} catch (AppwriteException e) {
73-
Console.WriteLine(e.Message);
75+
Console.WriteLine(e.Message);
7476
}
7577
```
7678

icon.png

47.6 KB
Loading

io/appwrite/src/Appwrite/Client.cs

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Net.Http.Headers;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace Appwrite
13+
{
14+
public class Client
15+
{
16+
private readonly HttpClient http;
17+
private readonly Dictionary<string, string> headers;
18+
private readonly Dictionary<string, string> config;
19+
private string endPoint;
20+
private bool selfSigned;
21+
22+
public Client() : this("https://appwrite.io/v1", false, new HttpClient())
23+
{
24+
}
25+
26+
public Client(string endPoint, bool selfSigned, HttpClient http)
27+
{
28+
this.endPoint = endPoint;
29+
this.selfSigned = selfSigned;
30+
this.headers = new Dictionary<string, string>()
31+
{
32+
{ "content-type", "application/json" },
33+
{ "x-sdk-name", ".NET" },
34+
{ "x-sdk-platform", "server" },
35+
{ "x-sdk-language", "dotnet" },
36+
{ "x-sdk-version", "0.4.1"},
37+
{ "X-Appwrite-Response-Format", "1.0.0" }
38+
};
39+
this.config = new Dictionary<string, string>();
40+
this.http = http;
41+
}
42+
43+
public Client SetSelfSigned(bool selfSigned)
44+
{
45+
this.selfSigned = selfSigned;
46+
return this;
47+
}
48+
49+
public Client SetEndPoint(string endPoint)
50+
{
51+
this.endPoint = endPoint;
52+
return this;
53+
}
54+
55+
public string GetEndPoint()
56+
{
57+
return endPoint;
58+
}
59+
60+
public Dictionary<string, string> GetConfig()
61+
{
62+
return config;
63+
}
64+
65+
/// <summary>Your project ID</summary>
66+
public Client SetProject(string value) {
67+
config.Add("project", value);
68+
AddHeader("X-Appwrite-Project", value);
69+
return this;
70+
}
71+
72+
/// <summary>Your secret API key</summary>
73+
public Client SetKey(string value) {
74+
config.Add("key", value);
75+
AddHeader("X-Appwrite-Key", value);
76+
return this;
77+
}
78+
79+
/// <summary>Your secret JSON Web Token</summary>
80+
public Client SetJWT(string value) {
81+
config.Add("jWT", value);
82+
AddHeader("X-Appwrite-JWT", value);
83+
return this;
84+
}
85+
86+
public Client SetLocale(string value) {
87+
config.Add("locale", value);
88+
AddHeader("X-Appwrite-Locale", value);
89+
return this;
90+
}
91+
92+
public Client AddHeader(String key, String value)
93+
{
94+
headers.Add(key, value);
95+
return this;
96+
}
97+
98+
public async Task<HttpResponseMessage> Call(string method, string path, Dictionary<string, string> headers, Dictionary<string, object> parameters)
99+
{
100+
if (selfSigned)
101+
{
102+
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
103+
}
104+
105+
bool methodGet = "GET".Equals(method, StringComparison.InvariantCultureIgnoreCase);
106+
107+
string queryString = methodGet ? "?" + parameters.ToQueryString() : string.Empty;
108+
109+
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), endPoint + path + queryString);
110+
111+
if ("multipart/form-data".Equals(headers["content-type"], StringComparison.InvariantCultureIgnoreCase))
112+
{
113+
MultipartFormDataContent form = new MultipartFormDataContent();
114+
115+
foreach (var parameter in parameters)
116+
{
117+
if (parameter.Key == "file")
118+
{
119+
FileInfo fi = parameters["file"] as FileInfo;
120+
121+
var file = File.ReadAllBytes(fi.FullName);
122+
123+
form.Add(new ByteArrayContent(file, 0, file.Length), "file", fi.Name);
124+
}
125+
else if (parameter.Value is IEnumerable<object>)
126+
{
127+
List<object> list = new List<object>((IEnumerable<object>) parameter.Value);
128+
for (int index = 0; index < list.Count; index++)
129+
{
130+
form.Add(new StringContent(list[index].ToString()), $"{parameter.Key}[{index}]");
131+
}
132+
}
133+
else
134+
{
135+
form.Add(new StringContent(parameter.Value.ToString()), parameter.Key);
136+
}
137+
}
138+
request.Content = form;
139+
140+
}
141+
else if (!methodGet)
142+
{
143+
string body = parameters.ToJson();
144+
145+
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
146+
}
147+
148+
foreach (var header in this.headers)
149+
{
150+
if (header.Key.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
151+
{
152+
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value));
153+
}
154+
else
155+
{
156+
if (http.DefaultRequestHeaders.Contains(header.Key)) {
157+
http.DefaultRequestHeaders.Remove(header.Key);
158+
}
159+
http.DefaultRequestHeaders.Add(header.Key, header.Value);
160+
}
161+
}
162+
163+
foreach (var header in headers)
164+
{
165+
if (header.Key.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
166+
{
167+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value));
168+
}
169+
else
170+
{
171+
if (request.Headers.Contains(header.Key)) {
172+
request.Headers.Remove(header.Key);
173+
}
174+
request.Headers.Add(header.Key, header.Value);
175+
}
176+
}
177+
try
178+
{
179+
var httpResponseMessage = await http.SendAsync(request);
180+
var code = (int) httpResponseMessage.StatusCode;
181+
var response = await httpResponseMessage.Content.ReadAsStringAsync();
182+
183+
if (code >= 400) {
184+
var message = response.ToString();
185+
var isJson = httpResponseMessage.Content.Headers.GetValues("Content-Type").FirstOrDefault().Contains("application/json");
186+
187+
if (isJson) {
188+
message = (JObject.Parse(message))["message"].ToString();
189+
}
190+
191+
throw new AppwriteException(message, code, response.ToString());
192+
}
193+
194+
return httpResponseMessage;
195+
}
196+
catch (System.Exception e)
197+
{
198+
throw new AppwriteException(e.Message, e);
199+
}
200+
201+
}
202+
}
203+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
using Newtonsoft.Json.Serialization;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Appwrite
8+
{
9+
public static class ExtensionMethods
10+
{
11+
public static string ToJson(this Dictionary<string, object> dict)
12+
{
13+
var settings = new JsonSerializerSettings
14+
{
15+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
16+
Converters = new List<JsonConverter> { new StringEnumConverter() }
17+
};
18+
19+
return JsonConvert.SerializeObject(dict, settings);
20+
}
21+
22+
public static string ToQueryString(this Dictionary<string, object> parameters)
23+
{
24+
List<string> query = new List<string>();
25+
26+
foreach (KeyValuePair<string, object> parameter in parameters)
27+
{
28+
if (parameter.Value != null)
29+
{
30+
if (parameter.Value is List<object>)
31+
{
32+
foreach(object entry in (dynamic) parameter.Value)
33+
{
34+
query.Add(parameter.Key + "[]=" + Uri.EscapeUriString(entry.ToString()));
35+
}
36+
}
37+
else
38+
{
39+
query.Add(parameter.Key + "=" + Uri.EscapeUriString(parameter.Value.ToString()));
40+
}
41+
}
42+
}
43+
return string.Join("&", query);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Appwrite
4+
{
5+
public class AppwriteException : Exception
6+
{
7+
public int? Code;
8+
public string Response = null;
9+
public AppwriteException(string message = null, int? code = null, string response = null)
10+
: base(message)
11+
{
12+
this.Code = code;
13+
this.Response = response;
14+
}
15+
public AppwriteException(string message, Exception inner)
16+
: base(message, inner)
17+
{
18+
}
19+
}
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Appwrite
2+
{
3+
public enum OrderType
4+
{
5+
ASC,
6+
DESC
7+
}
8+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Appwrite
6+
{
7+
public class Rule
8+
{
9+
public string Label { get; set; }
10+
public string Key { get; set; }
11+
public string Type { get; set; }
12+
public string Default { get; set; }
13+
public bool Required { get; set; }
14+
public bool Array { get; set; }
15+
}
16+
}

0 commit comments

Comments
 (0)