Giter Site home page Giter Site logo

openapi-kgen's People

Contributors

arjixwastaken avatar kroegerama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

openapi-kgen's Issues

Better generated names.

My API methods are already fluently named, so the generated names suck.

Original API
image

Generated client:
AuthApi.postApiAuthLogin, AuthApi.postApiAuthLogout, AuthApi.postApiAuthRefresh

would be nicer is they were just named AuthApi.Login, AuthApi.Logout, AuthApi.Refresh

How to contribute

I'd like to improve the code-gen, but I don't know how to start.
Can you please provide your dev setup?

Proposal for a new user facing API

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:

  • The call line is much longer than what it could be: DefaultApi.Companion::getSomeResources.execute(...) vs DefaultApi.getSomeResources(...)
  • Optional arguments are not supported

Any thought ?

PS: Kudos for this project, it's really awesome!

Please elaborate usage

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?

oAuth support

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.