Skip to content

Commit 12a6a4b

Browse files
authored
Merge pull request #131 from appwrite/feat-dotnet-exceptions
Feat .NET Exceptions
2 parents b101f1f + af7bf3c commit 12a6a4b

File tree

11 files changed

+151
-79
lines changed

11 files changed

+151
-79
lines changed

src/SDK/Language/DotNet.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ public function getFiles()
293293
'template' => '/dotnet/LICENSE.twig',
294294
'minify' => false,
295295
],
296+
[
297+
'scope' => 'default',
298+
'destination' => '.travis.yml',
299+
'template' => '/dotnet/.travis.yml.twig',
300+
'minify' => false,
301+
],
296302
[
297303
'scope' => 'method',
298304
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
@@ -340,7 +346,12 @@ public function getFiles()
340346
'template' => '/dotnet/src/Appwrite/Models/Rule.cs.twig',
341347
'minify' => false,
342348
],
343-
349+
[
350+
'scope' => 'default',
351+
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Models/Exception.cs',
352+
'template' => '/dotnet/src/Appwrite/Models/Exception.cs.twig',
353+
'minify' => false,
354+
],
344355
[
345356
'scope' => 'default',
346357
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Services/Service.cs',

src/SDK/SDK.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ public function __construct(Language $language, Spec $spec)
146146
}
147147
return implode("\n", $value);
148148
}, ['is_safe' => ['html']]));
149+
$this->twig->addFilter(new TwigFilter('dotnetComment', function ($value) {
150+
$value = explode("\n", $value);
151+
foreach ($value as $key => $line) {
152+
$value[$key] = " /// " . wordwrap($value[$key], 75, "\n /// ");
153+
}
154+
return implode("\n", $value);
155+
}, ['is_safe' => ['html']]));
149156
$this->twig->addFilter(new TwigFilter('escapeDollarSign', function ($value) {
150157
return str_replace('$', '\$', $value);
151158
}));

templates/dotnet/.travis.yml.twig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: csharp
2+
3+
mono: none
4+
5+
dotnet: 5.0
6+
7+
before_install:
8+
- sudo apt-get -y install libpam0g-dev
9+
10+
install:
11+
- dotnet restore ./src
12+
13+
script:
14+
- dotnet build -c Release ./src
15+
16+
before_deploy:
17+
- dotnet pack -c Release ./src
18+
19+
deploy:
20+
skip_cleanup: true
21+
provider: script
22+
script: dotnet nuget push ./src/Appwrite/bin/Release/Appwrite.*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
23+
on:
24+
tags: true

templates/dotnet/src/Appwrite/Appwrite.csproj.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.1;net461;netstandard2.0;</TargetFrameworks>
4-
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
54
<PackageId>{{spec.title}}</PackageId>
65
<Version>{{sdk.version}}</Version>
76
<Authors>{{spec.contactName}}</Authors>

templates/dotnet/src/Appwrite/Client.cs.twig

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Newtonsoft.Json.Linq;
12
using System;
23
using System.Collections.Generic;
34
using System.IO;
@@ -11,22 +12,14 @@ namespace {{ spec.title | caseUcfirst }}
1112
{
1213
public class Client
1314
{
14-
1515
private readonly HttpClient http;
16-
1716
private readonly Dictionary<string, string> headers;
18-
1917
private readonly Dictionary<string, string> config;
20-
2118
private string endPoint;
22-
2319
private bool selfSigned;
2420

25-
CookieContainer cookieJar = new CookieContainer();
26-
2721
public Client() : this("https://appwrite.io/v1", false, new HttpClient())
2822
{
29-
3023
}
3124

3225
public Client(string endPoint, bool selfSigned, HttpClient http)
@@ -44,9 +37,7 @@ namespace {{ spec.title | caseUcfirst }}
4437

4538
};
4639
this.config = new Dictionary<string, string>();
47-
this.http = http;
48-
49-
// coockie container ??
40+
this.http = http;
5041
}
5142

