Skip to content

Commit

Permalink
Merge pull request #320 from appwrite/feat-dotnet-response-models
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar authored May 14, 2023
2 parents 3dd97aa + 0cec05e commit 6d747e4
Show file tree
Hide file tree
Showing 29 changed files with 1,757 additions and 336 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ env:
- SDK=DartStable
- SDK=Deno1193
- SDK=Deno1303
- SDK=DotNet60
- SDK=DotNet70
- SDK=FlutterStable
- SDK=FlutterBeta
- SDK=Go112
Expand Down
91 changes: 65 additions & 26 deletions src/SDK/Language/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,26 @@ public function getKeywords(): array
public function getIdentifierOverrides(): array
{
return [
'Jwt' => 'JWT'
'Jwt' => 'JWT',
'Domain' => 'XDomain',
];
}

/**
* @param $type
* @param array $parameter
* @return string
*/
public function getTypeName(array $parameter): string
{
switch ($parameter['type']) {
case self::TYPE_INTEGER:
return 'int';
return 'long';
case self::TYPE_NUMBER:
return 'double';
case self::TYPE_STRING:
return 'string';
case self::TYPE_FILE:
return 'FileInfo';
return 'InputFile';
case self::TYPE_BOOLEAN:
return 'bool';
case self::TYPE_ARRAY:
Expand Down Expand Up @@ -249,7 +252,13 @@ public function getParamExample(array $param): string
$output .= '[object]';
break;
case self::TYPE_ARRAY:
$output .= '[List<object>]';
if (\str_starts_with($example, '[')) {
$example = \substr($example, 1);
}
if (\str_ends_with($example, ']')) {
$example = \substr($example, 0, -1);
}
$output .= 'new List<' . $this->getTypeName($param['array']) . '> {' . $example . '}';
break;
}
} else {
Expand Down Expand Up @@ -283,23 +292,28 @@ public function getFiles(): array
return [
[
'scope' => 'default',
'destination' => 'README.md',
'template' => 'dotnet/README.md.twig',
'destination' => '.travis.yml',
'template' => 'dotnet/.travis.yml.twig',
],
[
'scope' => 'default',
'destination' => 'CHANGELOG.md',
'template' => 'dotnet/CHANGELOG.md.twig',
],
[
'scope' => 'copy',
'destination' => '/icon.png',
'template' => 'dotnet/icon.png',
],
[
'scope' => 'default',
'destination' => 'LICENSE',
'template' => 'dotnet/LICENSE.twig',
],
[
'scope' => 'default',
'destination' => '.travis.yml',
'template' => 'dotnet/.travis.yml.twig',
'destination' => 'README.md',
'template' => 'dotnet/README.md.twig',
],
[
'scope' => 'method',
Expand All @@ -308,53 +322,78 @@ public function getFiles(): array
],
[
'scope' => 'default',
'destination' => '/src/Appwrite.sln',
'destination' => '/src/{{ spec.title | caseUcfirst }}.sln',
'template' => 'dotnet/src/Appwrite.sln',
],
[
'scope' => 'copy',
'destination' => '/icon.png',
'template' => 'dotnet/icon.png',
],
[
'scope' => 'default',
'destination' => '/src/Appwrite/Appwrite.csproj',
'destination' => '/src/{{ spec.title | caseUcfirst }}/{{ spec.title | caseUcfirst }}.csproj',
'template' => 'dotnet/src/Appwrite/Appwrite.csproj.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Client.cs',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Client.cs',
'template' => 'dotnet/src/Appwrite/Client.cs.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Helpers/ExtensionMethods.cs',
'template' => 'dotnet/src/Appwrite/Helpers/ExtensionMethods.cs',
'destination' => '/src/{{ spec.title | caseUcfirst }}/{{ spec.title | caseUcfirst }}Exception.cs',
'template' => 'dotnet/src/Appwrite/Exception.cs.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Models/OrderType.cs',
'destination' => '/src/{{ spec.title | caseUcfirst }}/ID.cs',
'template' => 'dotnet/src/Appwrite/ID.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Permission.cs',
'template' => 'dotnet/src/Appwrite/Permission.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Query.cs',
'template' => 'dotnet/src/Appwrite/Query.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Role.cs',
'template' => 'dotnet/src/Appwrite/Role.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Extensions/Extensions.cs',
'template' => 'dotnet/src/Appwrite/Extensions/Extensions.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/OrderType.cs',
'template' => 'dotnet/src/Appwrite/Models/OrderType.cs.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Models/Rule.cs',
'template' => 'dotnet/src/Appwrite/Models/Rule.cs.twig',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/UploadProgress.cs',
'template' => 'dotnet/src/Appwrite/Models/UploadProgress.cs.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Models/Exception.cs',
'template' => 'dotnet/src/Appwrite/Models/Exception.cs.twig',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/InputFile.cs',
'template' => 'dotnet/src/Appwrite/Models/InputFile.cs.twig',
],
[
'scope' => 'default',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Services/Service.cs',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Services/Service.cs',
'template' => 'dotnet/src/Appwrite/Services/Service.cs.twig',
],
[
'scope' => 'service',
'destination' => '/{{ sdk.namespace | caseSlash }}/src/Appwrite/Services/{{service.name | caseUcfirst}}.cs',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Services/{{service.name | caseUcfirst}}.cs',
'template' => 'dotnet/src/Appwrite/Services/ServiceTemplate.cs.twig',
],
[
'scope' => 'definition',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs',
'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig',
]
];
}
Expand Down
21 changes: 21 additions & 0 deletions templates/dotnet/base/params.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% import 'dotnet/base/utils.twig' as utils %}
{%~ for parameter in method.parameters.path %}
.Replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel | escapeKeyword }}){% if loop.last %};{% endif %}

