kroegerama / openapi-kgen Goto Github PK
View Code? Open in Web Editor NEWGenerate modern API Clients in Kotlin from OpenAPI specifications. Supports OpenAPI >= 3.0.0.
License: Apache License 2.0
Generate modern API Clients in Kotlin from OpenAPI specifications. Supports OpenAPI >= 3.0.0.
License: Apache License 2.0
I'd like to improve the code-gen, but I don't know how to start.
Can you please provide your dev setup?
It would be great if in addition to / instead of Moshi kotlinx-serialization would be supported as a (de-)serializer as that would be a step towards multiplatform support.
I'm proposing a new API for users to simplify error management. The current generated API lets the user handle all the errors by himself, like as following (written on the fly, might not be 100 % correct):
var response: Response<MyObject> = null
var errorString: String = null
try {
response = DefaultApi.Companion::getSomeResources()
} catch (e: Exception) {
errorString = e.localizedMessage
}
if (response?.isSuccessful) {
showData(response?.body())
} else {
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter<ErrorObject>(type)
val errorString = response?.errorBody()?.string()
val errorObject = if (errorString != null) {
jsonAdapter.fromJson(errorString)
} else {
null
}
if (errorObject != null) {
errorString = errorObject.message
}
else {
errorString = getString(R.string.error_unknown)
}
showError(errorString)
}
That makes a lot of error management to repeat each time.
I'm proposing to add another layer of generated API that would be equivalent to this (this code works):
import com.squareup.moshi.Moshi
import retrofit2.Response
import java.lang.Exception
fun <T, TError> Response<T>.errorObject(type: Class<TError>): TError? {
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter<TError>(type)
val errorString = errorBody()?.string()
return if (errorString != null) {
jsonAdapter.fromJson(errorString)
} else {
null
}
}
fun <T, TError> Response<T>.handle(type: Class<TError>): Triple<Response<T>, T?, TError?> {
return if (isSuccessful) {
Triple<Response<T>, T?, TError?>(this, body(), null)
} else {
Triple<Response<T>, T?, TError?>(this, null, errorObject(type))
}
}
suspend fun <TResponse, TError> exec(type: Class<TError>, f: suspend () -> Response<TResponse>): Triple<Response<TResponse>?, TResponse?, Pair<TError?, Exception?>?> {
return try {
val (response, result, error) = f.invoke().handle(type)
Triple(response, result, if (error == null) null else Pair(error, null))
} catch (e: Exception) {
Triple(null, null, Pair(null, e))
}
}
suspend fun <TResponse, TError> (suspend () -> Response<TResponse>).execute(type: Class<TError>): Triple<Response<TResponse>?, TResponse?, Pair<TError?, Exception?>?> {
return exec(type, this)
}
suspend fun <TResponse, TError, T1> (suspend (T1) -> Response<TResponse>).execute(type: Class<TError>, arg1: T1): Triple<Response<TResponse>?, TResponse?, Pair<TError?, Exception?>?> {
return exec(type) {
invoke(arg1)
}
}
And the usage is as follow:
val (response, result, error) = DefaultApi.Companion::getSomeResources.execute(ErrorHolder::class.java, null)
if (error != null) {
val errorMessage = error.first?.error?.message ?: error.second?.localizedMessage ?: getString(R.string.error_unknown)
Log.e("MainActivity", "FAILED: $errorMessage")
} else {
Log.d("MainActivity", "SUCCESS: " + (result ?: "null"))
}
A few drawback of this externally implemented way:
DefaultApi.Companion::getSomeResources.execute(...)
vs DefaultApi.getSomeResources(...)
Any thought ?
PS: Kudos for this project, it's really awesome!
Hi, this looks like the lib I'm needing, but I'm not getting how to use it, this should be usable with Retrofit, right? is there a sample somewhere where the generated code is being used with retrofit?
Is oAuth going to be supported any time soon? This is the only OpenAPI generator that supports all Kotlin features and practises. It needs more attention from the community and is a great start. Well done!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.