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

Commit

Permalink
Merge pull request #247 from appwrite/refactor-code-samples
Browse files Browse the repository at this point in the history
Refactor code samples
  • Loading branch information
christyjacob4 authored Sep 15, 2022
2 parents b3691df + 8ebc599 commit c9bcd63
Show file tree
Hide file tree
Showing 14 changed files with 1,005 additions and 840 deletions.
516 changes: 296 additions & 220 deletions app/views/docs/databases.phtml

Large diffs are not rendered by default.

71 changes: 23 additions & 48 deletions app/views/docs/functions.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,10 @@ $image = new View(__DIR__.'/../general/image.phtml');
<div class="ide margin-bottom" data-lang="javascript" data-lang-label="Web SDK">
<pre class="line-numbers"><code class="prism language-javascript" data-prism>import { Client, Functions } from "appwrite";

const client = new Client();

client
const client = new Client()
.setEndpoint('https://[HOSTNAME_OR_IP]/v1')
.setProject('[PROJECT_ID]');

const databases = new Databases(client, '[DATABASE_ID]');

let promise = functions.createExecution('[FUNCTION_ID]');

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

void main() { // Init SDK
Client client = Client();
Functions functions = Functions(client);
void main() async {
final client = Client()
.setEndpoint('https://[HOSTNAME_OR_IP]/v1')
.setProject('[PROJECT_ID]');

client
.setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
;
final functions = Functions(client);

Future result = functions.createExecution(
functionId: '[FUNCTION_ID]',
);

result
.then((response) {
print(response);
}).catchError((error) {
print(error.response);
});
final execution = await functions.createExecution(
functionId: '[FUNCTION_ID]'
);
}</code></pre>
</div>
</li>
Expand All @@ -594,48 +581,36 @@ void main() { // Init SDK
<div class="ide margin-bottom" data-lang="swift" data-lang-label="Apple SDK">
<pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite

func main() {
func main() async throws {
let client = Client()
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setEndpoint("https://[HOSTNAME_OR_IP]/v1")
.setProject("[PROJECT_ID]")

let functions = Functions(client)
let execution = functions.createExecution(

let execution = try await functions.createExecution(
functionId: "[FUNCTION_ID]"
)
print(execution.toMap())
}</code></pre>
</div>
</li>
<li>
<h3>Android</h3>

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

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val client = Client(applicationContext)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
suspend fun main() {
val client = Client(applicationContext)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1")
.setProject("[PROJECT_ID]")

val functions = Functions(client)
val functions = Functions(client)

GlobalScope.launch {
val response = functions.createExecution(
functionId = "[FUNCTION_ID]",
)
val json = response.body?.string()
}
}
val execution = functions.createExecution(
functionId = "[FUNCTION_ID]"
)
}</code></pre>
</div>
</li>
Expand Down
47 changes: 27 additions & 20 deletions app/views/docs/getting-started-for-android.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ $androidVersion = (isset($versions['android'])) ? $versions['android'] : '';
}');?></code></pre>
</div>

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

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

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

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

<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>
Expand All @@ -90,8 +90,12 @@ val client = Client(context)
<div class="ide" data-lang="android" data-lang-label="Android SDK">
<pre class="line-numbers"><code class="prism language-kotlin" data-prism>// Register User
val account = Account(client)
val response = account.create("unique()", "email@example.com", "password")
val json = response.body?.string()</code></pre>

val user = account.create(
userId = ID.unique(),
email = "email@example.com",
password = "password"
)</code></pre>
</div>

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

realtime.subscribe("files", callback = { response ->
if(response.events.contains("buckets.*.files.*.create")) {
val subscription = realtime.subscribe("files") {
if(it.events.contains("buckets.*.files.*.create")) {
// Log when a new file is uploaded
print(response.payload.toString());
print(it.payload.toString());
}
})
</code></pre>
Expand All @@ -117,24 +121,27 @@ realtime.subscribe("files", callback = { response ->
import io.appwrite.services.Account

val client = Client(context)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setSelfSigned(status: true) // For self signed certificates, only use for development
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setSelfSigned(status: true) // For self signed certificates, only use for development

// Register User
val account = Account(client)
val response = account.create("unique()", "email@example.com", "password")
val json = response.body?.string()

val user = account.create(
userId = ID.unique(),
email = "email@example.com",
password = "password"
)

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

realtime.subscribe("files", callback = { response ->
if(response.events.contains("buckets.*.files.*.create")) {
val subscription = realtime.subscribe("files") {
if(it.events.contains("buckets.*.files.*.create")) {
// Log when a new file is uploaded
print(response.payload.toString());
print(it.payload.toString());
}
})</code></pre>
}</code></pre>
</div>

<h2><a href="/docs/getting-started-for-android#nextSteps" id="nextSteps">Next Steps</a></h2>
Expand Down
89 changes: 51 additions & 38 deletions app/views/docs/getting-started-for-apple.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,47 @@ $appleVersion = $versions['apple'] ?? '';

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

<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>
<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>

<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>
<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>

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

<h3>Using Xcode</h3>

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

<h3>Using Swift Packages</h3>

<p>Add this to your Package.swift file: </p>
<p>Add the following to your Package.swift file: </p>

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

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

<div class="ide" data-lang="swift" data-lang-label="Swift">
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('targets: [
<pre class="line-numbers"><code class="prism language-swift" data-prism>targets: [
.target(
name: "[YOUR_TARGET]",
dependencies: [
.product(name: "Appwrite", package: "sdk-for-apple")
"Appwrite"
]
)
]');?></code></pre>
]</code></pre>
</div>

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

<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>
<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>

<div class="ide" data-lang="swift" data-lang-label="Swift">
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('targets: [
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url,
url.absoluteString.contains("appwrite-callback") else {
return
}
WebAuthComponent.handleIncomingCookie(from: url)
<pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url,
url.absoluteString.contains("appwrite-callback") else {
return
}
]');?></code></pre>
WebAuthComponent.handleIncomingCookie(from: url)
}
');?></code></pre>
</div>

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

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

let client = Client()
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setSelfSigned(status: true) // For self signed certificates, only use for development</code></pre>
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setSelfSigned(status: true) // For self signed certificates, only use for development
</code></pre>
</div>

<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>
Expand All @@ -120,10 +126,13 @@ let client = Client()

<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
<pre class="line-numbers"><code class="prism language-swift" data-prism>// Register User
let account = Account(client: client)
let user = try await account.create(userId: "unique()", email: "email@example.com", password: "password")
print(user.toMap())
}</code></pre>
let account = Account(client)

let user = try await account.create(
userId: ID.unique(),
email: "email@example.com",
password: "password"
)</code></pre>
</div>

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

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

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

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

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

// Register User
let account = Account(client: client)
let user = try await account.create(userId: "unique()", email: "email@example.com", password: "password")
print(user.toMap())
let account = Account(client)

let user = try await account.create(
userId: ID.unique(),
email: "email@example.com",
password: "password"
)

// Subscribe to files channel
let realtime = Realtime(client: client)
let realtime = Realtime(client)

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

0 comments on commit c9bcd63

Please sign in to comment.