5243
public Client SetSelfSigned(bool selfSigned)
@@ -71,10 +62,9 @@ namespace {{ spec.title | caseUcfirst }}
7162
return config;
7263
}
7364

74-
7565
{% for header in spec.global.headers %}
7666
{% if header.description %}
77-
/// {{header.description}}
67+
/// <summary>{{header.description}}</summary>
7868
{% endif %}
7969
public Client Set{{header.key | caseUcfirst}}(string value) {
8070
config.Add("{{ header.key | caseCamel }}", value);
@@ -83,8 +73,6 @@ namespace {{ spec.title | caseUcfirst }}
8373
}
8474

8575
{% endfor %}
86-
87-
8876
public Client AddHeader(String key, String value)
8977
{
9078
headers.Add(key, value);
@@ -106,8 +94,6 @@ namespace {{ spec.title | caseUcfirst }}
10694

10795
if ("multipart/form-data".Equals(headers["content-type"], StringComparison.InvariantCultureIgnoreCase))
10896
{
109-
110-
11197
MultipartFormDataContent form = new MultipartFormDataContent();
11298

11399
foreach (var parameter in parameters)
@@ -172,10 +158,24 @@ namespace {{ spec.title | caseUcfirst }}
172158
request.Headers.Add(header.Key, header.Value);
173159
}
174160
}
175-
HttpResponseMessage httpResponseMessage = await http.SendAsync(request);
161+
try
162+
{
163+
var httpResponseMessage = await http.SendAsync(request);
164+
var code = (int) httpResponseMessage.StatusCode;
165+
var response = await httpResponseMessage.Content.ReadAsStringAsync();
176166

177-
return httpResponseMessage;
178-
}
167+
if (code >= 400) {
168+
string message = (JObject.Parse(response))["message"].ToString();
169+
throw new {{spec.title | caseUcfirst}}Exception(message, code, response.ToString());
170+
}
179171

172+
return httpResponseMessage;
173+
}
174+
catch (System.Exception e)
175+
{
176+
throw new {{spec.title | caseUcfirst}}Exception(e.Message, e);
177+
}
178+
179+
}
180180
}
181181
}

templates/dotnet/src/Appwrite/Helpers/ExtensionMethods.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace {{ spec.title | caseUcfirst }}
88
{
99
public static class ExtensionMethods
1010
{
11-
1211
public static string ToJson(this Dictionary<string, object> dict)
1312
{
1413
var settings = new JsonSerializerSettings
@@ -43,6 +42,5 @@ public static string ToQueryString(this Dictionary<string, object> parameters)
4342
}
4443
return string.Join("&", query);
4544
}
46-
4745
}
4846
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Appwrite
4+
{
5+
public class {{spec.title | caseUcfirst}}Exception : Exception
6+
{
7+
public int? Code;
8+
public string Response = null;
9+
public {{spec.title | caseUcfirst}}Exception(string message = null, int? code = null, string response = null)
10+
: base(message)
11+
{
12+
this.Code = code;
13+
this.Response = response;
14+
}
15+
public {{spec.title | caseUcfirst}}Exception(string message, Exception inner)
16+
: base(message, inner)
17+
{
18+
}
19+
}
20+
}
21+

templates/dotnet/src/Appwrite/Models/Rule.cs.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ namespace {{ spec.title | caseUcfirst }}
66
{
77
public class Rule
88
{
9-
109
public string Label { get; set; }
1110
public string Key { get; set; }
1211
public string Type { get; set; }
1312
public string Default { get; set; }
1413
public bool Required { get; set; }
15-
1614
public bool Array { get; set; }
1715
}
1816
}

