-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix chunk uploads #993
base: master
Are you sure you want to change the base?
Fix chunk uploads #993
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ class Client @JvmOverloads constructor( | |
internal lateinit var http: OkHttpClient | ||
|
||
internal val headers: MutableMap<String, String> | ||
|
||
val config: MutableMap<String, String> | ||
|
||
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 <T> call( | ||
method: String, | ||
path: String, | ||
headers: Map<String, String> = mapOf(), | ||
method: String, | ||
path: String, | ||
headers: Map<String, String> = mapOf(), | ||
params: Map<String, Any?> = mapOf(), | ||
responseType: Class<T>, | ||
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")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with Steven, this seems fragile... |
||
throw e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't |
||
} | ||
} | ||
} | ||
|
||
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<Map<String, Any>>() | ||
|
||
{{ 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( | |
} | ||
}) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated, but can we fix the spacing here? Looks weird |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
Long
type the best choice?