I have an existing REST API in AWS API Gateway that I plan on accessing from an Android/Kotlin mobile app. Amplify handles Auth and Storage, but I don't want Amplify to handle the REST API, not that I could anyway (try yourself at own risk!).
API Gateway will generate an Android SDK, but since I used Proxy resources with the Any method, i.e. /resource1/{proxy+}/Any()
, the generated Java code was very concise:
package com.my.clientsdk
import java.util.*
@Service(endpoint = "my-endpoint-url")
public interface MyClientSdk {
ApiResponse execute(ApiRequest request);
}
I figured, why not just implement this in Kotlin along with the request/response POKOs? I'm running into problems initializing the client via instructions from the docs:
val factory = ApiClientFactory().apiKey("some-api-key")
val sdk = factory.build(MyClientSdk::class.java)
Setting a break point after this code, I get an error for variable sdk
:
Method threw 'com.amazonaws.mobileconnectors.apigateway.ApiClientException' exception. Cannot evaluate $Proxy6.toString()
The caller of this code is a UseCase invocation launched from a ViewModel coroutine, so it's not being done on the main thread (I've seen Java implementations using AsyncTask).
How should I properly implement the API Gateway generated SDK for Android manually, in Kotlin?
Should I just use something like okhttp or ktor to send HTTP requests with the correct headers/body/etc.? Looks like I'd have to generate specific requests myself anyway. Would I have to enable CORS settings for this to work?
Yes, you can just roll your own client interface.
After some debugging of vague AWS exceptions, I learned I needed to use internet/network permissions in the app. This would have been helpful info in the platform-specific docs.
Anyway, I am now at least getting error responses from my Lambda.