diff --git a/templates/android/library/src/main/java/io/package/Client.kt.twig b/templates/android/library/src/main/java/io/package/Client.kt.twig index 7ddeac404..ea2922f4d 100644 --- a/templates/android/library/src/main/java/io/package/Client.kt.twig +++ b/templates/android/library/src/main/java/io/package/Client.kt.twig @@ -60,7 +60,7 @@ class Client @JvmOverloads constructor( internal lateinit var http: OkHttpClient internal val headers: MutableMap - + val config: MutableMap internal val cookieJar = ListenableCookieJar(CookieManager( @@ -87,14 +87,14 @@ class Client @JvmOverloads constructor( "x-sdk-platform" to "{{ sdk.platform }}", "x-sdk-language" to "{{ language.name | caseLower }}", "x-sdk-version" to "{{ sdk.version }}"{% if spec.global.defaultHeaders | length > 0 %},{% endif %} - + {% for key,header in spec.global.defaultHeaders %} "{{ key | caseLower }}" to "{{ header }}"{% if not loop.last %},{% endif %} {% endfor %} ) config = mutableMapOf() - + setSelfSigned(selfSigned) } @@ -119,10 +119,10 @@ class Client @JvmOverloads constructor( {% endfor %} /** * Set self Signed - * + * * @param status * - * @return this + * @return this */ fun setSelfSigned(status: Boolean): Client { selfSigned = status @@ -171,10 +171,10 @@ class Client @JvmOverloads constructor( /** * Set endpoint and realtime endpoint. - * + * * @param endpoint * - * @return this + * @return this */ fun setEndpoint(endpoint: String): Client { this.endpoint = endpoint @@ -200,11 +200,11 @@ class Client @JvmOverloads constructor( /** * Add Header - * + * * @param key * @param value * - * @return this + * @return this */ fun addHeader(key: String, value: String): Client { headers[key] = value @@ -213,19 +213,19 @@ class Client @JvmOverloads constructor( /** * Send the HTTP request - * + * * @param method * @param path * @param headers * @param params * - * @return [T] + * @return [T] */ @Throws({{ spec.title | caseUcfirst }}Exception::class) suspend fun call( - method: String, - path: String, - headers: Map = mapOf(), + method: String, + path: String, + headers: Map = mapOf(), params: Map = mapOf(), responseType: Class, converter: ((Any) -> T)? = null @@ -364,16 +364,22 @@ class Client @JvmOverloads constructor( 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 + try { + // 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 + } catch (e: Exception) { + if (e.message != null && !e.message!!.contains("file could not be found")) { + throw e + } + } } while (offset < size) { @@ -460,14 +466,14 @@ class Client @JvmOverloads constructor( .charStream() .buffered() .use(BufferedReader::readText) - + val error = if (response.headers["content-type"]?.contains("application/json") == true) { val map = body.fromJson>() {{ spec.title | caseUcfirst }}Exception( - map["message"] as? String ?: "", + map["message"] as? String ?: "", (map["code"] as Number).toInt(), - map["type"] as? String ?: "", + map["type"] as? String ?: "", body ) } else { @@ -519,4 +525,4 @@ class Client @JvmOverloads constructor( } }) } -} \ No newline at end of file +} diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index d970c5101..d6e16d49e 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -172,7 +172,7 @@ class Client @JvmOverloads constructor( /** * Prepare the HTTP request - * + * * @param method * @param path * @param headers @@ -267,7 +267,7 @@ class Client @JvmOverloads constructor( * @param headers * @param params * - * @return [T] + * @return [T] */ @Throws({{ spec.title | caseUcfirst }}Exception::class) suspend fun call( @@ -290,7 +290,7 @@ class Client @JvmOverloads constructor( * @param headers * @param params * - * @return [T] + * @return [T] */ @Throws({{ spec.title | caseUcfirst }}Exception::class) suspend fun redirect( @@ -363,16 +363,22 @@ class Client @JvmOverloads constructor( 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 + try { + // 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 + } catch (e: Exception) { + if (e.message != null && !e.message!!.contains("file could not be found")) { + throw e + } + } } while (offset < size) { @@ -429,7 +435,7 @@ class Client @JvmOverloads constructor( return converter(result as Map) } - /** + /** * Await Redirect * * @param request @@ -456,14 +462,14 @@ class Client @JvmOverloads constructor( .charStream() .buffered() .use(BufferedReader::readText) - + val error = if (response.headers["content-type"]?.contains("application/json") == true) { val map = body.fromJson>() {{ spec.title | caseUcfirst }}Exception( - map["message"] as? String ?: "", + map["message"] as? String ?: "", (map["code"] as Number).toInt(), - map["type"] as? String ?: "", + map["type"] as? String ?: "", body ) } else { @@ -507,14 +513,14 @@ class Client @JvmOverloads constructor( .charStream() .buffered() .use(BufferedReader::readText) - + val error = if (response.headers["content-type"]?.contains("application/json") == true) { val map = body.fromJson>() {{ spec.title | caseUcfirst }}Exception( - map["message"] as? String ?: "", + map["message"] as? String ?: "", (map["code"] as Number).toInt(), - map["type"] as? String ?: "", + map["type"] as? String ?: "", body ) } else { @@ -564,4 +570,4 @@ class Client @JvmOverloads constructor( } }) } -} \ No newline at end of file +} diff --git a/templates/ruby/lib/container/client.rb.twig b/templates/ruby/lib/container/client.rb.twig index 0eb2f86fb..fed997519 100644 --- a/templates/ruby/lib/container/client.rb.twig +++ b/templates/ruby/lib/container/client.rb.twig @@ -16,7 +16,7 @@ module {{ spec.title | caseUcfirst }} 'x-sdk-platform'=> '{{ sdk.platform }}', 'x-sdk-language'=> '{{ language.name | caseLower }}', 'x-sdk-version'=> '{{ sdk.version }}'{% if spec.global.defaultHeaders | length > 0 %},{% endif %} - + {% for key,header in spec.global.defaultHeaders %} '{{key}}' => '{{header}}'{% if not loop.last %},{% endif %} {% endfor %} @@ -49,7 +49,7 @@ module {{ spec.title | caseUcfirst }} # @return [self] def set_endpoint(endpoint) @endpoint = endpoint - + self end @@ -133,15 +133,19 @@ module {{ spec.title | caseUcfirst }} offset = 0 id_param_name = id_param_name.to_sym if id_param_name if id_param_name&.empty? == false && params[id_param_name] != "unique()" + begin # Make a request to check if a file already exists - current = call( - method: "GET", - path: "#{path}/#{params[id_param_name]}", - headers: headers, - params: {} - ) - chunks_uploaded = current['chunksUploaded'].to_i - offset = chunks_uploaded * @chunk_size + current = call( + method: "GET", + path: "#{path}/#{params[id_param_name]}", + headers: headers, + params: {} + ) + chunks_uploaded = current['chunksUploaded'].to_i + offset = chunks_uploaded * @chunk_size + rescue => error + raise {{spec.title | caseUcfirst}}::Exception.new(error.message) unless error.message.downcase.include?("file could not be found") + end end while offset < size @@ -202,7 +206,7 @@ module {{ spec.title | caseUcfirst }} @http = Net::HTTP.new(uri.host, uri.port) unless defined? @http @http.use_ssl = !@self_signed payload = '' - + headers = @headers.merge(headers) params.compact! @@ -237,7 +241,7 @@ module {{ spec.title | caseUcfirst }} if response_type == "location" return location end - + # Handle Redirects if (response.class == Net::HTTPRedirection || response.class == Net::HTTPMovedPermanently) uri = URI.parse(uri.scheme + "://" + uri.host + "" + location) @@ -272,7 +276,7 @@ module {{ spec.title | caseUcfirst }} return response end - + def encode_form_data(value, key=nil) case value when Hash @@ -304,13 +308,13 @@ module {{ spec.title | caseUcfirst }} when Hash then value.map { |k,v| encode(v, append_key(key,k)) }.join('&') when Array then value.map { |v| encode(v, "#{key}[]") }.join('&') when nil then '' - else - "#{key}=#{CGI.escape(value.to_s)}" + else + "#{key}=#{CGI.escape(value.to_s)}" end end def append_key(root_key, key) root_key.nil? ? key : "#{root_key}[#{key.to_s}]" end - end + end end