Expose your data as a REST API

Setting up the background

  • Create a database in MySQL called “hospital_data”.
create database hospital_data;
  • Create a table in that database called “patients” with schema shown below.
use hospital_data;create table patients(PaitentId int, FirstName varchar(50), LastName varchar(50), Diagnosis varchar(50), ContactNumber varchar(50), PRIMARY KEY(PaitentId));
  • Insert some dummy data into the table
INSERT INTO patients VALUES (001, 'George', 'Michel', 'Covid-19', '0714945485');
INSERT INTO patients VALUES (002, 'Peter', 'Williams', 'Covid-19', '458490868');
INSERT INTO patients VALUES (003, 'Sophie', 'Robinson', 'Malaria', '98588486856');
INSERT INTO patients VALUES (004, 'Carlos', 'Nelson', 'Diphtheria', '98588486856');
INSERT INTO patients VALUES (005, 'Austin', 'Martin', 'Diabetes', '98588486856');

Implementation of Ballerina Service

  • Create a new Ballerina project using command “bal new hospital_system
  • Create a new file called “hospital_service.bal” to implement the service.
  • Copy paste the following code there
  • It is a Ballerina service with four resources which exposes CRUD capabilities on the table “patients”.
  • Note that we have imported “ballerinax/mysql.driver”, “ballerinax/mysql” and “ballerina/sql” modules. mysql is the Ballerina connector used to create mySQL connection with proper authentication with the help of mysql.driver module which contains the MySQL driver. Latter is used for constructing SQL queries with Ballerina.
  • Using Ballerina it is easy to extract values from JSON payload and assign them to variables.
  • Ballerina automatically maps JSON message with patient information to Patient record. You can manipulate information in Ballerina native way.
  • Later we use these values as the SQL query parameters. Note the language syntax for that (eg: ${patientId}) inside ParameterizedQuery.
  • Note resource function signature. Following defines a get resource with the path patients and takes diagnosis as a query parameter in the request. Response will be a JSON message. Learn more about REST capabilities here.
resource function get patients(string diagnosis) returns json {...}

Run the service

org = "ballerinax"
name = "mysql.driver"
version = "1.1.0"
Ballerina Hospital Service Implementation
Running the service

Test the service

"patientId": 006,
"firstName": "Hasitha",
"lastName": "Abeykoon",
"diagnosis": "Blood Pressure",
"contactNum": "0713467892"
curl -v -X POST -H "Content-Type:application/json" -d@patient.json
"patientId": 006,
"firstName": "Hasitha",
"lastName": "Abeykoon",
"diagnosis": "Blood Pressure",
"contactNum": "0713467899"
curl -v -X PUT -H "Content-Type:application/json" -d@patient.json
curl -v -X GET -H "Content-Type:application/json"
[{"PaitentId":1, "FirstName":"George", "LastName":"Michel", "Diagnosis":"Covid-19", "ContactNumber":"0714945485"}, {"PaitentId":2, "FirstName":"Peter", "LastName":"Williams", "Diagnosis":"Covid-19", "ContactNumber":"458490868"}]
curl -v -X DELETE -H "Content-Type:application/json"

Where to go from here

  1. You are exposing above API with plain HTTP. You can make it a secured service applying security to the service.
  2. Need a more managed service? Try this service in Choreo. You will be able to see statistics, define throttling policies, control access for the service. Choreo apps are written in Ballerina — so you can take above service as it is!
Choreo- managing your API




Associate Technical Lead at WSO2, Blogger

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

OpenMP by Gautham

Echo Ridge Lot 32 — Foundation All Done

Echo Ridge Lot 32 – Foundation All Done

Detect and Remove Cross-Site Scripting (XXS) Attacks from Text Input in Golang

A Developer’s Guide to Burning Out — Part 1

Development updates of X Predict Market

6 Essential software testing tools to add to your arsenal in 2019

Github Trick

Adding Enemy Spawn Waves to 2D Galaxy Shooter in Unity-Part 02!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hasitha Hiranya Abeykoon

Hasitha Hiranya Abeykoon

Associate Technical Lead at WSO2, Blogger

More from Medium

RETOOL :Database Front End UI

Data Stack Modernization

Data Stack Modernization — Blog image

dbt Setup and Installation

High level overview of how dbt aids in the ELT process

Traferium: The Key to Successfully Migrating Data to the Cloud

Traferium Time & Cost Savings infographic