{%~ endfor %}

var parameters = new Dictionary<string, object?>()
{
{%~ for parameter in method.parameters.query | merge(method.parameters.body) %}
{ "{{ parameter.name }}", {{ utils.map_parameter(parameter) }} }{% if not loop.last %},{% endif %}

{%~ endfor %}
};

var headers = new Dictionary<string, string>()
{
{%~ for key, header in method.headers %}
{ "{{ key }}", "{{ header }}" }{% if not loop.last %},{% endif %}

{%~ endfor %}
};
11 changes: 11 additions & 0 deletions templates/dotnet/base/requests/api.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% import 'dotnet/base/utils.twig' as utils %}
return _client.Call{% if method.type != 'webAuth' %}<{{ utils.resultType(spec.title, method) }}>{% endif %}(
method: "{{ method.method | caseUpper }}",
path: path,
headers: headers,
{%~ if not method.responseModel %}
parameters: parameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!);
{%~ else %}
parameters: parameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!,
convert: Convert);
{%~ endif %}
18 changes: 18 additions & 0 deletions templates/dotnet/base/requests/file.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
string? idParamName = {% if method.parameters.all | filter(p => p.isUploadID) | length > 0 %}{% for parameter in method.parameters.all | filter(parameter => parameter.isUploadID) %}"{{ parameter.name }}"{% endfor %}{% else %}null{% endif %};

{%~ for parameter in method.parameters.all %}
{%~ if parameter.type == 'file' %}
var paramName = "{{ parameter.name }}";
{%~ endif %}
{%~ endfor %}

