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 Endpoint assembly broker.
It’s really easy to setup and start using REST Services.
K2 Endpoint 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/#/ – Online Swagger Editor , if the Rest Service is accessible to the outside world
- https://github.com/swagger-api/swagger.io/blob/wordpress/tools/swagger-editor.md – You can download a copy of the editor to install on a server where the REST service is accessible
Creating a Swagger definition file
To create a Swagger definition file using the swagger editor (same for both online and local version) follow the following steps
- Click on ‘File’
- Click on ‘Import URL’
- Enter in 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 the C:\ called ‘Swagger’
- Copy file into the folder
- Open up the SmartObject Tester Tool
- Expand ‘ServiceObject Explorer’
- Right click on ‘Endpoints Rest’ and click on ‘Register Service Instance’
- In the section ‘Descriptor Location’ enter in the full path of the Swagger file we copied into the Swagger folder
- Click on ‘Next’ and then click on ‘Add’
- K2 will now go through the Swagger file and create service objects based on the definitions in the Swagger file
- 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 of the REST Broker. It will do this in two ways.
- There will be a Web API Handler that accepts Posts, the idea being that when the global registry changes it will post to the service with the new host address and application name. The service then goes into the library and finds the correct swagger definition and updates it with the new host address. It then exports the swagger definition file to the swagger folder on the server and then updates K2 with the new definition. ToDo
- In the portal you can view all the swagger definitions, where new ones can be added and old ones can be edited. Once created in the library, this will then create Swagger JSON file on the K2 Server.
Add a new Swagger file
- Click on ‘Add Swagger Definition’
- Form loads up where you can enter in the definition name, host address and the definition it self
- Click on ‘Submit’
- This will save the swagger definition in the library and also create its corresponding JSON file on to the server.
- You will still need to create the instance manually using the service object tester tool