Skip to content

Commit 31ee845

Browse files
Merge branch 'master' into feat-route-muliplexing
2 parents 1ab3a80 + f0d2ea2 commit 31ee845

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+456
-162
lines changed

templates/android/library/src/main/java/io/package/Client.kt.twig

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Client @JvmOverloads constructor(
6060
internal lateinit var http: OkHttpClient
6161

6262
internal val headers: MutableMap<String, String>
63-
63+
6464
val config: MutableMap<String, String>
6565

6666
internal val cookieJar = ListenableCookieJar(CookieManager(
@@ -87,14 +87,14 @@ class Client @JvmOverloads constructor(
8787
"x-sdk-platform" to "{{ sdk.platform }}",
8888
"x-sdk-language" to "{{ language.name | caseLower }}",
8989
"x-sdk-version" to "{{ sdk.version }}"{% if spec.global.defaultHeaders | length > 0 %},{% endif %}
90-
90+
9191
{% for key,header in spec.global.defaultHeaders %}
9292
"{{ key | caseLower }}" to "{{ header }}"{% if not loop.last %},{% endif %}
9393
{% endfor %}
9494

9595
)
9696
config = mutableMapOf()
97-
97+
9898
setSelfSigned(selfSigned)
9999
}
100100

@@ -119,10 +119,10 @@ class Client @JvmOverloads constructor(
119119
{% endfor %}
120120
/**
121121
* Set self Signed
122-
*
122+
*
123123
* @param status
124124
*
125-
* @return this
125+
* @return this
126126
*/
127127
fun setSelfSigned(status: Boolean): Client {
128128
selfSigned = status
@@ -171,10 +171,10 @@ class Client @JvmOverloads constructor(
171171

172172
/**
173173
* Set endpoint and realtime endpoint.
174-
*
174+
*
175175
* @param endpoint
176176
*
177-
* @return this
177+
* @return this
178178
*/
179179
fun setEndpoint(endpoint: String): Client {
180180
this.endpoint = endpoint
@@ -200,32 +200,51 @@ class Client @JvmOverloads constructor(
200200

201201
/**
202202
* Add Header
203-
*
203+
*
204204
* @param key
205205
* @param value
206206
*
207-
* @return this
207+
* @return this
208208
*/
209209
fun addHeader(key: String, value: String): Client {
210210
headers[key] = value
211211
return this
212212
}
213213

214+
/**
215+
* Sends a "ping" request to Appwrite to verify connectivity.
216+
*
217+
* @return String
218+
*/
219+
suspend fun ping(): String {
220+
val apiPath = "/ping"
221+
val apiParams = mutableMapOf<String, Any?>()
222+
val apiHeaders = mutableMapOf("content-type" to "application/json")
223+
224+
return call(
225+
"GET",
226+
apiPath,
227+
apiHeaders,
228+
apiParams,
229+
responseType = String::class.java
230+
)
231+
}
232+
214233
/**
215234
* Send the HTTP request
216-
*
235+
*
217236
* @param method
218237
* @param path
219238
* @param headers
220239
* @param params
221240
*
222-
* @return [T]
241+
* @return [T]
223242
*/
224243
@Throws({{ spec.title | caseUcfirst }}Exception::class)
225244
suspend fun <T> call(
226-
method: String,
227-
path: String,
228-
headers: Map<String, String> = mapOf(),
245+
method: String,
246+
path: String,
247+
headers: Map<String, String> = mapOf(),
229248
params: Map<String, Any?> = mapOf(),
230249
responseType: Class<T>,
231250
converter: ((Any) -> T)? = null
@@ -363,7 +382,7 @@ class Client @JvmOverloads constructor(
363382
var offset = 0L
364383
var result: Map<*, *>? = null
365384

366-
if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
385+
if (idParamName?.isNotEmpty() == true) {
367386
// Make a request to check if a file already exists
368387
val current = call(
369388
method = "GET",
@@ -460,14 +479,14 @@ class Client @JvmOverloads constructor(
460479
.charStream()
461480
.buffered()
462481
.use(BufferedReader::readText)
463-
482+
464483
val error = if (response.headers["content-type"]?.contains("application/json") == true) {
465484
val map = body.fromJson<Map<String, Any>>()
466485

467486
{{ spec.title | caseUcfirst }}Exception(
468-
map["message"] as? String ?: "",
487+
map["message"] as? String ?: "",
469488
(map["code"] as Number).toInt(),
470-
map["type"] as? String ?: "",
489+
map["type"] as? String ?: "",
471490
body
472491
)
473492
} else {
@@ -489,6 +508,14 @@ class Client @JvmOverloads constructor(
489508
it.resume(true as T)
490509
return
491510
}
511+
responseType == String::class.java -> {
512+
val body = response.body!!
513+
.charStream()
514+
.buffered()
515+
.use(BufferedReader::readText)
516+
it.resume(body as T)
517+
return
518+
}
492519
responseType == ByteArray::class.java -> {
493520
it.resume(response.body!!
494521
.byteStream()
@@ -519,4 +546,4 @@ class Client @JvmOverloads constructor(
519546
}
520547
})
521548
}
522-
}
549+
}

templates/android/library/src/main/java/io/package/models/RealtimeModels.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ data class RealtimeCallback(
1717

1818
open class RealtimeResponse(
1919
val type: String,
20-
val data: Any
20+
val data: Any?
2121
)
2222

2323
data class RealtimeResponseEvent<T>(

templates/android/library/src/main/java/io/package/services/Realtime.kt.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,17 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
175175
when (message.type) {
176176
TYPE_ERROR -> handleResponseError(message)
177177
TYPE_EVENT -> handleResponseEvent(message)
178+
TYPE_PONG -> {}
178179
}
179180
}
180181
}
181182

182183
private fun handleResponseError(message: RealtimeResponse) {
183-
throw message.data.jsonCast<{{ spec.title | caseUcfirst }}Exception>()
184+
throw message.data?.jsonCast<{{ spec.title | caseUcfirst }}Exception>() ?: RuntimeException("Data is not present")
184185
}
185186

186187
private suspend fun handleResponseEvent(message: RealtimeResponse) {
187-
val event = message.data.jsonCast<RealtimeResponseEvent<Any>>()
188+
val event = message.data?.jsonCast<RealtimeResponseEvent<Any>>() ?: return
188189
if (event.channels.isEmpty()) {
189190
return
190191
}

templates/apple/Sources/Client.swift.twig

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ open class Client {
214214
) ?? ""
215215
}
216216

217+
///
218+
/// Sends a "ping" request to Appwrite to verify connectivity.
219+
///
220+
/// @return String
221+
/// @throws Exception
222+
///
223+
open func ping() async throws -> String {
224+
let apiPath: String = "/ping"
225+
226+
let apiHeaders: [String: String] = [
227+
"content-type": "application/json"
228+
]
229+
230+
return try await call(
231+
method: "GET",
232+
path: apiPath,
233+
headers: apiHeaders
234+
)
235+
}
236+
217237
///
218238
/// Make an API call
219239
///
@@ -284,6 +304,8 @@ open class Client {
284304
}
285305
}
286306

307+
var data = try await response.body.collect(upTo: Int.max)
308+
287309
switch response.status.code {
288310
case 0..<400:
289311
if response.headers["Set-Cookie"].count > 0 {
@@ -296,10 +318,11 @@ open class Client {
296318
switch T.self {
297319
case is Bool.Type:
298320
return true as! T
321+
case is String.Type:
322+
return (data.readString(length: data.readableBytes) ?? "") as! T
299323
case is ByteBuffer.Type:
300-
return try await response.body.collect(upTo: Int.max) as! T
324+
return data as! T
301325
default:
302-
let data = try await response.body.collect(upTo: Int.max)
303326
if data.readableBytes == 0 {
304327
return true as! T
305328
}
@@ -309,7 +332,6 @@ open class Client {
309332
}
310333
default:
311334
var message = ""
312-
var data = try await response.body.collect(upTo: Int.max)
313335
var type = ""
314336

315337
do {
@@ -365,7 +387,7 @@ open class Client {
365387
var offset = 0
366388
var result = [String:Any]()
367389

368-
if idParamName != nil && params[idParamName!] as! String != "unique()" {
390+
if idParamName != nil {
369391
// Make a request to check if a file already exists
370392
do {
371393
let map = try await call(

templates/cli/lib/commands/init.js.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ const initFunction = async () => {
299299
$id: functionId,
300300
name: answers.name,
301301
runtime: answers.runtime.id,
302+
specification: answers.specification,
302303
execute: ["any"],
303304
events: [],
304305
scopes: ["users.read"],

templates/cli/lib/commands/push.js.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
10951095
response = await functionsUpdate({
10961096
functionId: func['$id'],
10971097
name: func.name,
1098+
specification: func.specification,
10981099
execute: func.execute,
10991100
events: func.events,
11001101
schedule: func.schedule,
@@ -1126,6 +1127,7 @@ const pushFunction = async ({ functionId, async, code, withVariables } = { retur
11261127
functionId: func.$id,
11271128
name: func.name,
11281129
runtime: func.runtime,
1130+
specification: func.specification,
11291131
execute: func.execute,
11301132
events: func.events,
11311133
schedule: func.schedule,

templates/cli/lib/config.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const process = require("process");
55
const JSONbig = require("json-bigint")({ storeAsString: false });
66

77
const KeysVars = new Set(["key", "value"]);
8-
const KeysFunction = new Set(["path", "$id", "execute", "name", "enabled", "logging", "runtime", "scopes", "events", "schedule", "timeout", "entrypoint", "commands", "vars"]);
8+
const KeysFunction = new Set(["path", "$id", "execute", "name", "enabled", "logging", "runtime", "specification", "scopes", "events", "schedule", "timeout", "entrypoint", "commands", "vars"]);
99
const KeysDatabase = new Set(["$id", "name", "enabled"]);
1010
const KeysCollection = new Set(["$id", "$permissions", "databaseId", "name", "enabled", "documentSecurity", "attributes", "indexes"]);
1111
const KeysStorage = new Set(["$id", "$permissions", "fileSecurity", "name", "enabled", "maximumFileSize", "allowedFileExtensions", "compression", "encryption", "antivirus"]);

templates/cli/lib/questions.js.twig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Client = require("./client");
33
const { localConfig, globalConfig } = require('./config');
44
const { projectsList } = require('./commands/projects');
55
const { teamsList } = require('./commands/teams');
6-
const { functionsListRuntimes, functionsList } = require('./commands/functions');
6+
const { functionsListRuntimes, functionsListSpecifications, functionsList } = require('./commands/functions');
77
const { accountListMfaFactors } = require("./commands/account");
88
const { sdkForConsole } = require("./sdks");
99
const { validateRequired } = require("./validations");
@@ -301,6 +301,25 @@ const questionsCreateFunction = [
301301
})
302302
return choices;
303303
},
304+
},
305+
{
306+
type: "list",
307+
name: "specification",
308+
message: "What specification would you like to use?",
309+
choices: async () => {
310+
let response = await functionsListSpecifications({
311+
parseOutput: false
312+
})
313+
let specifications = response["specifications"]
314+
let choices = specifications.map((spec, idx) => {
315+
return {
316+
name: `${spec.cpus} CPU, ${spec.memory}MB RAM`,
317+
value: spec.slug,
318+
disabled: spec.enabled === false ? 'Upgrade to use' : false
319+
}
320+
})
321+
return choices;
322+
},
304323
}
305324
];
306325

templates/dart/lib/src/client.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ abstract class Client {
4848
/// Add headers that should be sent with all API calls.
4949
Client addHeader(String key, String value);
5050

51+
/// Sends a "ping" request to Appwrite to verify connectivity.
52+
Future<String> ping();
53+
5154
/// Upload a file in chunks.
5255
Future<Response> chunkedUpload({
5356
required String path,

templates/dart/lib/src/client_base.dart.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ abstract class ClientBase implements Client {
2020
@override
2121
ClientBase addHeader(String key, String value);
2222

23+
@override
24+
Future<String> ping();
25+
2326
@override
2427
Future<Response> call(
2528
HttpMethod method, {

0 commit comments

Comments
 (0)