Getting started with generated code

905 views
Skip to first unread message

Paul Masri-Stone

unread,
Feb 6, 2017, 3:44:43 AM2/6/17
to Swagger
Hi, bit of a noob question.

I've defined an API with Swagger - awesome tool - and I'm using the code generation tool to generate Qt C++ clientside code. Is there a guide to getting started with it or some examples out there using it? I've looked through all the generated classes, but I don't get how to use them. Rather than spend a day or two scratching my head, are there resources I can look at?

Thanks

tony tam

unread,
Feb 6, 2017, 2:47:28 PM2/6/17
to swagger-sw...@googlegroups.com
Hi Paul there isn’t much of a getting-started guide for QT but there should be one.  Maybe you can help with a PR to the repository?

Getting started should be simple, but here’s a quick tutorial to help out.

* Upon generation, you will have a set of files created—the ones ending in “Api” are the classes used to call the server.  Every method in there corresponds, through “operationId” in your spec and includes the parameters for calling it
* A Model class is created for each schema definition in your swagger file.  These are input/outputs for the calls to the api classes
* Each API call executes a function which calls back to a slot in your application.  To receive the callback, you’ll need to connect your listener to the appropriate callback.

For example this sample client:


To get a “pet by ID” you would
* include SWGPetApi.h in your class making the call
* instantiate the SWGPetApi class like such:

SWGPetApi * api = new SWGPetApi();

* Connect the SWGPetApi instance to your app like such:

this->connect(api, getPetByIdSignal(SWGPet*), this, getPetCallback(SWGPet* pet)

* Call the getPetById(qint64 pet_id); method
* Your function “getPetByIdCallback(SWGPet* pet)” will be called by the api class

There are some other convenience features, such as serializing the models to JSON and back, for example you can write a model to JSON like this:

SWGPet pet;
pet.setId(300);

qDebug() << pet.asJson();

and also instantiate objects from json:

QString json("{\"id\":3}");

// populate the object from JSON
pet.fromJson(json);
etc.

Tony

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Masri-Stone

unread,
Feb 7, 2017, 11:04:08 AM2/7/17
to Swagger
Thanks Tony. That's awesome. Hopefully your notes will be enough to get me up and running. I'll let you know.

When you say "there isn’t much of a getting-started guide for QT but there should be one", does that mean there are getting-started guides for other languages/frameworks (clientside / serverside)? If so, where can I find them?

Best,
Paul.

Paul Masri-Stone

unread,
Feb 26, 2017, 9:09:08 AM2/26/17
to Swagger
Hi Tony,

I just wanted to say thanks again. I'm up and running and your message really helped.

For others, I also hadn't realised that there is a PetStore sample for Qt/C++, though this is more of a unit test app than a usable example. It's also worth saying that the generated code isn't bug-free but it's a great way to get started.

I plan to write my own classes now that I understand the structure better, including an upgraded version of SWGHttpRequest that includes HTTP status response codes and possibly with progressive loading signals. I'm not sure I'll be able to convert this to mustache-style code for a pull request but I'd be happy to raise a Git issue and provide it to help you and the Swagger team upgrade the code generators, if that's of interest.

Cheers,
Paul

wing328

unread,
Mar 1, 2017, 5:58:50 AM3/1/17
to Swagger
Hi Paul,

For other langauges (C#, Java, Ruby, Python, PHP, etc), the auto-generated code also comes with documentation (installation instruction, code samples, etc). Here are a few examples:


If you've time, I can work with you to create auto-generated documentation for QT5 C++ client.

Best regards,
William


Paul Masri-Stone於 2017年2月26日星期日 UTC+8下午10時09分08秒寫道:
Reply all
Reply to author
Forward
0 new messages