question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AaU, I want to configure the timeouts used by the AWSApiPlugin.

See original GitHub issue

I randomly get the following error when I send a request. (Regardless of GET, POST) com.amplifyframework.api.ApiException: Received an IO exception while making the request.

Here’s my code:

fun <K, T> sendRequest(
        request: K? = null, api: APIs,
        queryMap: MutableMap<String, String>? = mutableMapOf(),
        pathParameters: MutableMap<String, String>? = mutableMapOf(),
        onSuccessAction: ((BaseResponse<T>) -> Unit)? = null
    ) {
        showIndicator()
        //Dynamic path parameters control
        var editedEndpoint = api.endpoint
        pathParameters?.forEach {
            editedEndpoint = editedEndpoint.replace("{${it.key}}", it.value)
        }

        when (api.method) {
            RequestMethod.POST -> {
                val json = gson.toJson(request)
                val bytes = json.toByteArray(StandardCharsets.UTF_8)
                
                val options = RestOptions.builder()
                    .addPath(editedEndpoint)
                    .addBody(bytes)
                    .addHeaders(
                        mutableMapOf(
                            "Content-Type" to "application/json",
                            "Accept" to "application/json"
                        )
                    )
                    .addQueryParameters(queryMap ?: mapOf())
                    .build()
                Amplify.API.post(options,
                    { response ->
                        when {
                            response.code.isSuccessful -> {

                                val hasData = response.data.asJSONObject().isNull("data").not()
                                if (hasData && api.responseType != Empty::class.java) {
                                    val typedData = gson.fromJson(
                                        response.data.asJSONObject()["data"].toString(),
                                        api.responseType
                                    )
                                    onSuccessAction?.invoke(BaseResponse(typedData) as BaseResponse<T>)
                                    hideIndicator()
                                } else {
                                    onSuccessAction?.invoke(BaseResponse(null))
                                    hideIndicator()
                                }
                            }
                            response.code.isClientError -> {
                                
                            }
                            response.code.isServiceFailure -> {
                                
                            }
                        }
                    },
                    {
                        //API Error
                    })
            }

            RequestMethod.GET -> {
                val options = RestOptions.builder()
                    .addPath(editedEndpoint)
                    .addHeaders(
                        mutableMapOf(
                            "Content-Type" to "application/x-www-form-urlencoded; charset=utf-8",
                            "Accept" to "application/json"
                        )
                    )
                    .addQueryParameters(queryMap ?: mapOf())
                    .build()
               
                Amplify.API.get(options,
                    { response ->
                        when {
                            response.code.isSuccessful -> {
                                val hasData = response.data.asJSONObject().isNull("data").not()
                                if (hasData && api.responseType != Empty::class.java) {
                                    val typedData = gson.fromJson(
                                        response.data.asJSONObject().optString("data"),
                                        api.responseType
                                    )
                                    onSuccessAction?.invoke(BaseResponse(typedData) as BaseResponse<T>)
                                    hideIndicator()
                                } else {
                                    onSuccessAction?.invoke(BaseResponse(null))
                                    hideIndicator()
                                }
                            }
                            response.code.isClientError -> {
                               
                            }
                            response.code.isServiceFailure -> {
                                
                            }
                        }
                    },
                    {
                        //API Error
                    })
            }
        }
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jamesonwilliamscommented, Oct 31, 2020

I agree this would be a good feature to include. Configurable timeouts is sort of a must-have for an API client. We will track enthusiasm according to user reactions on this ticket and will try to get it scheduled sooner or later. @aeraydogan Feel free to raise a PR! All in all we’d need:

  1. Updates to the configuration objects and the amplifyconfiguration.json reader
  2. The changes to the plugin that you propose
  3. Updates on the docs site that talk about how to use it.

If the timeouts are not supplied in the ampliflyconfiguration.json, the current default values should be the same.

Potentially, it might make sense to provide per-request timeout values in the options structure provided to Amplify.API.whatever(...), too.

0reactions
jamesonwilliamscommented, Mar 5, 2021

Resolved in 1.17.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customize Integration Timeouts in Amazon API Gateway
You can now customize the timeout value for API integrations in Amazon API Gateway. You can set the maximum amount of time an...
Read more >
Configuring I/O timeout on Message Processors | Apigee Edge
This document explains how to configure the I/O timeout for the Apigee ... This property is used for all the API Proxies running...
Read more >
Setting a Request Timeout for a Spring REST API
If we want to place a timeout on our database requests, we might want to use Spring's @Transactional method and its timeout property....
Read more >
Always use a timeout for http requests
Many third party services we use in our apps provide APIs to be ... If we don't have a timeout set, our entire...
Read more >
Retries and Timeouts | AWS SDK for Go V2
The AWS SDK for Go V2 enables you to configure the retry behavior of requests to HTTP services. By default, service clients use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found