Search code examples
c++swaggercasablancacpprest-sdk

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?


Solution

  • 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?