Swagger Console

Creating Client generated API SDKs using Swagger Editor

AyushX
Member
Content

API documentations using the Swagger tool has always been a great choice for development purposes. Here in this article we explore how we can use the tool to generate client SDKs for APIs in various programming languages, so that we can access the APIs directly from our application without using HTTP requests.

Requirements
  1. Swagger editor installed or access the online Swagger Editor.
  2. ctrlX WORKS installed.
Steps
  1. Open ctrlX WORKS and start a virtual device.
  2. After successfully launching a ctrlX COREvirtual, open the virtual controls web UI in a web browser.
  3. Click on the help icon (?) >> API reference.ctrlX WORKS HomectrlX WORKS Home
  4. Select any API reference whose client SDK is to be generated.
  5. Click on the API reference link as shown in the image below, to get access to the API documentation URL. ctrlX API DocumentationctrlX API Documentation
  6. Copy the URL from the new tab.
  7. Open the Swagger editor and click on File >> Import URL, and paste the copied URL.
  8. After successfully loading the API documentation in Swagger, click on Generate Client.Swagger EditorSwagger Editor
  9. Select the preferred language for which the SDK is to be generated. The generated zip file will be downloaded and stored in the download folder, ready to be imported in your application.

Once the library is generated, it can be imported into the project as required.

Note: It is also possible to access the openapi.js (file containing the json of all API data) from the inspection window of your browser. Inspect>> Sources>> openapi.js
This might be helpful in cases where the link to the openapi json (API reference URL) is not directly available.

Swagger EditorSwagger Editor

Examples

Following examples will help you understand the usage procedure for the libraries.

Requirements

  • Visual Studio Code
  • Running server of ctrlX WORKS
Setup in Angular
  1. On creating your Angular project, import the openApi generated library into your project. The import folder might vary based upon the project architecture. We here use a lib folder to store the libraries to store and import our self generated libraries.

    Sample Angular ProjectSample Angular Project
  2. On addition of library into the project, we add the path to the library in to our App.module.ts file as shown in the image below. After importing the File, we add the import variable ApiModule into imports array variable in @MyModule.

    Angular app.module.tsAngular app.module.ts
  3. After addition of the library path, we will start working with the library as per our needs. To show an example, we here import BatchService into our component. To learn about the service usage, we refer to the service description in the OpenApi file of the generated library folder.

    Angular Import API libraryAngular Import API libraryctrlX BatchService API DescriptionctrlX BatchService API Description
Setup in Python

For the demonstration, we are using google colab.

  1. Upload the library file in the google drive and mount google drive in the notebook as shown in the image below.
    Google collab notebook demonstrationGoogle collab notebook demonstration
    import swagger_client
    python setup.py install --user
    from __future__ import print_function
    import time
    import swagger_client
    from swagger_client.rest import ApiException
    from pprint import pprint
    configuration = swagger_client.Configuration()
    configuration.access_token = 'YOUR_ACCESS_TOKEN'
  2. We are importing the Swagger client into the notebook which we use to access the API classes and their functionality.
    Swagger API library usage demonstrationSwagger API library usage demonstration
  3. Here now we simply use the required class and its function using the '.' operator.Sample Python Scripts to runSample Python Scripts to run

As we can see, generating OpenApi libraries help in integration of the ctrlX WORKS Apis considerably, without the need to make individual HTTP requests to the server.

Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist