Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use swagger-codegen cpprest client library code?

I've recently used swagger-codegen to generate the cpprest client code for my swagger spec. The code all compiles and links swell in my C++ app.

But, how do I actually use it from my C++ application? I've seem to have initialized the ApiClient and ApiConfiguration. But it's not clear to me how to incorporate the getXXX() call on my API object (ex: DefaultApi).

I've done a rather extensive internet search for source code demonstrating using the generated client code, but to no avail. I've also noted there is the swagger-codegen sample petstore client library for cpprest here: (https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/cpprest), but is there a test harness for it anywhere?

like image 372
Jay Koutavas Avatar asked Apr 17 '18 19:04

Jay Koutavas


People also ask

How do I generate a Python client from Swagger?

In the Swagger Editor toolbar, select Generate Client > Python to download a client written in Python. Our example code is written in Python. Swagger Editor supports over 50 other languages that you can use for the client interface you develop.

How do I run swagger codegen on Windows?

Download the Swagger Codegen JAR File Download the latest version of the Swagger Codegen JAR 2.3. 0+. Windows users can use Invoke-WebRequest in PowerShell 3.0+ (e.g., Invoke-WebRequest -OutFile swagger-codegen-cli. jar http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar).

How do I get swagger codegen?

Swagger Codegen is available for download in the GitHub repository, or can be generated for any new or existing OpenAPI-defined API in the integrated SwaggerHub platform.


1 Answers

Well, I worked out the basics for this, a trivial example:

std::shared_ptr<ApiClient> apiClient(new ApiClient);
std::shared_ptr<ApiConfiguration> apiConfig(new ApiConfiguration);
apiConfig->setBaseUrl("http://example.com/api/v1");
apiClient->setConfiguration(apiConfig);
ExampleApi api(apiClient);
api.getExample().then([=](pplx::task<std::shared_ptr<Example>> example) {
  try {
      std::cout << example.get()->getDescription() << '\n';
  } catch(const std::exception& e) {
      std::cout << "getExample() exception: " << e.what() << '\n';
  }
});

I'd still like to learn how the petstore cpprest generated code is tested. Where's the harness? Is there one?

like image 98
Jay Koutavas Avatar answered Sep 29 '22 23:09

Jay Koutavas