Skip to content

Commit

Permalink
Merge pull request #131 from appwrite/feat-dotnet-exceptions
Browse files Browse the repository at this point in the history
Feat .NET Exceptions
  • Loading branch information
eldadfux authored Mar 23, 2021
2 parents b101f1f + af7bf3c commit 12a6a4b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 79 deletions.
13 changes: 12 additions & 1 deletion src/SDK/Language/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public function getFiles()
'template' => '/dotnet/LICENSE.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '.travis.yml',
'template' => '/dotnet/.travis.yml.twig',
'minify' => false,
],
[
'scope' => 'method',
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
Expand Down Expand Up @@ -340,7 +346,12 @@ public function getFiles()
'template' => '/dotnet/src/Appwrite/Models/Rule.cs.twig',
'minify' => false,
],

[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Models/Exception.cs',
'template' => '/dotnet/src/Appwrite/Models/Exception.cs.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Services/Service.cs',
Expand Down
7 changes: 7 additions & 0 deletions src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function __construct(Language $language, Spec $spec)
}
return implode("\n", $value);
}, ['is_safe' => ['html']]));
$this->twig->addFilter(new TwigFilter('dotnetComment', function ($value) {
$value = explode("\n", $value);
foreach ($value as $key => $line) {
$value[$key] = " /// " . wordwrap($value[$key], 75, "\n /// ");
}
return implode("\n", $value);
}, ['is_safe' => ['html']]));
$this->twig->addFilter(new TwigFilter('escapeDollarSign', function ($value) {
return str_replace('$', '\$', $value);
}));
Expand Down
24 changes: 24 additions & 0 deletions templates/dotnet/.travis.yml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: csharp

mono: none

dotnet: 5.0

before_install:
- sudo apt-get -y install libpam0g-dev

install:
- dotnet restore ./src

script:
- dotnet build -c Release ./src

before_deploy:
- dotnet pack -c Release ./src

deploy:
skip_cleanup: true
provider: script
script: dotnet nuget push ./src/Appwrite/bin/Release/Appwrite.*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
on:
tags: true
1 change: 0 additions & 1 deletion templates/dotnet/src/Appwrite/Appwrite.csproj.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net461;netstandard2.0;</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
<PackageId>{{spec.title}}</PackageId>
<Version>{{sdk.version}}</Version>
<Authors>{{spec.contactName}}</Authors>
Expand Down
40 changes: 20 additions & 20 deletions templates/dotnet/src/Appwrite/Client.cs.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -11,22 +12,14 @@ namespace {{ spec.title | caseUcfirst }}
{
public class Client
{

private readonly HttpClient http;

private readonly Dictionary<string, string> headers;

private readonly Dictionary<string, string> config;

private string endPoint;

private bool selfSigned;

CookieContainer cookieJar = new CookieContainer();

public Client() : this("https://appwrite.io/v1", false, new HttpClient())
{

}

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

};
this.config = new Dictionary<string, string>();
this.http = http;

// coockie container ??
this.http = http;
}

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


{% for header in spec.global.headers %}
{% if header.description %}
/// {{header.description}}
/// <summary>{{header.description}}</summary>
{% endif %}
public Client Set{{header.key | caseUcfirst}}(string value) {
config.Add("{{ header.key | caseCamel }}", value);
Expand All @@ -83,8 +73,6 @@ namespace {{ spec.title | caseUcfirst }}
}

{% endfor %}


public Client AddHeader(String key, String value)
{
headers.Add(key, value);
Expand All @@ -106,8 +94,6 @@ namespace {{ spec.title | caseUcfirst }}

if ("multipart/form-data".Equals(headers["content-type"], StringComparison.InvariantCultureIgnoreCase))
{


MultipartFormDataContent form = new MultipartFormDataContent();

foreach (var parameter in parameters)
Expand Down Expand Up @@ -172,10 +158,24 @@ namespace {{ spec.title | caseUcfirst }}
request.Headers.Add(header.Key, header.Value);
}
}
HttpResponseMessage httpResponseMessage = await http.SendAsync(request);
try
{
var httpResponseMessage = await http.SendAsync(request);
var code = (int) httpResponseMessage.StatusCode;
var response = await httpResponseMessage.Content.ReadAsStringAsync();

return httpResponseMessage;
}
if (code >= 400) {
string message = (JObject.Parse(response))["message"].ToString();
throw new {{spec.title | caseUcfirst}}Exception(message, code, response.ToString());
}

return httpResponseMessage;
}
catch (System.Exception e)
{
throw new {{spec.title | caseUcfirst}}Exception(e.Message, e);
}

}
}
}
2 changes: 0 additions & 2 deletions templates/dotnet/src/Appwrite/Helpers/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace {{ spec.title | caseUcfirst }}
{
public static class ExtensionMethods
{

public static string ToJson(this Dictionary<string, object> dict)
{
var settings = new JsonSerializerSettings
Expand Down Expand Up @@ -43,6 +42,5 @@ public static string ToQueryString(this Dictionary<string, object> parameters)
}
return string.Join("&", query);
}

}
}
21 changes: 21 additions & 0 deletions templates/dotnet/src/Appwrite/Models/Exception.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Appwrite
{
public class {{spec.title | caseUcfirst}}Exception : Exception
{
public int? Code;
public string Response = null;
public {{spec.title | caseUcfirst}}Exception(string message = null, int? code = null, string response = null)
: base(message)
{
this.Code = code;
this.Response = response;
}
public {{spec.title | caseUcfirst}}Exception(string message, Exception inner)
: base(message, inner)
{
}
}
}

