Alexjok
04/03/2019, 11:34 AMinline fun <reified T> execute(): T {
return doRequest()
}
private inline fun <reified T> doRequest(): T
{
//doSomething
}
Get the error "Public API inline function cannot access non public API"
How to solve it?diesieben07
04/03/2019, 11:37 AMexecute
that call is replaced by doRequest
(since that's what execute
does). For that to work doRequest
must be accessible to whoever might call execute
, which in this case is everyone.
You can make doRequest
internal
and annotate it with @PublishedApi
- note however that you must then treat doRequest
like a public method, you can't change it without breaking your API.diesieben07
04/03/2019, 11:37 AMreified
for doRequest
, but instead pass T::class
from execute
.Jonathan Mew
04/03/2019, 11:44 AMlouiscad
04/03/2019, 11:51 AM@PublishedApi internal
instead of private
.diesieben07
04/03/2019, 11:51 AMinline
is required here, since you are using reified
. But if you just need the KClass<T>
(or Class<T>
, you can make doRequest
not inline and take a Class<T>
parameter instead.diesieben07
04/03/2019, 11:52 AMdiesieben07
04/03/2019, 11:52 AMdiesieben07
04/03/2019, 11:52 AMAlexjok
04/03/2019, 11:52 AMinline fun <reified T> execute(): T {
return doRequest()
}
private inline fun <reified T> doRequest(): T
{
fuelManager(token) //companion object for authorization
val request = when(httpType)
{
<http://HttpType.POST|HttpType.POST> -> <http://Fuel.post|Fuel.post>(url, httpParamas).request
HttpType.GET -> Fuel.get(url, httpParamas).request
}
val (response, error) = request.timeoutRead(TIMEOUT).responseString().third
return extractAuthResult(response, error, url)
}
private inline fun <reified T> extractResult(response: String?, error: FuelError?, url: String): T
{
if(error != null || response.isNullOrEmpty() || response.contains("error") ){
throw HTTPException("url=$url\r\nresponse = $response")
}
return try { Gson().fromJson(response) }
catch(e: JsonParseException)
{
throw HTTPException("Gson Error\r\nurl=$url\r\nreason = $e")
}
}
private inline fun <reified T> Gson.fromJson(json: String) = this.fromJson<T>(json, object: TypeToken<T>() {}.type)
Alexjok
04/03/2019, 11:53 AMlouiscad
04/03/2019, 11:54 AMdiesieben07
04/03/2019, 11:55 AMTypeToken
in the public facing function and then passing it to the others.Alexjok
04/03/2019, 12:03 PM