Skip to content

Commit ab4d24f

Browse files
committed
Realtime snapshot
1 parent 8434e38 commit ab4d24f

21 files changed

+2428
-144
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
![Maven Central](https://img.shields.io/maven-central/v/io.appwrite/sdk-for-android.svg?color=green&style=flat-square)
44
![License](https://img.shields.io/github/license/appwrite/sdk-for-android.svg?style=flat-square)
5-
![Version](https://img.shields.io/badge/api%20version-0.9.0-blue.svg?style=flat-square)
5+
![Version](https://img.shields.io/badge/api%20version-0.8.0-blue.svg?style=flat-square)
66
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
77
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite_io?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite_io)
88
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
99

10-
**This SDK is compatible with Appwrite server version 0.9.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-android/releases).**
10+
**This SDK is compatible with Appwrite server version 0.10.x. For older versions, please check previous releases.**
1111

12-
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Android SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
12+
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Flutter SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to https://appwrite.io/docs
1313

14-
![Appwrite](https://appwrite.io/images/github.png)
14+
![Appwrite](https://appwrite.io/v1/images/console.png)
1515

1616
## Installation
1717

@@ -38,7 +38,7 @@ repositories {
3838
Next, add the dependency to your project's `build.gradle(.kts)` file:
3939

4040
```groovy
41-
implementation("io.appwrite:sdk-for-android:0.0.1")
41+
implementation("io.appwrite:sdk-for-android:0.2.0-SNAPSHOT")
4242
```
4343

4444
### Maven
@@ -49,7 +49,7 @@ Add this to your project's `pom.xml` file:
4949
<dependency>
5050
<groupId>io.appwrite</groupId>
5151
<artifactId>sdk-for-android</artifactId>
52-
<version>0.0.1</version>
52+
<version>0.2.0-SNAPSHOT</version>
5353
</dependency>
5454
</dependencies>
5555
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'io.github.gradle-nexus.publish-plugin'
33
// Top-level build file where you can add configuration options common to all sub-projects/modules.
44
buildscript {
55
ext.kotlin_version = "1.4.31"
6-
version '0.0.1'
6+
version '0.2.0-SNAPSHOT'
77
repositories {
88
maven { url "https://plugins.gradle.org/m2/" }
99
google()

docs/examples/java/account/get-session.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

docs/examples/kotlin/account/get-session.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

library/build.gradle

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ plugins {
77
ext {
88
PUBLISH_GROUP_ID = 'io.appwrite'
99
PUBLISH_ARTIFACT_ID = 'sdk-for-android'
10-
PUBLISH_VERSION = '0.0.1'
10+
PUBLISH_VERSION = '0.2.0-SNAPSHOT'
1111
POM_URL = 'https://github.com/appwrite/sdk-for-android'
1212
POM_SCM_URL = 'https://github.com/appwrite/sdk-for-android'
1313
POM_ISSUE_URL = 'https://github.com/appwrite/sdk-for-android/issues'
14-
POM_DESCRIPTION = 'Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Android SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)'
14+
POM_DESCRIPTION = 'Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Flutter SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to https://appwrite.io/docs'
1515
POM_LICENSE_URL = 'https://opensource.org/licenses/GPL-3.0'
1616
POM_LICENSE_NAME = "GPL-3.0"
1717
POM_DEVELOPER_ID = 'appwrite'
@@ -53,8 +53,8 @@ android {
5353

5454
dependencies {
5555
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${KotlinCompilerVersion.VERSION}")
56-
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
57-
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3")
56+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1")
57+
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0")
5858

5959
api(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
6060
api("com.squareup.okhttp3:okhttp")
@@ -65,15 +65,16 @@ dependencies {
6565
implementation("net.gotev:cookie-store:1.3.5")
6666
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
6767
implementation("androidx.lifecycle:lifecycle-common-java8:2.3.1")
68-
implementation("androidx.appcompat:appcompat:1.2.0")
69-
implementation("androidx.fragment:fragment-ktx:1.3.2")
70-
implementation("androidx.activity:activity-ktx:1.2.2")
68+
implementation("androidx.appcompat:appcompat:1.3.1")
69+
implementation("androidx.fragment:fragment-ktx:1.3.6")
70+
implementation("androidx.activity:activity-ktx:1.3.1")
7171
implementation("androidx.browser:browser:1.3.0")
7272

7373
testImplementation 'junit:junit:4.+'
74-
testImplementation "androidx.test.ext:junit-ktx:1.1.2"
75-
testImplementation "androidx.test:core-ktx:1.3.0"
74+
testImplementation "androidx.test.ext:junit-ktx:1.1.3"
75+
testImplementation "androidx.test:core-ktx:1.4.0"
7676
testImplementation "org.robolectric:robolectric:4.5.1"
77+
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1")
7778
}
7879

7980
apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"

library/src/main/java/io/appwrite/Client.kt

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import android.content.pm.PackageManager
55
import com.google.gson.Gson
66
import io.appwrite.appwrite.BuildConfig
77
import io.appwrite.exceptions.AppwriteException
8-
import io.appwrite.extensions.JsonExtensions.fromJson
9-
import io.appwrite.models.Error
8+
import io.appwrite.extensions.fromJson
109
import kotlinx.coroutines.CoroutineScope
1110
import kotlinx.coroutines.Dispatchers
1211
import kotlinx.coroutines.Job
@@ -36,6 +35,7 @@ import kotlin.coroutines.resume
3635
class Client @JvmOverloads constructor(
3736
context: Context,
3837
var endPoint: String = "https://appwrite.io/v1",
38+
var endPointRealtime: String? = null,
3939
private var selfSigned: Boolean = false
4040
) : CoroutineScope {
4141

@@ -71,7 +71,7 @@ class Client @JvmOverloads constructor(
7171
"origin" to "appwrite-android://${context.packageName}",
7272
"user-agent" to "${context.packageName}/${appVersion}, ${System.getProperty("http.agent")}",
7373
"x-sdk-version" to "appwrite:android:${BuildConfig.SDK_VERSION}",
74-
"x-appwrite-response-format" to "0.9.0"
74+
"x-appwrite-response-format" to "0.10.0"
7575
)
7676
config = mutableMapOf()
7777

@@ -93,6 +93,21 @@ class Client @JvmOverloads constructor(
9393
return this
9494
}
9595

96+
/**
97+
* Set Key
98+
*
99+
* Your secret API key
100+
*
101+
* @param {string} key
102+
*
103+
* @return this
104+
*/
105+
fun setKey(value: String): Client {
106+
config["key"] = value
107+
addHeader("x-appwrite-key", value)
108+
return this
109+
}
110+
96111
/**
97112
* Set JWT
98113
*
@@ -121,6 +136,19 @@ class Client @JvmOverloads constructor(
121136
return this
122137
}
123138

139+
/**
140+
* Set Mode
141+
*
142+
* @param {string} mode
143+
*
144+
* @return this
145+
*/
146+
fun setMode(value: String): Client {
147+
config["mode"] = value
148+
addHeader("x-appwrite-mode", value)
149+
return this
150+
}
151+
124152
/**
125153
* Set self Signed
126154
*
@@ -171,14 +199,31 @@ class Client @JvmOverloads constructor(
171199
}
172200

173201
/**
174-
* Set endpoint
202+
* Set endpoint and realtime endpoint.
175203
*
176204
* @param endpoint
177205
*
178206
* @return this
179207
*/
180208
fun setEndpoint(endPoint: String): Client {
181209
this.endPoint = endPoint
210+
211+
if (this.endPointRealtime == null && endPoint.startsWith("http")) {
212+
this.endPointRealtime = endPoint.replaceFirst("http", "ws")
213+
}
214+
215+
return this
216+
}
217+
218+
/**
219+
* Set realtime endpoint
220+
*
221+
* @param endpoint
222+
*
223+
* @return this
224+
*/
225+
fun setEndpointRealtime(endPoint: String): Client {
226+
this.endPointRealtime = endPoint
182227
return this
183228
}
184229

@@ -317,9 +362,9 @@ class Client @JvmOverloads constructor(
317362

318363
val contentType: String = response.headers["content-type"] ?: ""
319364
val error = if (contentType.contains("application/json", ignoreCase = true)) {
320-
bodyString.fromJson(Error::class.java)
365+
bodyString.fromJson<AppwriteException>()
321366
} else {
322-
Error(bodyString, response.code)
367+
AppwriteException(bodyString, response.code)
323368
}
324369

325370
it.cancel(AppwriteException(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.appwrite.extensions
2+
3+
import kotlinx.coroutines.Dispatchers.IO
4+
import kotlinx.coroutines.async
5+
import kotlinx.coroutines.awaitAll
6+
import kotlinx.coroutines.withContext
7+
8+
suspend fun <T> Collection<T>.forEachAsync(
9+
callback: suspend (T) -> Unit
10+
) = withContext(IO) {
11+
map { async { callback.invoke(it) } }.awaitAll()
12+
}

library/src/main/java/io/appwrite/extensions/JsonExtensions.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@ package io.appwrite.extensions
22

33
import com.google.gson.Gson
44

5-
object JsonExtensions {
5+
val gson = Gson()
66

7-
fun Any.toJson(): String =
8-
Gson().toJson(this)
7+
fun Any.toJson(): String =
8+
gson.toJson(this)
99

10-
fun <T> String.fromJson(clazz: Class<T>): T =
11-
Gson().fromJson(this, clazz)
12-
}
10+
fun <T> String.fromJson(clazz: Class<T>): T =
11+
gson.fromJson(this, clazz)
12+
13+
inline fun <reified T> String.fromJson(): T =
14+
gson.fromJson(this, T::class.java)
15+
16+
fun <T> Any.jsonCast(to: Class<T>): T =
17+
toJson().fromJson(to)
18+
19+
inline fun <reified T> Any.jsonCast(): T =
20+
toJson().fromJson(T::class.java)
21+
22+
fun <T> Any.tryJsonCast(to: Class<T>): T? = try {
23+
toJson().fromJson(to)
24+
} catch (ex: Exception) {
25+
ex.printStackTrace()
26+
null
27+
}
28+
29+
inline fun <reified T> Any.tryJsonCast(): T? = try {
30+
toJson().fromJson(T::class.java)
31+
} catch (ex: Exception) {
32+
ex.printStackTrace()
33+
null
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.appwrite.models
2+
3+
import java.io.Closeable
4+
5+
data class RealtimeSubscription(
6+
private val close: () -> Unit
7+
) : Closeable {
8+
override fun close() = close.invoke()
9+
}
10+
11+
data class RealtimeCallback(
12+
val payloadClass: Class<*>,
13+
val callback: (RealtimeResponseEvent<*>) -> Unit
14+
)
15+
16+
open class RealtimeResponse(
17+
val type: String,
18+
val data: Any
19+
)
20+
21+
data class RealtimeResponseEvent<T>(
22+
val event: String,
23+
val channels: Collection<String>,
24+
val timestamp: Long,
25+
var payload: T
26+
)
27+
28+
enum class RealtimeCode(val value: Int) {
29+
POLICY_VIOLATION(1008),
30+
UNKNOWN_ERROR(-1)
31+
}

0 commit comments

Comments
 (0)