Hooray, it’s finally here, after months and months of beta testing. In 4.7 we finally get to use REST services without having to write a load of code to wrap around the REST service and then access it via the Endpoints Assembly broker.

It’s really easy to set up and start using REST services.

The K2 Endpoints REST broker uses Swagger to define the service objects and methods. Swagger is becoming the open standard for describing REST APIs.

Getting access to a Swagger editor

To get a Swagger definition of a REST service, you need access to a Swagger editor that can see the REST service:

  • http://editor.swagger.io/ is the online Swagger editor, if the REST service is accessible to the outside world
  • You can also download a copy of the editor (https://github.com/swagger-api/swagger.io/blob/wordpress/tools/swagger-editor.md) and install it on a server where the REST service is accessible

Creating a Swagger definition file

To create a Swagger definition file using the Swagger editor (it’s the same for both the online and local versions), follow these steps:

  • Click on ‘File’
  • Click on ‘Import URL’
  • Enter the URL
  • If it can’t find the URL, uncheck ‘Use CORS proxy’
  • Once it has found the REST service, click on ‘Import’
  • If there are no errors, click on ‘File’
  • Click on ‘Download JSON’
  • Take the .json file and copy it to the K2 server
  • Create a folder on C:\ called ‘Swagger’
  • Copy the file into the folder
  • Open up the SmartObject Service Tester
  • Expand ‘ServiceObject Explorer’
  • Right-click on ‘Endpoints REST’ and click on ‘Register Service Instance’
  • In ‘Descriptor Location’, enter the full path of the Swagger file we copied into the Swagger folder
  • Click on ‘Next’ and then on ‘Add’
  • K2 will now go through the Swagger file and create service objects based on its definitions
  • You can now create SmartObjects as normal

Using K2 Framework (K2F) with Swagger and the REST broker

K2F provides support for managing the Swagger definitions used by the REST broker. It does this in two ways:

  • A Web API handler that accepts posts (still on my to-do list). The idea is that when the global registry changes, it posts to the service with the new host address and application name. The service then finds the correct Swagger definition in the library and updates it with the new host address. It then exports the definition file to the Swagger folder on the server and updates K2 with the new definition.
  • In the portal you can view all the Swagger definitions, add new ones and edit old ones. Once a definition is created in the library, the Swagger JSON file is created on the K2 server.

Adding a new Swagger file

  • Click on ‘Add Swagger Definition’
  • A form loads up where you can enter the definition name, the host address and the definition itself
  • Click on ‘Submit’
  • This saves the Swagger definition in the library and also creates its corresponding JSON file on the server
  • You still need to create the service instance manually using the SmartObject Service Tester