Swaggen factory (Swagger JSON to Kotlin data class and Retrofit interface)
We, android developers, are always excited to build something cool something really meaningful which will help the business grow and ultimately help ourselves to learn new technologies, to focus on the real work we always look for the automate the tasks which are essential and unavoidable and of course redundant.
Every android developer in their life spends some time in building data classes and retrofit interfaces from request and response of API calls, which backend team provide. Nowadays many backend teams from various companies maintain really good API documentation in Swagger, which uses OpenAPI specification. To take advantage of this and help the team I created 1 ruby script which provides parse data class and retrofit interfaces.
You can find this script here https://github.com/csbhalerao/swag_gen_factory
This script will take the Swagger openapi.json file and generate the Kotlin network API code.
This script will help in generating Kotlin code for network API calls like Data requests and responses, also this script will generate the Retrofit interface which includes abstract function with retrofit annotation.
You need a ruby setup on your machine.
You can check inside a terminal emulator by typing:
This should give some basic version information about ruby if ruby is already installed.
If ruby is not installed you can refer here for installation process https://www.ruby-lang.org/en/documentation/installation/
How to use this script
- Download this script.
- Get the openapi.json from the swagger editor.
- Then just execute the following script.
ruby gen_network_code.rb openapi.json
4. After the successful execution of the script, you will get NetworkApi.kt with a request, response, and retrofit interface.
This script is created to help developers. Please provide me the feedback and let me know how I can improve the script or let me know what else we can automate. Thanks for reading this blog. Happy coding.