Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 2.46 KB

File metadata and controls

84 lines (67 loc) · 2.46 KB

Client generation steps using Open API generator

We use custom mustache templates to generate the open api client for the API specs. The templates are located under the ./templates/go folder

Step 1: Generate the the client for a Schema using open api Docker image or cli. Provide the following parameters

  • -i path to open api spec yaml
  • -t path to custom templates in ./templates/go
  • -g generator type go-experimental
  • -p packageName= <Api namespace in StartCase>
  • -o output path ./src/<Api namespace in lowercase>
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
    -i /local/schema/CheckoutService-v51.yaml \
    -t /local/templates/go \
    -g go-experimental \
    -p packageName=Checkout \
    -o /local/src/checkout

or

openapi-generator-cli generate \
    -i /schema/CheckoutService-v51.yaml
    -t /templates/go \
    -g go-experimental \
    -p packageName=Checkout \
    -o /src/checkout

Step 2: Delete the following files/folders from ./src/<api package folder name in lowercase>. If the foldername is not in lowercase, rename it

  • configuration.go
  • client.go
  • utils.go
  • response.go
  • travis.yml
  • git_push.sh
  • go.mod
  • go.sum
  • docs

Step 3: Remove the HTTP method(Post, Get, Put, Patch) suffix on API endpoint methods (Regex to find them ([A-Z][a-zA-Z0-9]*)Post\(request)

Step 4: Add the new service to APIClient struct in ./src/api/api.go and add import for the same

type APIClient struct {
	client *common.Client
	// API Services
    Checkout *checkout.Checkout
    <Api namespace in StartCase> *<Api namespace in lowercase>.<Api namespace in StartCase>
}

Init service in the NewClient method

func NewClient(cfg *common.Config) *APIClient {
    ...
    // API Services
	c.Checkout = &checkout.Checkout{
		Client: c.client,
		BasePath: func() string {
			return fmt.Sprintf("%s/%s", c.client.Cfg.CheckoutEndpoint, CheckoutAPIVersion)
		},
	}
    c.<Api namespace in StartCase> = &<Api namespace in lowercase>.<Api namespace in StartCase>{
        Client: c.client,
        BasePath: func() string {
            return fmt.Sprintf("%s/%s", c.client.Cfg.<API end point>, <API version constant>)
        },
    }
}

Step 5: Run make run or go run main.go and Fix any issues found

Step 6: Add tests for the new APIs created under ./src/<Api namespace in lowercase>

Step 7: Run make test or go test ./... and Fix any issues found