Open alisters opened 3 years ago
@alisters thanks for offering contributions on this. We definitely welcome Volley support via the --library
option in the Kotlin client generator.
Let us know if you need any help.
@wing328 - great. This is progressing, but as always more slowly than hoped ! We are using it in house, and are adding some auth to it. Next update will hopefully be a PR and any/all help and feedback welcomed then.
@wing328
Design decisions for the Android Kotlin library
A kotlin client for Android using the currently recommended http client, Volley. See https://developer.android.com/training/volley
Currently sends GsonRequests - volley requests where the json payload is de/serialized using gson
Currently only supports Gson as a serializer
Defaults the source location to src/main/java as per standard Android builds
Reuse model class and other jvm common infrastructure from the kotlin client
Api calls use co-routines, the suspendCoroutine builder function and execute them using volley callbacks to avoid tying up a thread.
An api "client" stub is generated by tag for a set of operations as per the base generator
A RequestFactory is injected into each client stub - facilitating extension if required, with a default implementation provided.
The request factory can be passed per tag (api client) or per operation (an extra parameter is created on operations with non-global security), when per operation auth overriding global security.
Since the request factory is injected, appropriate DI container based scoping of the Request Factory and pre-generated auth header factories allow for thread safe and secure setting of credentials.
The request factory is passed header factories that optionally late bind or lazily provide header values allow for refreshing tokens etc
Factoring of header factories to the Request Factory allow ambient provision of credentials. Code gen library is credential storage agnostic.
Header factories allow the merging of generated headers from open api spec with dynamically added headers
Injection of http url stack to allow custom http stacks
Optional generation of room database models, and transform methods to these from open api models
In our own code, the generated room and api models are extended with additional extension properties - while code gen consumers get their clients generated for free the resulting code is still extensible using kotlin. I'll provide some sample code.
Additional supporting files ??
Hilt Dependency injection example - when constructing our clients, we inject the appropriate header factories. We don't use auth from the api, but in this sample i also inject a basic auth header factory and bind it's credentials
return SomeApi (context = context, requestQueue = restService.getRequestQueue(), requestFactory = RequestFactory(sessionHeaderFactory, traceHeaderFactory, RequestFactory.basicAuthHeaderFactoryBuilder("username", "password")), basePath = configurationService.getBaseUrl() )
I've attempted to add per operation security but can't see an easy way to do it presently.
Have seen that it was done for the python client here - https://github.com/OpenAPITools/openapi-generator/pull/6569/files - but am wondering if this is commonly implemented ?
@wing328 We've updated the PR after generating the required samples and docs. Could you indicate
I was unable to tag the issue as Kotlin client related when i raised it. I'm happy to be tagged on bugs raised, and to monitor the appropriate tags if you can tell me what they will be.
@wing328 Some help is required please. I'm unsure why the circle:ci and bitrise builds are failing.
Dry Run Results:
k C:\dev...-kotlin-volley.openapi-generator-ignore Skipped by user option(s) Skipped by supportingFiles options supplied by user. w C:\dev...-integration-kotlin-volley.openapi-generator\VERSION Write............... File will be written.
States:
Circle:ci circle_parallel seems to be failing on the typescript client ?
Please ignore those CI failure for the time being. I'll take a look at your PR later today.
Ok and thank you.
If you're not familiar with volley, the major aspect i didn't really call out in the above comments is that volley doesn't directly communicate with an http client - it adds requests to a queue which then interacts with an http client. So in our code gen setup we replaced an apiclient or api invoker that the other kotlin and java clients had with just the RequestFactory. In turn this changes the way you might interact with the code gen in the course of making a given request - how do you set the username and password for a single operation that has overridden global security ? We could put these calls on the request factory but it didn't feel great. TBD further if you wish to.
Is your feature request related to a problem? Please describe.
We use android and volley, but with Kotlin. Presently there is
Searching the issues shows the volley client may need attention - https://github.com/OpenAPITools/openapi-generator/issues/9293
Additionally we use co-routines, but my understanding (limited!) of the implementation of co-routines in both these libraries is that whilst they add a suspending function, they then block on it's return. I'd like to see suspendCoroutine, adding the callback listeners to the co-routine continuation, and suspending the co-routine until a value has returned (avoiding tying up a thread until the response arrives). This is the documented way of adapting existing callback based api's to co-routines - though the best explanation of it i've found is in this video, at this point - https://youtu.be/YrrUCSi72E8?t=982
Describe the solution you'd like
I'd like to leverage the model generation in the kotlin client generator, but add a library to for volley
Describe alternatives you've considered
Using one of the existing two generators / libraries. See some of the shortcomings outlined above
Additional context
We've started work on it, and should have a PR ready with basic functionality ready in a week or two. It has some obvious extension points with the ability to pass your own configured requestQueue, or your own factory for mapping from the various http call fields to volley request classes.