Skip to content

[FR] API generation for C++/Qt #4587

Closed as not planned
Closed as not planned

Description

@martonmiklos

Please verify that this feature request has NOT been suggested before.

  • I checked and didn't find a similar feature request

Problem statement

Hi folks,

I used to write tools in C++ with the Qt framework a lot and it would be handy if I could interact with the InvenTree sometimes.

Making REST calls with Qt is quite straightforward to implement "by hand", but I would like to spare (or more exactly make more future-proof) the generation of the exchanged data classes.

When working with Django/InvenTree migrations I started to wonder that some python code must have awareness of the InvenTree/Django objects and their fields. I could write a python script which would do the same thing to generate the C++ classes. This could be ran on each InvenTree release to regenerate the API for the new releases.

I opened this FR to start some brainstorming on this topic because I think there could be some common interests with others.

Suggested solution

Describe alternatives you've considered

Examples of other systems

API code generator exists for Qt for WSDL based webservices: https://github.com/KDAB/KDSoap

Do you want to develop this?

  • I want to develop this.

Activity

  1. added
    enhancement
    triage:not-checked
    on Apr 5, 2023
  2. SchrodingersGat commented on Apr 5, 2023

    @SchrodingersGat
    Member

    The API is not the same as the underlying model structure. But the API is self documenting (at /api-doc/).

    FYI I use the the inventree API in a Qt application using the pythonqt bindings.

    A C++ API library would be interesting but I'm certainly not going to have any time or interest to develop it.

  3. martonmiklos commented on Apr 5, 2023

    @martonmiklos
    ContributorAuthor

    But the API is self documenting (at /api-doc/).

    Mhh good point. Are you aware if this api-doc output could be tricked to get json or parsable output?

  4. SchrodingersGat commented on Apr 5, 2023

    @SchrodingersGat
    Member

    Not sure if it can be parsed as JSON. Maybe?

    Another option is that each API endpoint also is self describing. Perform an OPTIONS request against an API endpoint to see the available fields. This is how the web interface (and the app) works, it gets all the field info from the API itself.

  5. matmair commented on Apr 5, 2023

    @matmair
    Member

    #4178 might help with this - that would output an OpenApi file that can be used to generate client libraries.

  6. martonmiklos commented on Apr 5, 2023

    @martonmiklos
    ContributorAuthor

    Thanks guys for your great inputs!

    I found an useful library:
    https://github.com/qtrest/qtrest

    If I manage got gain access to the API endpoints with some parseable format I can write a code generator for this library.

  7. wolflu05 commented on Apr 6, 2023

    @wolflu05
    Member

    Aren't there already some code generators for swagger?

  8. matmair commented on Apr 6, 2023

    @matmair
    Member

    Swagger => OpenAPI. They use the open standard (I think they partook in the development - not 100% about that though)

  9. SchrodingersGat commented on Apr 12, 2023

    @SchrodingersGat
    Member

    @martonmiklos I'm going to close this one out as it's out of scope, but keep us posted if you get something working!

  10. added
    invalid
    and removed
    triage:not-checked
    on May 13, 2023
  11. martonmiklos commented on Jan 9, 2024

    @martonmiklos
    ContributorAuthor

    Some side info for the posterity, I got it rolling with the following methods:

    servers:
        - url: http://inventree-instance:8000
    

    I have not looked into it deeply yet, but I think it would be useful if the InvenTree would generate those fields into the schema.

    npx @openapitools/openapi-generator-cli generate -i "InvenTree\ API.yaml" -g cpp-qt-client -o /home/mm/Projektek/inventree2/qntree/gen_src --skip-validate-spec --additional-properties=cppNamespace=InvenTree --additional-properties=modelNamePrefix=""
    

    The generated code will not be compile-able see: OpenAPITools/openapi-generator#17487
    I will submit PR to the openapi-generator soon to fix the issue. Otherwise the methods of the classes generated for models having field called 'valid' needs to be manually renamed/prefixed.

    Usage wise it is quite comfy, you could use the API key in the following manner:

        m_apiInstance = new InvenTree::ActionApi();
    
        m_partApi = new InvenTree::PartApi();
        m_partApi->addHeaders("Authorization", "Token inv-d7e8312f7b53alofa290faa71708264247f0c21c11-20231228");
    
        connect(m_partApi, &InvenTree::PartApi::partCategoryParameters_ListSignal, this, [=](PaginatedCategoryParameterTemplateList summary) {
            qWarning() << summary.asJson();
        });
    
  12. SchrodingersGat commented on Jan 10, 2024

    @SchrodingersGat
    Member

    @martonmiklos very interesting, this might be worth adding to the documentation for others to benefit from. If there are extra information fields that we need to add to the existing schema, please raise a separate issue for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementinvalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions