Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit c9bcd63

Browse files
Merge pull request #247 from appwrite/refactor-code-samples
Refactor code samples
2 parents b3691df + 8ebc599 commit c9bcd63

14 files changed

+1005
-840
lines changed

app/views/docs/databases.phtml

Lines changed: 296 additions & 220 deletions
Large diffs are not rendered by default.

app/views/docs/functions.phtml

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,10 @@ $image = new View(__DIR__.'/../general/image.phtml');
543543
<div class="ide margin-bottom" data-lang="javascript" data-lang-label="Web SDK">
544544
<pre class="line-numbers"><code class="prism language-javascript" data-prism>import { Client, Functions } from "appwrite";
545545

546-
const client = new Client();
547-
548-
client
546+
const client = new Client()
549547
.setEndpoint('https://[HOSTNAME_OR_IP]/v1')
550548
.setProject('[PROJECT_ID]');
551549

552-
const databases = new Databases(client, '[DATABASE_ID]');
553-
554550
let promise = functions.createExecution('[FUNCTION_ID]');
555551

556552
promise.then(function (response) {
@@ -566,25 +562,16 @@ promise.then(function (response) {
566562
<div class="ide margin-bottom" data-lang="dart" data-lang-label="Flutter SDK">
567563
<pre class="line-numbers"><code class="prism language-dart" data-prism>import 'package:appwrite/appwrite.dart';
568564

569-
void main() { // Init SDK
570-
Client client = Client();
571-
Functions functions = Functions(client);
565+
void main() async {
566+
final client = Client()
567+
.setEndpoint('https://[HOSTNAME_OR_IP]/v1')
568+
.setProject('[PROJECT_ID]');
572569

573-
client
574-
.setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
575-
.setProject('5df5acd0d48c2') // Your project ID
576-
;
570+
final functions = Functions(client);
577571

578-
Future result = functions.createExecution(
579-
functionId: '[FUNCTION_ID]',
580-
);
581-
582-
result
583-
.then((response) {
584-
print(response);
585-
}).catchError((error) {
586-
print(error.response);
587-
});
572+
final execution = await functions.createExecution(
573+
functionId: '[FUNCTION_ID]'
574+
);
588575
}</code></pre>
589576
</div>
590577
</li>
@@ -594,48 +581,36 @@ void main() { // Init SDK
594581
<div class="ide margin-bottom" data-lang="swift" data-lang-label="Apple SDK">
595582
<pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
596583

597-
func main() {
584+
func main() async throws {
598585
let client = Client()
599-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
600-
.setProject("5df5acd0d48c2") // Your project ID
586+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1")
587+
.setProject("[PROJECT_ID]")
601588

602589
let functions = Functions(client)
603-
let execution = functions.createExecution(
590+
591+
let execution = try await functions.createExecution(
604592
functionId: "[FUNCTION_ID]"
605593
)
606-
print(execution.toMap())
607594
}</code></pre>
608595
</div>
609596
</li>
610597
<li>
611598
<h3>Android</h3>
612599

613600
<div class="ide margin-bottom" data-lang="kotlin" data-lang-label="Android SDK">
614-
<pre class="line-numbers"><code class="prism language-kotlin" data-prism>import androidx.appcompat.app.AppCompatActivity
615-
import android.os.Bundle
616-
import kotlinx.coroutines.GlobalScope
617-
import kotlinx.coroutines.launch
618-
import io.appwrite.Client
601+
<pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
619602
import io.appwrite.services.Functions
620603

621-
class MainActivity : AppCompatActivity() {
622-
override fun onCreate(savedInstanceState: Bundle?) {
623-
super.onCreate(savedInstanceState)
624-
setContentView(R.layout.activity_main)
625-
626-
val client = Client(applicationContext)
627-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
628-
.setProject("5df5acd0d48c2") // Your project ID
604+
suspend fun main() {
605+
val client = Client(applicationContext)
606+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1")
607+
.setProject("[PROJECT_ID]")
629608

630-
val functions = Functions(client)
609+
val functions = Functions(client)
631610

632-
GlobalScope.launch {
633-
val response = functions.createExecution(
634-
functionId = "[FUNCTION_ID]",
635-
)
636-
val json = response.body?.string()
637-
}
638-
}
611+
val execution = functions.createExecution(
612+
functionId = "[FUNCTION_ID]"
613+
)
639614
}</code></pre>
640615
</div>
641616
</li>

app/views/docs/getting-started-for-android.phtml

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ $androidVersion = (isset($versions['android'])) ? $versions['android'] : '';
3636
}');?></code></pre>
3737
</div>
3838

39-
<p>And add this to your project's build.gradle file:</p>
39+
<p>Then add this to your project's build.gradle file:</p>
4040

4141
<div class="ide" data-lang="groovy" data-lang-label="Groovy">
42-
<pre class="line-numbers"><code class="prism language-groovy" data-prism>'implementation("io.appwrite:sdk-for-android:<?php echo $this->escape($androidVersion); ?>")'</code></pre>
42+
<pre class="line-numbers"><code class="prism language-groovy" data-prism>implementation("io.appwrite:sdk-for-android:<?php echo $this->escape($androidVersion); ?>")</code></pre>
4343
</div>
4444

4545
<h3><a href="/docs/getting-started-for-android#OAuthCallback" id="OAuthCallback">OAuth Callback</a></h3>
@@ -74,9 +74,9 @@ $androidVersion = (isset($versions['android'])) ? $versions['android'] : '';
7474
import io.appwrite.services.Account
7575

7676
val client = Client(context)
77-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
78-
.setProject("5df5acd0d48c2") // Your project ID
79-
.setSelfSigned(status: true) // For self signed certificates, only use for development</code></pre>
77+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
78+
.setProject("5df5acd0d48c2") // Your project ID
79+
.setSelfSigned(status: true) // For self signed certificates, only use for development</code></pre>
8080
</div>
8181

8282
<p>Before sending any API calls to your new Appwrite project, make sure your Android device or emulator has network access to your Appwrite project's hostname or IP address.</p>
@@ -90,8 +90,12 @@ val client = Client(context)
9090
<div class="ide" data-lang="android" data-lang-label="Android SDK">
9191
<pre class="line-numbers"><code class="prism language-kotlin" data-prism>// Register User
9292
val account = Account(client)
93-
val response = account.create("unique()", "[email protected]", "password")
94-
val json = response.body?.string()</code></pre>
93+
94+
val user = account.create(
95+
userId = ID.unique(),
96+
email = "[email protected]",
97+
password = "password"
98+
)</code></pre>
9599
</div>
96100

97101
<h2><a href="/docs/getting-started-for-android#listenToChanges" id="listenToChanges">Listen to Changes</a></h2>
@@ -102,10 +106,10 @@ val json = response.body?.string()</code></pre>
102106
<pre class="line-numbers"><code class="prism language-kotlin" data-prism>// Subscribe to files channel
103107
val realtime = Realtime(client)
104108

105-
realtime.subscribe("files", callback = { response ->
106-
if(response.events.contains("buckets.*.files.*.create")) {
109+
val subscription = realtime.subscribe("files") {
110+
if(it.events.contains("buckets.*.files.*.create")) {
107111
// Log when a new file is uploaded
108-
print(response.payload.toString());
112+
print(it.payload.toString());
109113
}
110114
})
111115
</code></pre>
@@ -117,24 +121,27 @@ realtime.subscribe("files", callback = { response ->
117121
import io.appwrite.services.Account
118122

119123
val client = Client(context)
120-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
121-
.setProject("5df5acd0d48c2") // Your project ID
122-
.setSelfSigned(status: true) // For self signed certificates, only use for development
124+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
125+
.setProject("5df5acd0d48c2") // Your project ID
126+
.setSelfSigned(status: true) // For self signed certificates, only use for development
123127

124-
// Register User
125128
val account = Account(client)
126-
val response = account.create("unique()", "[email protected]", "password")
127-
val json = response.body?.string()
129+
130+
val user = account.create(
131+
userId = ID.unique(),
132+
email = "[email protected]",
133+
password = "password"
134+
)
128135

129136
// Subscribe to files channel
130137
val realtime = Realtime(client)
131138

132-
realtime.subscribe("files", callback = { response ->
133-
if(response.events.contains("buckets.*.files.*.create")) {
139+
val subscription = realtime.subscribe("files") {
140+
if(it.events.contains("buckets.*.files.*.create")) {
134141
// Log when a new file is uploaded
135-
print(response.payload.toString());
142+
print(it.payload.toString());
136143
}
137-
})</code></pre>
144+
}</code></pre>
138145
</div>
139146

140147
<h2><a href="/docs/getting-started-for-android#nextSteps" id="nextSteps">Next Steps</a></h2>

app/views/docs/getting-started-for-apple.phtml

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,47 @@ $appleVersion = $versions['apple'] ?? '';
2323

2424
<h2><a href="/docs/getting-started-for-apple#addProject" id="addProject">Add your Apple Platform</a></h2>
2525

26-
<p>To init your SDK and start interacting with Appwrite services, you need to add a new Apple platform to your project. To add a new platform, go to your Appwrite console, choose the project you created in the step before, and click the 'Add Platform' button. Only API requests initiated from platforms added to your Appwrite project will be accepted. This prevents unauthorized apps from accessing your Appwrite project.</p>
26+
<p>To init your SDK and start interacting with Appwrite services, you need to add a new Apple platform to your project. To add a new platform, go to your Appwrite console, choose the project you created in the step before, and click the <b>'Add Platform'</b> button. Only API requests initiated from platforms added to your Appwrite project will be accepted. This prevents unauthorized apps from accessing your Appwrite project.</p>
2727

28-
<p>From the options, choose to add a new <b>Apple</b> platform, select the iOS, macOS, watchOS or tvOS tab and add your app <u>name</u> and <u>bundle identifier</u>, Your bundle identifier can be found at the top of the `General` tab in your project settings, or in your `Info.plist` file. By registering your new app platform, you are allowing your app to communicate with the Appwrite API.</p>
28+
<p>From the options, choose to add a new <b>Apple</b> platform, select the iOS, macOS, watchOS or tvOS tab and add your app <u>name</u> and <u>bundle identifier</u>, Your bundle identifier can be found at the top of the <b>General</b> tab in your project settings, or in your <b>Info.plist</b> file. By registering your new app platform, you are allowing your app to communicate with the Appwrite API.</p>
2929

3030
<h2><a href="/docs/getting-started-for-apple#getSDK" id="getSDK">Get Appwrite Apple SDK</a></h2>
3131

3232
<h3>Using Xcode</h3>
3333

3434
<ol>
35-
<li>Select File > Swift Packages > Add Package Dependency</li>
35+
<li>Select File > Add Packages</li>
3636
<li>Search for the Appwrite SDK with the URL https://github.com/appwrite/sdk-for-apple</li>
37-
<li>Add version rules</li>
38-
<li>Select next and wait for package resolution to complete</li>
39-
<li>Make sure `Appwrite` is selected to add to your target and select finish</li>
37+
<li>In the right panel, select your target project and add your desired version rules</li>
38+
<li>Select Add Package and wait for package resolution to complete</li>
39+
<li>Make sure the Appwrite package product is checked and select Add Package again</li>
4040
</ol>
4141

4242
<h3>Using Swift Packages</h3>
4343

44-
<p>Add this to your Package.swift file: </p>
44+
<p>Add the following to your Package.swift file: </p>
4545

4646
<div class="ide" data-lang="swift" data-lang-label="Swift">
47-
<pre class="line-numbers"><code class="prism language-swift" data-prism>'dependencies: [
48-
.package(url: "https://github.com/appwrite/sdk-for-apple", from: "<?php echo $this->escape($appleVersion); ?>"),
49-
]')'</code></pre>
47+
<pre class="line-numbers"><code class="prism language-swift" data-prism>dependencies: [
48+
.package(
49+
name: "Appwrite",
50+
url: "https://github.com/appwrite/sdk-for-swift",
51+
.exact("<?php echo $this->escape($appleVersion); ?>")
52+
)
53+
]')</code></pre>
5054
</div>
5155

5256
<p>Then add the dependency to your target:</p>
5357

5458
<div class="ide" data-lang="swift" data-lang-label="Swift">
55-
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('targets: [
59+
<pre class="line-numbers"><code class="prism language-swift" data-prism>targets: [
5660
.target(
5761
name: "[YOUR_TARGET]",
5862
dependencies: [
59-
.product(name: "Appwrite", package: "sdk-for-apple")
63+
"Appwrite"
6064
]
6165
)
62-
]');?></code></pre>
66+
]</code></pre>
6367
</div>
6468

6569
<h3><a href="/docs/getting-started-for-apple#OAuthCallback" id="OAuthCallback">OAuth Callback</a></h3>
@@ -83,18 +87,18 @@ $appleVersion = $versions['apple'] ?? '';
8387
');?></code></pre>
8488
</div>
8589

86-
<p>If you're using UIKit, you'll also need to add a hook to your `SceneDelegate.swift` file to ensure cookies work correctly.</p>
90+
<p>If you're using UIKit, you'll also need to add a hook to your <b>SceneDelegate.swift</b> file to ensure cookies work correctly.</p>
8791

8892
<div class="ide" data-lang="swift" data-lang-label="Swift">
89-
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('targets: [
90-
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
91-
guard let url = URLContexts.first?.url,
92-
url.absoluteString.contains("appwrite-callback") else {
93-
return
94-
}
95-
WebAuthComponent.handleIncomingCookie(from: url)
93+
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('
94+
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
95+
guard let url = URLContexts.first?.url,
96+
url.absoluteString.contains("appwrite-callback") else {
97+
return
9698
}
97-
]');?></code></pre>
99+
WebAuthComponent.handleIncomingCookie(from: url)
100+
}
101+
');?></code></pre>
98102
</div>
99103

100104
<h2><a href="/docs/getting-started-for-apple#initSDK" id="initSDK">Init your SDK</a></h2>
@@ -103,11 +107,13 @@ $appleVersion = $versions['apple'] ?? '';
103107

104108
<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
105109
<pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
110+
import AppwriteModels
106111

107112
let client = Client()
108-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
109-
.setProject("5df5acd0d48c2") // Your project ID
110-
.setSelfSigned(status: true) // For self signed certificates, only use for development</code></pre>
113+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
114+
.setProject("5df5acd0d48c2") // Your project ID
115+
.setSelfSigned(status: true) // For self signed certificates, only use for development
116+
</code></pre>
111117
</div>
112118

113119
<p>Before sending any API calls to your new Appwrite project, make sure your device or iOS emulator has network access to your Appwrite project's hostname or IP address.</p>
@@ -120,10 +126,13 @@ let client = Client()
120126

121127
<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
122128
<pre class="line-numbers"><code class="prism language-swift" data-prism>// Register User
123-
let account = Account(client: client)
124-
let user = try await account.create(userId: "unique()", email: "[email protected]", password: "password")
125-
print(user.toMap())
126-
}</code></pre>
129+
let account = Account(client)
130+
131+
let user = try await account.create(
132+
userId: ID.unique(),
133+
134+
password: "password"
135+
)</code></pre>
127136
</div>
128137

129138
<h2><a href="/docs/getting-started-for-apple#listenToChanges" id="listenToChanges">Listen to Changes</a></h2>
@@ -132,7 +141,7 @@ print(user.toMap())
132141

133142
<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
134143
<pre class="line-numbers"><code class="prism language-swift" data-prism>// Subscribe to files channel
135-
let realtime = Realtime(client: client)
144+
let realtime = Realtime(client)
136145

137146
let subscription = realtime.subscribe(channels: ["files"]) { message in
138147
if(message.events!.contains("buckets.*.files.*.create")) {
@@ -146,21 +155,25 @@ let subscription = realtime.subscribe(channels: ["files"]) { message in
146155

147156
<h2><a href="/docs/getting-started-for-apple#fullExample" id="fullExample">Full Example</a></h2>
148157
<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
149-
<pre class="line-numbers"><code class="prism language-swift" data-prism>import io.appwrite.Client
150-
import Appwrite
158+
<pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
159+
import AppwriteModels
151160

152161
let client = Client()
153162
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
154-
.setProject("5df5acd0d48c2") // Your project ID
155-
.setSelfSigned(status: true) // For self signed certificates, only use for development
163+
.setProject("5df5acd0d48c2") // Your project ID
164+
.setSelfSigned(status: true) // For self signed certificates, only use for development
156165

157166
// Register User
158-
let account = Account(client: client)
159-
let user = try await account.create(userId: "unique()", email: "[email protected]", password: "password")
160-
print(user.toMap())
167+
let account = Account(client)
168+
169+
let user = try await account.create(
170+
userId: ID.unique(),
171+
172+
password: "password"
173+
)
161174

162175
// Subscribe to files channel
163-
let realtime = Realtime(client: client)
176+
let realtime = Realtime(client)
164177

165178
let subscription = realtime.subscribe(channels: ["files"]) { message in
166179
if(message.events!.contains("buckets.*.files.*.create")) {

0 commit comments

Comments
 (0)