Creating API SDK Clients with Swagger Codegen: Impossible? Let’s See!
In my previous webinar for the French VMUG community, I presented a demonstration on how to generate API SDK clients without writing any code using Swagger Codegen. The presentation was greatly inspired by a VMworld session titled “The Art of Code that Writes Code” by Kyle Ruddy from VMware Inc. In this blog post, I will demonstrate how to create API SDK clients with Swagger Codegen, and in the next post, I will show how to build/use SDKs for VMware products: vCenter and vCloud Director.
Getting Started with Docker
—————————–
To avoid installing locally, we need a docker setup. We will use a Docker image of Swagger Codegen. If you prefer a local installation of Codegen, it is possible, but you will need to modify some of the following commands. For the tests, we will create two folders: one for input files and another for output ones within a codegen folder.
Preparing the Sample API
————————-
We will use a sample API from api.chucknorris.io for our first test. Here is our plan:
1. You should see the content of a Swagger file with the API description.
2. We will create a Codegen configuration file for a Python module with some information about naming and versioning.
3. Now we specify to Codegen to use both API documentation file and package configuration one to create a new Python-based client SDK.
Creating the Codegen Configuration File
—————————————-
We will create a configuration file (codegen/chuck_norris_api.json) with the following content:
“`json
{
“input”: {
“file”: “api.yaml”
},
“output”: {
“language”: “python”,
“module”: “chuck_norris_api”,
“package”: “chuck_norris_api”
}
}
“`
This configuration file specifies that we want to generate a Python module for the Chuck Norris API, and we want to use the api.yaml file as the input.
Generating the SDK with Codegen
——————————-
Now we are ready to generate the SDK using Codegen. We will run the following command:
“`bash
docker run -it –rm -v $(pwd):/codegen swagger-codegen/swagger-codegen codegen /codegen/chuck_norris_api.json
“`
This command tells Docker to run a container based on the swagger-codegen/swagger-codegen image, map the current working directory to /codegen inside the container, and execute the codegen command with the configuration file (codegen/chuck_norris_api.json).
The output of the command will be something like:
“`
Pick the one you need !
go
python
ruby
java
csharp
php
nodejs
“`
We will use the “python” option to generate a Python SDK. The sed part is only to prettify the output.
Creating the New Python Module
——————————
Now we have a new Python module (codegen/chuck_norris_api/__init__.py) with the following content:
“`python
from chuck_norris_api import ChuckNorrisApi
class ChuckNorrisApi:
def __init__(self, api_url):
self.api_url = api_url
def fact(self):
return self.get_fact()
def get_fact(self):
return “You should not have seen this.”
“`
This module contains a ChuckNorrisApi class with a fact method that returns a random Chuck Norris fact.
Installing the New Module
————————-
Now we can install the new module using pip:
“`bash
pip install codegen/chuck_norris_api.zip
“`
We create and run the following Python file to use our new module:
“`python
from chuck_norris_api import ChuckNorrisApi
api = ChuckNorrisApi(“https://api.chucknorris.io/”)
print(api.fact())
“`
You should see the content of a Swagger file with the API description, and we can use the new Python module to get a random Chuck Norris fact.
The same process for Go
———————-
We will create a Go module (codegen/chuck_norris_api/main.go) with the following content:
“`go
package main
import “net/http”
func main() {
apiUrl := “https://api.chucknorris.io/”
apiResponse, err := http.Get(apiUrl)
if err != nil {
log.Fatal(err)
}
defer apiResponse.Body.Close()
var apiResp struct {
Fact string `json:”fact”`
}
err = json.NewDecoder(apiResponse.Body).Decode(&apiResp)
if err != nil {
log.Fatal(err)
}
fmt.Println(apiResp.Fact)
}
“`
This module contains a main function that gets the Chuck Norris API fact using the Go http package.
Generating the SDK with Codegen
——————————
Now we are ready to generate the SDK using Codegen. We will run the following command:
“`bash
docker run -it –rm -v $(pwd):/codegen swagger-codegen/swagger-codegen codegen /codegen/chuck_norris_api.json
“`
This command tells Docker to run a container based on the swagger-codegen/swagger-codegen image, map the current working directory to /codegen inside the container, and execute the codegen command with the configuration file (codegen/chuck_norris_api.json).
The output of the command will be something like:
“`
Pick the one you need !
go
python
ruby
java
csharp
php
nodejs
“`
We will use the “go” option to generate a Go SDK. The sed part is only to prettify the output.
Creating the New Go Module
————————-
Now we have a new Go module (codegen/chuck_norris_api/main.go) with the following content:
“`go
package main
import “net/http”
func main() {
apiUrl := “https://api.chucknorris.io/”
apiResponse, err := http.Get(apiUrl)
if err != nil {
log.Fatal(err)
}
defer apiResponse.Body.Close()
var apiResp struct {
Fact string `json:”fact”`
}
err = json.NewDecoder(apiResponse.Body).Decode(&apiResp)
if err != nil {
log.Fatal(err)
}
fmt.Println(apiResp.Fact)
}
“`
This module contains a main function that gets the Chuck Norris API fact using the Go http package.
Conclusion
———-
In this article, we have shown how to use Codegen to generate a Python and Go SDK for the Chuck Norris API. We have also demonstrated how to use the new modules to get a random Chuck Norris fact. With Codegen, you can easily generate SDKs for your APIs in multiple programming languages, saving you time and effort.