Skip to content

Commit ca8e34f

Browse files
authored
Merge pull request #230 from appwrite/feat-kotlin-java-docs
Add separated java + kotlin docs for android and kotlin
2 parents dafc5fa + dde32a2 commit ca8e34f

File tree

6 files changed

+183
-8
lines changed

6 files changed

+183
-8
lines changed

src/SDK/Language/Android.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ public function getFiles()
2828
],
2929
[
3030
'scope' => 'method',
31-
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
32-
'template' => '/android/docs/example.md.twig',
31+
'destination' => 'docs/examples/kotlin/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
32+
'template' => '/android/docs/kotlin/example.md.twig',
33+
'minify' => false,
34+
],
35+
[
36+
'scope' => 'method',
37+
'destination' => 'docs/examples/java/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
38+
'template' => '/android/docs/java/example.md.twig',
3339
'minify' => false,
3440
],
3541
[

src/SDK/Language/Kotlin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,14 @@ public function getFiles()
248248
],
249249
[
250250
'scope' => 'method',
251-
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
252-
'template' => '/kotlin/docs/example.md.twig',
251+
'destination' => 'docs/examples/kotlin/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
252+
'template' => '/kotlin/docs/kotlin/example.md.twig',
253+
'minify' => false,
254+
],
255+
[
256+
'scope' => 'method',
257+
'destination' => 'docs/examples/java/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
258+
'template' => '/kotlin/docs/java/example.md.twig',
253259
'minify' => false,
254260
],
255261
[
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import androidx.appcompat.app.AppCompatActivity
2+
import android.os.Bundle
3+
import kotlinx.coroutines.GlobalScope
4+
import kotlinx.coroutines.launch
5+
import {{ sdk.namespace | caseDot }}.Client
6+
import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}
7+
8+
public class MainActivity extends AppCompatActivity {
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(R.layout.activity_main);
14+
15+
Client client = new Client(getApplicationContext())
16+
{% if method.security|length > 0 %}
17+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
18+
{% for node in method.security %}
19+
{% for key,header in node|keys %}
20+
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
21+
{% endfor %}
22+
{% endfor %}
23+
{% endif %}
24+
25+
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
26+
27+
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new Continuation<Object>() {
28+
@NotNull
29+
@Override
30+
public CoroutineContext getContext() {
31+
return EmptyCoroutineContext.INSTANCE;
32+
}
33+
34+
@Override
35+
public void resumeWith(@NotNull Object o) {
36+
String json = "";
37+
try {
38+
if (o instanceof Result.Failure) {
39+
Result.Failure failure = (Result.Failure) o;
40+
throw failure.exception;
41+
}{% if method.type != "webAuth" %} else {
42+
Response response = (Response) o;
43+
json = response.body().string();
44+
}{% endif %}
45+
46+
}
47+
} catch (Throwable th) {
48+
Log.e("ERROR", th.toString());
49+
}
50+
}
51+
});{% endif %}
52+
53+
{% if method.type == "webAuth" %}
54+
this,
55+
{% endif %}
56+
{% for parameter in method.parameters.all %}
57+
{% if parameter.required %}
58+
{{ parameter | paramExample }}{% if not loop.last %}, {% endif %}
59+
60+
{% endif %}
61+
{% if loop.last %}
62+
new Continuation<Object>() {
63+
@NotNull
64+
@Override
65+
public CoroutineContext getContext() {
66+
return EmptyCoroutineContext.INSTANCE;
67+
}
68+
69+
@Override
70+
public void resumeWith(@NotNull Object o) {
71+
String json = "";
72+
try {
73+
if (o instanceof Result.Failure) {
74+
Result.Failure failure = (Result.Failure) o;
75+
throw failure.exception;
76+
}{% if method.type != "webAuth" %} else {
77+
Response response = (Response) o;
78+
json = response.body().string();
79+
}{% endif %}
80+
81+
} catch (Throwable th) {
82+
Log.e("ERROR", th.toString());
83+
}
84+
}
85+
}
86+
);
87+
{% endif %}
88+
{% endfor %}
89+
}
90+
}

templates/android/docs/example.md.twig renamed to templates/android/docs/kotlin/example.md.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ class MainActivity : AppCompatActivity() {
2323
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
2424

2525
GlobalScope.launch {
26-
val response = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
26+
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
2727

28+
{% if method.type == "webAuth" %}
29+
activity = this@MainActivity,
30+
{% endif %}
2831
{% for parameter in method.parameters.all %}
2932
{% if parameter.required %}
3033
{{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %}
@@ -34,7 +37,8 @@ class MainActivity : AppCompatActivity() {
3437
)
3538
{% endif %}
3639
{% endfor %}
37-
val json = response.body?.string()
40+
{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %} println(result); // Resource URL{% else %} val json = response.body?.string(){% endif %}
41+
3842
}
3943
}
4044
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {{ sdk.namespace | caseDot }}.Client
2+
import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}
3+
4+
public void main() {
5+
Client client = Client(context)
6+
{% if method.security|length > 0 %}
7+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
8+
{% for node in method.security %}
9+
{% for key,header in node|keys %}
10+
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
11+
{% endfor %}{% endfor %}{% endif %}
12+
13+
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
14+
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new Continuation<Response>() {
15+
@NotNull
16+
@Override
17+
public CoroutineContext getContext() {
18+
return EmptyCoroutineContext.INSTANCE;
19+
}
20+
21+
@Override
22+
public void resumeWith(@NotNull Object o) {
23+
String json = "";
24+
try {
25+
if (o instanceof Result.Failure) {
26+
Result.Failure failure = (Result.Failure) o;
27+
throw failure.exception;
28+
} else {
29+
Response response = (Response) o;
30+
}
31+
} catch (Throwable th) {
32+
Log.e("ERROR", th.toString());
33+
}
34+
}
35+
});{% endif %}
36+
37+
{% for parameter in method.parameters.all %}
38+
{% if parameter.required %}
39+
{{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %}
40+
41+
{% endif %}
42+
{% if loop.last %}
43+
new Continuation<Response>() {
44+
@NotNull
45+
@Override
46+
public CoroutineContext getContext() {
47+
return EmptyCoroutineContext.INSTANCE;
48+
}
49+
50+
@Override
51+
public void resumeWith(@NotNull Object o) {
52+
String json = "";
53+
try {
54+
if (o instanceof Result.Failure) {
55+
Result.Failure failure = (Result.Failure) o;
56+
throw failure.exception;
57+
} else {
58+
Response response = (Response) o;
59+
}
60+
} catch (Throwable th) {
61+
Log.e("ERROR", th.toString());
62+
}
63+
}
64+
}
65+
);
66+
{% endif %}
67+
{% endfor %}
68+
}

templates/kotlin/docs/example.md.twig renamed to templates/kotlin/docs/kotlin/example.md.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ suspend fun main() {
1111
{% endfor %}{% endfor %}{% endif %}
1212

1313
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
14-
val response = {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
14+
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
1515

1616
{% for parameter in method.parameters.all %}
1717
{% if parameter.required %}
@@ -22,5 +22,6 @@ suspend fun main() {
2222
)
2323
{% endif %}
2424
{% endfor %}
25-
val json = response.body?.string()
25+
{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %} println(result); // Resource URL{% else %} val json = response.body?.string(){% endif %}
26+
2627
}

0 commit comments

Comments
 (0)