What is this
A centralized place for the options available to generate a client that can be used to communicate with an HTTP / REST style API.
Problem definition
- You need to call an HTTP/REST API from C#, TypeScript or some other programming platform
- This boils down to serializing Request and Response to and from JSON, invoking the API using some sort of HTTP Client
- Hand-crafting the code to invoke the API is tedious and a good fit for code generation
The Tools
When the API to call exposes an OpenAPI Spec (Swagger Doc is now OpenAPI) URL or YAML/JSON File
Option 1: Use SwaggerHub
- Browse to https://swagger.io/tools/swagger-codegen/
- Sign up (using a provider such as Github or create an account using email)
- Go to SwaggerHUB
- Paste the Spec in the text box
- Click on the Export button (top right corner) and select the platform for which client/mock needs to be generated
Option 2: Use NSwag Studio
- Get NSwag Studio from Github
- In the input tab, paste the OpenAPI Spec URL or paste the spec in the text box
- Configure parameters for generation - e.g. Scope and lifetime of HttpClient, Collection types to use etc.
- To generate code into a file, configure a file path
- Click generate!
Step-by-step tutorial by Nuno Caneco here