templates/dotnet/src/Appwrite/Services/ServiceTemplate.cs.twig

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,52 @@ namespace {{ spec.title | caseUcfirst }}
2222
{
2323
public class {{ service.name | caseUcfirst }} : Service
2424
{
25-
2625
public {{ service.name | caseUcfirst }}(Client client) : base(client) { }
2726

28-
29-
{% for method in service.methods %}
30-
27+
{% for method in service.methods %}
28+
{% if method.title %}
29+
/// <summary>
3130
/// {{ method.title }}
32-
{% if method.description %}
33-
/*
34-
{{ method.description|comment1 }}
35-
*/
36-
{% endif %}
37-
public {% if method.type == "location" %}string{% else %}async Task<HttpResponseMessage>{% endif %} {{ method.name | caseUcfirst }}({{ _self.method_parameters(method.parameters) }})
31+
{% endif %}
32+
{% if method.description %}
33+
/// <para>
34+
{{method.description|dotnetComment}}
35+
/// </para>
36+
{% endif %}
37+
/// </summary>
38+
public {% if method.type == "location" %}string{% else %}async Task<HttpResponseMessage>{% endif %} {{ method.name | caseUcfirst }}({{ _self.method_parameters(method.parameters) }})
3839
{
3940
string path = "{{ method.path }}"{% for parameter in method.parameters.path %}.Replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }}){% endfor %};
4041

4142
Dictionary<string, object> parameters = new Dictionary<string, object>()
4243
{
43-
{% for parameter in method.parameters.query | merge(method.parameters.body) %}
44-
44+
{% for parameter in method.parameters.query | merge(method.parameters.body) %}
4545
{ "{{ parameter.name }}", {{ _self.map_parameter(parameter) }} }{% if not loop.last or _self.methodNeedsSecurityParameters(method) %},{% endif %}
4646

47-
{% endfor %}
48-
};
47+
{% endfor %}
48+
};
49+
{% if _self.methodNeedsSecurityParameters(method) %}{% for node in method.security %}
50+
{% for key,header in node|keys %}
51+
// { "{{header|caseLower}}", _client.GetConfig().get("{{header|caseLower}}") }{% if not loop.last %},{% endif %}
4952

50-
// {% if _self.methodNeedsSecurityParameters(method) %}
51-
// {% for node in method.security %}
52-
// {% for key,header in node|keys %}
53-
// { "{{header|caseLower}}", _client.GetConfig().get("{{header|caseLower}}") }{% if not loop.last %},{% endif %}
54-
// {% endfor %}
55-
// {% endfor %}
56-
// {% endif %}
57-
58-
59-
{% if method.type == 'location' %}
53+
{% endfor %}
54+
{% endfor %}
55+
{% endif %}
56+
{% if method.type == 'location' %}
6057
return _client.GetEndPoint() + path + "?" + parameters.ToQueryString();
61-
{% else %}
58+
{% else %}
6259

6360
Dictionary<string, string> headers = new Dictionary<string, string>()
6461
{
65-
{{ method.headers|map((header, key) => " {\"#{key}\", \"#{header}\" }")|join(',\n')|raw }}
62+
{{ method.headers|map((header, key) => " { \"#{key}\", \"#{header}\" }")|join(',\n')|raw }}
6663
};
6764

6865
return await _client.Call("{{ method.method | caseUpper }}", path, headers, parameters);
69-
{% endif %}
70-
}
71-
{% endfor %}
66+
{% endif %}
67+
}
68+
{% if not loop.last %}
69+
70+
{% endif %}
71+
{% endfor %}
7272
};
7373
}

tests/SDKTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class SDKTest extends TestCase
8686
'dotnet-5.0' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dotnet/src/test/ mcr.microsoft.com/dotnet/sdk:5.0-alpine pwsh tests.ps1',
8787
'dotnet-3.1' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dotnet/src/test/ mcr.microsoft.com/dotnet/sdk:3.1-alpine pwsh tests.ps1',
8888
],
89-
'supportException' => false,
89+
'supportException' => true,
9090
],
9191

9292
'typescript' => [

0 commit comments

Comments
 (0)