2 changes: 0 additions & 2 deletions templates/dotnet/src/Appwrite/Models/Rule.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ namespace {{ spec.title | caseUcfirst }}
{
public class Rule
{

public string Label { get; set; }
public string Key { get; set; }
public string Type { get; set; }
public string Default { get; set; }
public bool Required { get; set; }

public bool Array { get; set; }
}
}
58 changes: 29 additions & 29 deletions templates/dotnet/src/Appwrite/Services/ServiceTemplate.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,52 @@ namespace {{ spec.title | caseUcfirst }}
{
public class {{ service.name | caseUcfirst }} : Service
{

public {{ service.name | caseUcfirst }}(Client client) : base(client) { }


{% for method in service.methods %}

{% for method in service.methods %}
{% if method.title %}
/// <summary>
/// {{ method.title }}
{% if method.description %}
/*
{{ method.description|comment1 }}
*/
{% endif %}
public {% if method.type == "location" %}string{% else %}async Task<HttpResponseMessage>{% endif %} {{ method.name | caseUcfirst }}({{ _self.method_parameters(method.parameters) }})
{% endif %}
{% if method.description %}
/// <para>
{{method.description|dotnetComment}}
/// </para>
{% endif %}
/// </summary>
public {% if method.type == "location" %}string{% else %}async Task<HttpResponseMessage>{% endif %} {{ method.name | caseUcfirst }}({{ _self.method_parameters(method.parameters) }})
{
string path = "{{ method.path }}"{% for parameter in method.parameters.path %}.Replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }}){% endfor %};

Dictionary<string, object> parameters = new Dictionary<string, object>()
{
{% for parameter in method.parameters.query | merge(method.parameters.body) %}

{% for parameter in method.parameters.query | merge(method.parameters.body) %}
{ "{{ parameter.name }}", {{ _self.map_parameter(parameter) }} }{% if not loop.last or _self.methodNeedsSecurityParameters(method) %},{% endif %}

{% endfor %}
};
{% endfor %}
};
{% if _self.methodNeedsSecurityParameters(method) %}{% for node in method.security %}
{% for key,header in node|keys %}
// { "{{header|caseLower}}", _client.GetConfig().get("{{header|caseLower}}") }{% if not loop.last %},{% endif %}

// {% if _self.methodNeedsSecurityParameters(method) %}
// {% for node in method.security %}
// {% for key,header in node|keys %}
// { "{{header|caseLower}}", _client.GetConfig().get("{{header|caseLower}}") }{% if not loop.last %},{% endif %}
// {% endfor %}
// {% endfor %}
// {% endif %}


{% if method.type == 'location' %}
{% endfor %}
{% endfor %}
{% endif %}
{% if method.type == 'location' %}
return _client.GetEndPoint() + path + "?" + parameters.ToQueryString();
{% else %}
{% else %}

Dictionary<string, string> headers = new Dictionary<string, string>()
{
{{ method.headers|map((header, key) => " {\"#{key}\", \"#{header}\" }")|join(',\n')|raw }}
{{ method.headers|map((header, key) => " { \"#{key}\", \"#{header}\" }")|join(',\n')|raw }}
};

return await _client.Call("{{ method.method | caseUpper }}", path, headers, parameters);
{% endif %}
}
{% endfor %}
{% endif %}
}
{% if not loop.last %}

{% endif %}
{% endfor %}
};
}
2 changes: 1 addition & 1 deletion tests/SDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SDKTest extends TestCase
'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',
'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',
],
'supportException' => false,
'supportException' => true,
],

'typescript' => [
Expand Down
60 changes: 37 additions & 23 deletions tests/languages/dotnet/tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Await-Task {

process {
while (-not $task.AsyncWaitHandle.WaitOne(200)) { }
$task.GetAwaiter().GetResult()
$task.GetAwaiter()
}
}

Expand All @@ -25,38 +25,52 @@ $bar = New-Object Appwrite.Bar -ArgumentList $client
$general = New-Object Appwrite.General -ArgumentList $client

$list = $("string in array")
$response = $foo.get("string", 123, $list) | Await-Task
Print-Response $response
$response = $foo.Get("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $foo.post("string", 123, $list) | Await-Task
Print-Response $response
$response = $foo.Post("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $foo.put("string", 123, $list) | Await-Task
Print-Response $response
$response = $foo.Put("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $foo.patch("string", 123, $list) | Await-Task
Print-Response $response
$response = $foo.Patch("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $foo.delete("string", 123, $list) | Await-Task
Print-Response $response
$response = $foo.Delete("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $bar.get("string", 123, $list) | Await-Task
Print-Response $response
$response = $bar.Get("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $bar.post("string", 123, $list) | Await-Task
Print-Response $response
$response = $bar.Post("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $bar.put("string", 123, $list) | Await-Task
Print-Response $response
$response = $bar.Put("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $bar.patch("string", 123, $list) | Await-Task
Print-Response $response
$response = $bar.Patch("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $bar.delete("string", 123, $list) | Await-Task
Print-Response $response
$response = $bar.Delete("string", 123, $list) | Await-Task
Print-Response $response.GetResult()

$response = $general.Redirect() | Await-Task
Print-Response $response
Print-Response $response.GetResult()

$response = $general.Upload("string", 123, $list, (Get-Item "../../../../resources/file.png")) | Await-Task
Print-Response $response
Print-Response $response.GetResult()

try {
$response = $general.Error400() | Await-Task
$response.GetResult()
} catch [Appwrite.AppwriteException] {
Write-Host $_.Exception.Message
}

try {
$response = $general.Error500() | Await-Task
$response.GetResult()
} catch [Appwrite.AppwriteException] {
Write-Host $_.Exception.Message
}

0 comments on commit 12a6a4b

Please sign in to comment.