Swagger-codegen: [cpprest] Example of using the cpprest client library code?

Created on 17 Apr 2018  路  3Comments  路  Source: swagger-api/swagger-codegen

Description

I've recently started using 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 at tree/master/samples/client/petstore/cpprest but is there a test harness for it anywhere? Am I missing something obvious? (Be kind, I'm new to swagger/cpprest)

Swagger-codegen version

2.3.1

Most helpful comment

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?

All 3 comments

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?

Did you manage to link the project in linux? I am trying to do the same, but I am getting undefined references.

Compiled and executed successfully.

#include "api/api/PetApi.h"
using namespace io::swagger::client::api;

int main()
{
    std::shared_ptr<ApiClient> apiClient(new ApiClient);
    std::shared_ptr<ApiConfiguration> apiConfig(new ApiConfiguration);
    apiConfig->setBaseUrl("http://petstore.swagger.io/v2");
    apiClient->setConfiguration(apiConfig);
    PetApi api(apiClient);
    api.getPetById(1).then([=](pplx::task<std::shared_ptr<Pet>> example) {
        try {
            std::cout << example.get()->getName() << '\n';
        } catch(const std::exception& e) {
            std::cout << "getExample() exception: " << e.what() << '\n';
        }
    }).wait();
}
$ ./bin/a.out
id_1
Was this page helpful?
0 / 5 - 0 ratings