Skip to content

Commit

Permalink
Merge pull request #92 from /issues/85-request-throttling
Browse files Browse the repository at this point in the history
React to request throttling (answer 429)
  • Loading branch information
onthecrow authored Nov 8, 2023
2 parents bcd0926 + 033ffce commit 9dc20a4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
the API query parameters.
- Added support for converting multipage into a group of files.
- Added `default_effects` field in `UploadcareFile`.
- Added react to request throttling (429 code response).
- Widget:
- SocialApi doesn't use `GET /sources` method anymore.
- Project:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.uploadcare.android.library.data.PageData
import com.uploadcare.android.library.exceptions.UploadcareApiException
import com.uploadcare.android.library.exceptions.UploadcareAuthenticationException
import com.uploadcare.android.library.exceptions.UploadcareInvalidRequestException
import com.uploadcare.android.library.exceptions.UploadcareRequestThrottlingException
import com.uploadcare.android.library.urls.UrlParameter
import com.uploadcare.android.library.urls.UrlUtils.Companion.trustedBuild
import com.uploadcare.android.library.urls.Urls
Expand Down Expand Up @@ -603,6 +604,9 @@ class RequestHelper(private val client: UploadcareClient) {
throw UploadcareAuthenticationException("$response")
} else if (statusCode == 400 || statusCode == 404) {
throw UploadcareInvalidRequestException("$response")
} else if (statusCode == 429) {
val retryTimeout = response.header("Retry-After")?.toLong() ?: 0
throw UploadcareRequestThrottlingException("$response", retryTimeout)
} else {
throw UploadcareApiException(
"Unknown exception during an API call, response: $response")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ class UploadFailureException : UploadcareApiException {
constructor(message: String, cause: Throwable) : super(message, cause)
}

class UploadcareRequestThrottlingException(
message: String?,
val retryTimeout: Long
) : UploadcareApiException(message)

internal class UploadPausedException(message: String?): UploadcareApiException(message)

0 comments on commit 9dc20a4

Please sign in to comment.