Download samples and resource required for this project from this location.
https://drive.google.com/file/d/0B3OmQJfm2Ft8ZVJXZi02S19KQ3M/view?usp=sharing
Goto ballerina distribution
/ballerina-0.8.0-SNAPSHOT/bin
Then it will generate connector as well.
Then run following command. For this we need to pass swagger input file to generate swagger.
Sample command
Example commands for connector, skeleton and mock service generation is as follows in order.
ballerina swagger connector /tmp/testSwagger.json -d /tmp -p wso2.carbon.test
ballerina swagger skeleton /tmp/testSwagger.json -d /tmp -p wso2.carbon.test
ballerina swagger mock /tmp/testSwagger.json -d /tmp -p wso2.carbon.test
Command:
>>./ballerina swagger connector /home/sanjeewa/Desktop/student.yaml -p org.wso2 -d ./test
Please use following sample swagger definition for this.
swagger: '2.0'
info:
version: '1.0.0'
title: Swagger School (Simple)
description: A sample API that uses a school as an example to demonstrate features in the swagger-2.0 specification
termsOfService: http://helloreverb.com/terms/
contact:
name: Swagger API team
email: foo@example.com
url: http://swagger.io
license:
name: MIT
url: http://opensource.org/licenses/MIT
host: schol.swagger.io
basePath: /api
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/students:
get:
description: Returns all students from the system that the user has access to
operationId: findstudents
produces:
- application/json
- application/xml
- text/xml
- text/html
parameters:
- name: limit
in: query
description: maximum number of results to return
required: false
type: integer
format: int32
responses:
'200':
description: student response
schema:
type: array
items:
$ref: '#/definitions/student'
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
post:
description: Creates a new student in the school. Duplicates are allowed
operationId: addstudent
produces:
- application/json
parameters:
- name: student
in: body
description: student to add to the school
required: true
schema:
$ref: '#/definitions/newstudent'
responses:
'200':
description: student response
schema:
$ref: '#/definitions/student'
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
/students/{id}}:
get:
description: Returns a user based on a single ID, if the user does not have access to the student
operationId: findstudentById
produces:
- application/json
- application/xml
- text/xml
- text/html
parameters:
- name: id
in: path
description: ID of student to fetch
required: true
type: integer
format: int64
- name: ids
in: query
description: ID of student to fetch
required: false
type: integer
format: int64
responses:
'200':
description: student response
schema:
$ref: '#/definitions/student'
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
delete:
description: deletes a single student based on the ID supplied
operationId: deletestudent
parameters:
- name: id
in: path
description: ID of student to delete
required: true
type: integer
format: int64
responses:
'204':
description: student deleted
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
definitions:
student:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
newstudent:
type: object
required:
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
errorModel:
type: object
required:
- code
- textMessage
properties:
code:
type: integer
format: int32
textMessage:
type: string
Then you will see relevant files in output directory.
├── test
└── org
└── wso2
├── default.bal
├── LICENSE
├── README.md
└── types.json
Now copy this connector content to ballerina editor and load it as connector. Please see below image.
import ballerina.lang.messages;
import ballerina.lang.system;
import ballerina.net.http;
import ballerina.lang.jsonutils;
import ballerina.lang.exceptions;
import ballerina.lang.arrays;
connector Default(string text) {
action Addstudent(string msg, string auth)(message ) {
http:ClientConnector rmEP = create http:ClientConnector("http://127.0.0.1:8080");
message request = {};
message requestH;
message response;
requestH = authHeader(request, auth);
response = http:ClientConnector.post(rmEP, "/students", requestH);
return response;
}
action Findstudents(string msg, string auth)(message ) {
<span style='font-family: "consolas"; font-size: 11pt; vertical-align: basel