/
Client.kt
538 lines (488 loc) Β· 16.4 KB
/
Client.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package io.appwrite
import android.content.Context
import android.content.pm.PackageManager
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.appwrite.appwrite.BuildConfig
import io.appwrite.cookies.stores.SharedPreferencesCookieStore
import io.appwrite.exceptions.AppwriteException
import io.appwrite.extensions.fromJson
import io.appwrite.json.PreciseNumberAdapter
import io.appwrite.models.InputFile
import io.appwrite.models.UploadProgress
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.*
import okhttp3.Headers.Companion.toHeaders
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.File
import java.io.RandomAccessFile
import java.io.IOException
import java.net.CookieManager
import java.net.CookiePolicy
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resume
class Client @JvmOverloads constructor(
context: Context,
var endPoint: String = "https://HOSTNAME/v1",
var endPointRealtime: String? = null,
private var selfSigned: Boolean = false
) : CoroutineScope {
companion object {
const val CHUNK_SIZE = 5*1024*1024; // 5MB
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
private val job = Job()
private val gson = GsonBuilder().registerTypeAdapter(
object : TypeToken<Map<String, Any>>(){}.type,
PreciseNumberAdapter()
).create()
lateinit var http: OkHttpClient
private val headers: MutableMap<String, String>
val config: MutableMap<String, String>
private val cookieJar = CookieManager(
SharedPreferencesCookieStore(context, "myCookie"),
CookiePolicy.ACCEPT_ALL
)
private val appVersion by lazy {
try {
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
return@lazy pInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
return@lazy ""
}
}
init {
headers = mutableMapOf(
"content-type" to "application/json",
"origin" to "appwrite-android://${context.packageName}",
"user-agent" to "${context.packageName}/${appVersion}, ${System.getProperty("http.agent")}",
"x-sdk-name" to "Android",
"x-sdk-platform" to "client",
"x-sdk-language" to "android",
"x-sdk-version" to "4.0.1",
"x-appwrite-response-format" to "1.4.0"
)
config = mutableMapOf()
setSelfSigned(selfSigned)
}
/**
* Set Project
*
* Your project ID
*
* @param {string} project
*
* @return this
*/
fun setProject(value: String): Client {
config["project"] = value
addHeader("x-appwrite-project", value)
return this
}
/**
* Set JWT
*
* Your secret JSON Web Token
*
* @param {string} jwt
*
* @return this
*/
fun setJWT(value: String): Client {
config["jWT"] = value
addHeader("x-appwrite-jwt", value)
return this
}
/**
* Set Locale
*
* @param {string} locale
*
* @return this
*/
fun setLocale(value: String): Client {
config["locale"] = value
addHeader("x-appwrite-locale", value)
return this
}
/**
* Set self Signed
*
* @param status
*
* @return this
*/
fun setSelfSigned(status: Boolean): Client {
selfSigned = status
val builder = OkHttpClient()
.newBuilder()
.cookieJar(JavaNetCookieJar(cookieJar))
if (!selfSigned) {
http = builder.build()
return this
}
try {
// Create a trust manager that does not validate certificate chains
val trustAllCerts = arrayOf<TrustManager>(
@Suppress("CustomX509TrustManager")
object : X509TrustManager {
@Suppress("TrustAllX509TrustManager")
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}
@Suppress("TrustAllX509TrustManager")
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
}
)
// Install the all-trusting trust manager
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
// Create an ssl socket factory with our all-trusting manager
val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier(HostnameVerifier { _, _ -> true })
http = builder.build()
} catch (e: Exception) {
throw RuntimeException(e)
}
return this
}
/**
* Set endpoint and realtime endpoint.
*
* @param endpoint
*
* @return this
*/
fun setEndpoint(endPoint: String): Client {
this.endPoint = endPoint
if (this.endPointRealtime == null && endPoint.startsWith("http")) {
this.endPointRealtime = endPoint.replaceFirst("http", "ws")
}
return this
}
/**
* Set realtime endpoint
*
* @param endpoint
*
* @return this
*/
fun setEndpointRealtime(endPoint: String): Client {
this.endPointRealtime = endPoint
return this
}
/**
* Add Header
*
* @param key
* @param value
*
* @return this
*/
fun addHeader(key: String, value: String): Client {
headers[key] = value
return this
}
/**
* Send the HTTP request
*
* @param method
* @param path
* @param headers
* @param params
*
* @return [T]
*/
@Throws(AppwriteException::class)
suspend fun <T> call(
method: String,
path: String,
headers: Map<String, String> = mapOf(),
params: Map<String, Any?> = mapOf(),
responseType: Class<T>,
converter: ((Any) -> T)? = null
): T {
val filteredParams = params.filterValues { it != null }
val requestHeaders = this.headers.toHeaders().newBuilder()
.addAll(headers.toHeaders())
.build()
val httpBuilder = (endPoint + path).toHttpUrl().newBuilder()
if ("GET" == method) {
filteredParams.forEach {
when (it.value) {
null -> {
return@forEach
}
is List<*> -> {
val list = it.value as List<*>
for (index in list.indices) {
httpBuilder.addQueryParameter(
"${it.key}[]",
list[index].toString()
)
}
}
else -> {
httpBuilder.addQueryParameter(it.key, it.value.toString())
}
}
}
val request = Request.Builder()
.url(httpBuilder.build())
.headers(requestHeaders)
.get()
.build()
return awaitResponse(request, responseType, converter)
}
val body = if (MultipartBody.FORM.toString() == headers["content-type"]) {
val builder = MultipartBody.Builder().setType(MultipartBody.FORM)
filteredParams.forEach {
when {
it.key == "file" -> {
builder.addPart(it.value as MultipartBody.Part)
}
it.value is List<*> -> {
val list = it.value as List<*>
for (index in list.indices) {
builder.addFormDataPart(
"${it.key}[]",
list[index].toString()
)
}
}
else -> {
builder.addFormDataPart(it.key, it.value.toString())
}
}
}
builder.build()
} else {
gson.toJson(filteredParams)
.toRequestBody("application/json".toMediaType())
}
val request = Request.Builder()
.url(httpBuilder.build())
.headers(requestHeaders)
.method(method, body)
.build()
return awaitResponse(request, responseType, converter)
}
/**
* Upload a file in chunks
*
* @param path
* @param headers
* @param params
*
* @return [T]
*/
@Throws(AppwriteException::class)
suspend fun <T> chunkedUpload(
path: String,
headers: MutableMap<String, String>,
params: MutableMap<String, Any?>,
responseType: Class<T>,
converter: ((Any) -> T),
paramName: String,
idParamName: String? = null,
onProgress: ((UploadProgress) -> Unit)? = null,
): T {
var file: RandomAccessFile? = null
val input = params[paramName] as InputFile
val size: Long = when(input.sourceType) {
"path", "file" -> {
file = RandomAccessFile(input.path, "r")
file.length()
}
"bytes" -> {
(input.data as ByteArray).size.toLong()
}
else -> throw UnsupportedOperationException()
}
if (size < CHUNK_SIZE) {
val data = when(input.sourceType) {
"file", "path" -> File(input.path).asRequestBody()
"bytes" -> (input.data as ByteArray).toRequestBody(input.mimeType.toMediaType())
else -> throw UnsupportedOperationException()
}
params[paramName] = MultipartBody.Part.createFormData(
paramName,
input.filename,
data
)
return call(
method = "POST",
path,
headers,
params,
responseType,
converter
)
}
val buffer = ByteArray(CHUNK_SIZE)
var offset = 0L
var result: Map<*, *>? = null
if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
// Make a request to check if a file already exists
val current = call(
method = "GET",
path = "$path/${params[idParamName]}",
headers = headers,
params = emptyMap(),
responseType = Map::class.java,
)
val chunksUploaded = current["chunksUploaded"] as Long
offset = chunksUploaded * CHUNK_SIZE
}
while (offset < size) {
when(input.sourceType) {
"file", "path" -> {
file!!.seek(offset)
file!!.read(buffer)
}
"bytes" -> {
val end = if (offset + CHUNK_SIZE < size) {
offset + CHUNK_SIZE - 1
} else {
size - 1
}
(input.data as ByteArray).copyInto(
buffer,
startIndex = offset.toInt(),
endIndex = end.toInt()
)
}
else -> throw UnsupportedOperationException()
}
params[paramName] = MultipartBody.Part.createFormData(
paramName,
input.filename,
buffer.toRequestBody()
)
headers["Content-Range"] =
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size - 1)}/$size"
result = call(
method = "POST",
path,
headers,
params,
responseType = Map::class.java
)
offset += CHUNK_SIZE
headers["x-appwrite-id"] = result!!["\$id"].toString()
onProgress?.invoke(
UploadProgress(
id = result!!["\$id"].toString(),
progress = offset.coerceAtMost(size).toDouble() / size * 100,
sizeUploaded = offset.coerceAtMost(size),
chunksTotal = result!!["chunksTotal"].toString().toInt(),
chunksUploaded = result!!["chunksUploaded"].toString().toInt(),
)
)
}
return converter(result as Map<String, Any>)
}
/**
* Await Response
*
* @param request
* @param responseType
* @param converter
*
* @return [T]
*/
@Throws(AppwriteException::class)
private suspend fun <T> awaitResponse(
request: Request,
responseType: Class<T>,
converter: ((Any) -> T)? = null
) = suspendCancellableCoroutine<T> {
http.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
if (it.isCancelled) {
return
}
it.cancel(e)
}
@Suppress("UNCHECKED_CAST")
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
val error = if (response.headers["content-type"]?.contains("application/json") == true) {
val map = gson.fromJson<Map<String, Any>>(
body,
object : TypeToken<Map<String, Any>>(){}.type
)
AppwriteException(
map["message"] as? String ?: "",
(map["code"] as Number).toInt(),
map["type"] as? String ?: "",
body
)
} else {
AppwriteException(body, response.code)
}
it.cancel(error)
return
}
when {
responseType == Boolean::class.java -> {
it.resume(true as T)
return
}
responseType == ByteArray::class.java -> {
it.resume(response.body!!
.byteStream()
.buffered()
.use(BufferedInputStream::readBytes) as T
)
return
}
response.body == null -> {
it.resume(true as T)
return
}
}
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
if (body.isEmpty()) {
it.resume(true as T)
return
}
val map = gson.fromJson<Any>(
body,
object : TypeToken<Any>(){}.type
)
it.resume(
converter?.invoke(map) ?: map as T
)
}
})
}
}