Skip to content

Commit ad16d15

Browse files
authored
Merge branch 'master' into feat-dart-strings
2 parents f12dfd8 + 6185cdf commit ad16d15

17 files changed

+77
-39
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ SDK Language name (JS, PHP…)
4848
**getKeywords**
4949
An array with language keywords to avoid using as param or function names, template engine will solve conflicts
5050

51+
**getIdentifierOverrides**
52+
Returns an associative array that can be used to override keywords with pre-defined word using `overrideIdentifier` filter.
53+
5154
**getFiles**
5255
An array with a list of language template files in [twig format](https://twig.symfony.com/).
5356
Each file scope determines what template parameters will be available.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
44
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
5-
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite_io?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite_io)
5+
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
66
[![Follow Appwrite on StackShare](https://img.shields.io/badge/follow%20on-stackshare-blue?style=flat-square)](https://stackshare.io/appwrite)
77
[![appwrite.io](https://img.shields.io/badge/appwrite-.io-f02e65?style=flat-square)](https://appwrite.io)
88

src/SDK/Language/Dart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function getKeywords()
101101
"set",
102102
"yield",
103103
"required",
104-
"default"
104+
"extension",
105+
"late"
105106
];
106107
}
107108

@@ -110,7 +111,7 @@ public function getKeywords()
110111
*/
111112
public function getIdentifierOverrides()
112113
{
113-
return [];
114+
return ['Function' => 'Func'];
114115
}
115116

116117
/**

templates/dart/lib/services/service.dart.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class {{ service.name | caseUcfirst }} extends Service {
5252
};
5353

5454
final res = await client.call(HttpMethod.{{ method.method | caseLower }}, path: path, params: params, headers: headers);
55-
return {% if method.responseModel and method.responseModel != 'any' %}models.{{method.responseModel | ucFirstAndEscape}}.fromMap(res.data){% else %} res.data{% endif %};
55+
return {% if method.responseModel and method.responseModel != 'any' %}models.{{method.responseModel | caseUcfirst | overrideIdentifier}}.fromMap(res.data){% else %} res.data{% endif %};
5656
{% endif %}
5757
}
5858
{% endfor %}

templates/dart/lib/src/models/model.dart.twig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>{% else %}{{property.sub_schema | ucFirstAndEscape }}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}
1+
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %}
22
part of {{ language.params.packageName }}.models;
33

44
/// {{ definition.description }}
5-
class {{ definition.name | ucFirstAndEscape }} {
5+
class {{ definition.name | caseUcfirst | overrideIdentifier }} {
66
{% for property in definition.properties %}
77
/// {{ property.description }}
88
final {% if not property.required %}{{_self.sub_schema(property)}}? {{ property.name | escapeKeyword }}{% else %}{{_self.sub_schema(property)}} {{ property.name | escapeKeyword }}{% endif %};
@@ -11,7 +11,7 @@ class {{ definition.name | ucFirstAndEscape }} {
1111
final Map<String, dynamic> data;
1212
{% endif %}
1313

14-
{{ definition.name | ucFirstAndEscape}}({
14+
{{ definition.name | caseUcfirst | overrideIdentifier}}({
1515
{% for property in definition.properties %}{% if property.required %}
1616
required {% endif %}this.{{ property.name | escapeKeyword }},
1717
{% endfor %}
@@ -20,10 +20,10 @@ class {{ definition.name | ucFirstAndEscape }} {
2020
{% endif %}
2121
});
2222

23-
factory {{ definition.name | ucFirstAndEscape}}.fromMap(Map<String, dynamic> map) {
24-
return {{ definition.name | ucFirstAndEscape }}(
23+
factory {{ definition.name | caseUcfirst | overrideIdentifier}}.fromMap(Map<String, dynamic> map) {
24+
return {{ definition.name | caseUcfirst | overrideIdentifier }}(
2525
{% for property in definition.properties %}
26-
{{ property.name | escapeKeyword }}: {% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | ucFirstAndEscape}}.fromMap(p))){% else %}{{property.sub_schema | ucFirstAndEscape}}.fromMap(map['{{property.name | escapeDollarSign }}']){% endif %}{% else %}map['{{property.name | escapeDollarSign }}']{% if property.type == "number" %}.toDouble(){% endif %}{% if property.type == "string" %}.toString(){% endif %}{% endif %},
26+
{{ property.name | escapeKeyword }}: {% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(p))){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(map['{{property.name | escapeDollarSign }}']){% endif %}{% else %}map['{{property.name | escapeDollarSign }}']{% if property.type == "number" %}.toDouble(){% endif %}{% if property.type == "string" %}.toString(){% endif %}{% endif %},
2727
{% endfor %}
2828
{% if definition.additionalProperties %}
2929
data: map,

templates/flutter/lib/services/service.dart.twig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,14 @@ class {{ service.name | caseUcfirst }} extends Service {
5252
});
5353

5454
Uri endpoint = Uri.parse(client.endPoint);
55-
Uri url = new Uri(scheme: endpoint.scheme,
55+
Uri url = Uri(scheme: endpoint.scheme,
5656
host: endpoint.host,
5757
port: endpoint.port,
5858
path: endpoint.path + path,
5959
query: query.join('&')
6060
);
6161

62-
if(kIsWeb) {
63-
redirect(url.toString());
64-
return Future.value();
65-
}else{
66-
return client.webAuth(url);
67-
}
62+
return client.webAuth(url);
6863

6964
{% elseif method.type == 'location' %}
7065
params.keys.forEach((key) {if (params[key] is int || params[key] is double) {

templates/flutter/lib/src/client_base.dart.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ abstract class ClientBase implements Client {
77
{% if header.description %}
88
/// {{header.description}}
99
{% endif %}
10+
@override
1011
ClientBase set{{header.key | caseUcfirst}}(value);
1112
{% endfor %}
1213

14+
@override
1315
ClientBase setSelfSigned({bool status = true});
1416

17+
@override
1518
ClientBase setEndpoint(String endPoint);
1619

20+
@override
1721
Client setEndPointRealtime(String endPoint);
1822

23+
@override
1924
ClientBase addHeader(String key, String value);
2025

26+
@override
2127
Future<Response> call(
2228
HttpMethod method, {
2329
String path = '',

templates/flutter/lib/src/client_browser.dart.twig

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:html' as html;
2+
import 'package:flutter_web_auth/flutter_web_auth.dart';
23
import 'package:http/http.dart' as http;
34
import 'package:http/browser_client.dart';
45
import 'client_mixin.dart';
@@ -17,10 +18,12 @@ ClientBase createClient({
1718
class ClientBrowser extends ClientBase with ClientMixin {
1819
String _endPoint;
1920
Map<String, String>? _headers;
21+
@override
2022
late Map<String, String> config;
2123
late BrowserClient _httpClient;
2224
String? _endPointRealtime;
2325

26+
@override
2427
String? get endPointRealtime => _endPointRealtime;
2528

2629
ClientBrowser({
@@ -46,23 +49,27 @@ class ClientBrowser extends ClientBase with ClientMixin {
4649
init();
4750
}
4851

52+
@override
4953
String get endPoint => _endPoint;
5054

5155
{% for header in spec.global.headers %}
5256
{% if header.description %}
5357
/// {{header.description}}
5458
{% endif %}
59+
@override
5560
ClientBrowser set{{header.key | caseUcfirst}}(value) {
5661
config['{{ header.key | caseCamel }}'] = value;
5762
addHeader('{{header.name}}', value);
5863
return this;
5964
}
6065
{% endfor %}
6166

67+
@override
6268
ClientBrowser setSelfSigned({bool status = true}) {
6369
return this;
6470
}
6571

72+
@override
6673
ClientBrowser setEndpoint(String endPoint) {
6774
this._endPoint = endPoint;
6875
_endPointRealtime = endPoint
@@ -71,17 +78,20 @@ class ClientBrowser extends ClientBase with ClientMixin {
7178
return this;
7279
}
7380

81+
@override
7482
ClientBrowser setEndPointRealtime(String endPoint) {
7583
_endPointRealtime = endPoint;
7684
return this;
7785
}
7886

87+
@override
7988
ClientBrowser addHeader(String key, String value) {
8089
_headers![key] = value;
8190

8291
return this;
8392
}
8493

94+
@override
8595
Future init() async {
8696
if (html.window.localStorage.keys.contains('cookieFallback')) {
8797
addHeader('x-fallback-cookies',
@@ -128,6 +138,16 @@ class ClientBrowser extends ClientBase with ClientMixin {
128138

129139
@override
130140
Future webAuth(Uri url) {
131-
throw UnimplementedError();
132-
}
141+
return FlutterWebAuth.authenticate(
142+
url: url.toString(),
143+
callbackUrlScheme: "appwrite-callback-" + config['project']!,
144+
).then((value) async {
145+
Uri url = Uri.parse(value);
146+
final key = url.queryParameters['key'];
147+
final secret = url.queryParameters['secret'];
148+
if (key == null || secret == null) {
149+
throw AppwriteException(
150+
"Invalid OAuth2 Response. Key and Secret not available.", 500);
151+
}
152+
}); }
133153
}

templates/flutter/lib/src/client_io.dart.twig

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,29 @@ ClientBase createClient({
2727
class ClientIO extends ClientBase with ClientMixin {
2828
String _endPoint;
2929
Map<String, String>? _headers;
30+
@override
3031
late Map<String, String> config;
3132
bool selfSigned;
3233
bool _initialized = false;
3334
String? _endPointRealtime;
3435
late http.Client _httpClient;
3536
late HttpClient _nativeClient;
3637
late CookieJar _cookieJar;
37-
List<Interceptor> _interceptors = [];
38+
final List<Interceptor> _interceptors = [];
3839

3940
bool get initialized => _initialized;
4041
CookieJar get cookieJar => _cookieJar;
42+
@override
4143
String? get endPointRealtime => _endPointRealtime;
4244

4345
ClientIO({
4446
String endPoint = 'https://appwrite.io/v1',
4547
this.selfSigned = false,
4648
}) : _endPoint = endPoint {
47-
_nativeClient = new HttpClient()
49+
_nativeClient = HttpClient()
4850
..badCertificateCallback =
4951
((X509Certificate cert, String host, int port) => selfSigned);
50-
_httpClient = new IOClient(_nativeClient);
52+
_httpClient = IOClient(_nativeClient);
5153
_endPointRealtime = endPoint
5254
.replaceFirst('https://', 'wss://')
5355
.replaceFirst('http://', 'ws://');
@@ -66,12 +68,13 @@ class ClientIO extends ClientBase with ClientMixin {
6668
init();
6769
}
6870

71+
@override
6972
String get endPoint => _endPoint;
7073

7174
Future<Directory> _getCookiePath() async {
7275
final directory = await getApplicationDocumentsDirectory();
7376
final path = directory.path;
74-
final Directory dir = new Directory('$path/cookies');
77+
final Directory dir = Directory('$path/cookies');
7578
await dir.create();
7679
return dir;
7780
}
@@ -80,20 +83,23 @@ class ClientIO extends ClientBase with ClientMixin {
8083
{% if header.description %}
8184
/// {{header.description}}
8285
{% endif %}
86+
@override
8387
ClientIO set{{header.key | caseUcfirst}}(value) {
8488
config['{{ header.key | caseCamel }}'] = value;
8589
addHeader('{{header.name}}', value);
8690
return this;
8791
}
8892
{% endfor %}
8993

94+
@override
9095
ClientIO setSelfSigned({bool status = true}) {
9196
this.selfSigned = status;
9297
_nativeClient.badCertificateCallback =
9398
((X509Certificate cert, String host, int port) => status);
9499
return this;
95100
}
96101

102+
@override
97103
ClientIO setEndpoint(String endPoint) {
98104
this._endPoint = endPoint;
99105
_endPointRealtime = endPoint
@@ -102,11 +108,13 @@ class ClientIO extends ClientBase with ClientMixin {
102108
return this;
103109
}
104110

111+
@override
105112
ClientIO setEndPointRealtime(String endPoint) {
106113
_endPointRealtime = endPoint;
107114
return this;
108115
}
109116

117+
@override
110118
ClientIO addHeader(String key, String value) {
111119
_headers![key] = value;
112120

@@ -116,7 +124,7 @@ class ClientIO extends ClientBase with ClientMixin {
116124
Future init() async {
117125
// if web skip cookie implementation and origin header as those are automatically handled by browsers
118126
final Directory cookieDir = await _getCookiePath();
119-
_cookieJar = new PersistCookieJar(storage: FileStorage(cookieDir.path));
127+
_cookieJar = PersistCookieJar(storage: FileStorage(cookieDir.path));
120128
this._interceptors.add(CookieManager(_cookieJar));
121129
PackageInfo packageInfo = await PackageInfo.fromPlatform();
122130
addHeader('Origin',
@@ -191,6 +199,7 @@ class ClientIO extends ClientBase with ClientMixin {
191199
return response;
192200
}
193201

202+
@override
194203
Future webAuth(Uri url) {
195204
return FlutterWebAuth.authenticate(
196205
url: url.toString(),
@@ -203,7 +212,7 @@ class ClientIO extends ClientBase with ClientMixin {
203212
throw {{spec.title | caseUcfirst}}Exception(
204213
"Invalid OAuth2 Response. Key and Secret not available.", 500);
205214
}
206-
Cookie cookie = new Cookie(key, secret);
215+
Cookie cookie = Cookie(key, secret);
207216
cookie.domain = Uri.parse(_endPoint).host;
208217
cookie.httpOnly = true;
209218
cookie.path = '/';
@@ -213,6 +222,7 @@ class ClientIO extends ClientBase with ClientMixin {
213222
});
214223
}
215224

225+
@override
216226
Future<Response> call(
217227
HttpMethod method, {
218228
String path = '',

templates/flutter/lib/src/client_mixin.dart.twig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class ClientMixin {
6767
}
6868

6969
Response prepareResponse(http.Response res, {ResponseType? responseType}) {
70-
if (responseType == null) {
71-
responseType = ResponseType.json;
72-
}
70+
responseType ??= ResponseType.json;
7371
if (res.statusCode >= 400) {
7472
if ((res.headers['content-type'] ?? '').contains('application/json')) {
7573
final response = json.decode(res.body);
@@ -82,7 +80,7 @@ class ClientMixin {
8280
throw {{spec.title | caseUcfirst}}Exception(res.body);
8381
}
8482
}
85-
var data;
83+
dynamic data;
8684
if ((res.headers['content-type'] ?? '').contains('application/json')) {
8785
if (responseType == ResponseType.json) {
8886
data = json.decode(res.body);
@@ -103,7 +101,7 @@ class ClientMixin {
103101

104102
Future<http.Response> toResponse(http.StreamedResponse streamedResponse) async {
105103
if(streamedResponse.statusCode == 204) {
106-
return new http.Response('',
104+
return http.Response('',
107105
streamedResponse.statusCode,
108106
headers: streamedResponse.headers.map((k,v) => k.toLowerCase()=='content-type' ? MapEntry(k, 'text/plain') : MapEntry(k,v)),
109107
request: streamedResponse.request,

0 commit comments

Comments
 (0)