return _client.ChunkedUpload(
path,
headers,
parameters,
{%~ if method.responseModel %}
Convert,
{%~ endif %}
paramName,
idParamName,
onProgress);
5 changes: 5 additions & 0 deletions templates/dotnet/base/requests/location.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return _client.Call<byte[]>(
method: "{{ method.method | caseUpper }}",
path: path,
headers: headers,
parameters: parameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!);
16 changes: 16 additions & 0 deletions templates/dotnet/base/utils.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% macro parameter(parameter) %}
{% if parameter.name == 'orderType' %}{{ 'OrderType orderType = OrderType.ASC' }}{% else %}
{{ parameter | typeName }}{% if not parameter.required %}?{% endif %} {{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %} = null{% endif %}{% endif %}
{% endmacro %}
{% macro method_parameters(parameters, consumes) %}
{% if parameters.all|length > 0 %}{% for parameter in parameters.all | filter((param) => not param.isGlobal) %}{{ _self.parameter(parameter) }}{% if not loop.last %}{{ ', ' }}{% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %},{% endif %}{% endif %}{% if 'multipart/form-data' in consumes %} Action<UploadProgress>? onProgress = null{% endif %}
{% endmacro %}
{% macro map_parameter(parameter) %}
{% if parameter.name == 'orderType' %}{{ parameter.name | caseCamel ~ '.ToString()'}}{% elseif parameter.isGlobal %}{{ parameter.name | caseUcfirst | escapeKeyword }}{% else %}{{ parameter.name | caseCamel | escapeKeyword }}{% endif %}
{% endmacro %}
{% macro methodNeedsSecurityParameters(method) %}
{% if (method.type == "webAuth" or method.type == "location") and method.auth|length > 0 %}{{ true }}{% else %}{{false}}{% endif %}
{% endmacro %}
{% macro resultType(namespace, method) %}
{% if method.type == "webAuth" %}bool{% elseif method.type == "location" %}byte[]{% elseif not method.responseModel or method.responseModel == 'any' %}object{% else %}Models.{{method.responseModel | caseUcfirst | overrideIdentifier }}{% endif %}
{% endmacro %}
20 changes: 10 additions & 10 deletions templates/dotnet/docs/example.md.twig
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using {{ spec.title | caseUcfirst }};
using {{ spec.title | caseUcfirst }}.Models;

Client client = new Client();

Client client = new Client()
{% if method.auth|length > 0 %}
client
.SetEndPoint("https://cloud.appwrite.io/v1") // Your API Endpoint
.SetEndPoint("https://cloud.appwrite.io/v1") // Your API Endpoint
{% for node in method.auth %}
{% for key,header in node|keys %}
.Set{{header | caseUcfirst}}("{{node[header]["x-appwrite"]["demo"]}}") // {{node[header].description}}
{% endfor %}
{% endfor %};
.Set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
{% endfor %}{% endfor %}{% endif %}

{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);

{% endif %}
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %});
{% if method.type == 'location' %}byte[]{% else %}{{ method.responseModel | caseUcfirst | overrideIdentifier }}{% endif %} result = await {{ service.name | caseCamel }}.{{ method.name | caseUcfirst }}({% if method.parameters.all | length == 0 %});{% endif %}
{% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %},{% endif %}

{% if method.type == 'location' %}string{% else %}HttpResponseMessage{% endif %} result = await {{ service.name | caseCamel }}.{{ method.name | caseUcfirst }}({% for parameter in method.parameters.all %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{ parameter | paramExample }}{% endif %}{% endfor %});
{{ parameter.name }}: {{ parameter | paramExample }}{% endif %}{% endfor %}{% if method.parameters.all | length > 0 %});{% endif %}
7 changes: 4 additions & 3 deletions templates/dotnet/src/Appwrite/Appwrite.csproj.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net461;netstandard2.0;</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<PackageId>{{spec.title}}</PackageId>
<Version>{{sdk.version}}</Version>
<Authors>{{spec.contactName}}</Authors>
Expand All @@ -14,11 +14,12 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>{{sdk.gitURL}}</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<None Include="..\..\icon.png" Pack="true" PackagePath="$(PackageIcon)"/>
</ItemGroup>
Expand Down
Loading

0 comments on commit 6d747e4

Please sign in to comment.