10.8.3. General Purpose Web Service

<< Click to Display Table of Contents >>

Navigation:  10. FAQ > 10.8. Web Service, Real-time Data Streaming and Master Data Management >

10.8.3. General Purpose Web Service

 

You can transform any Anatella graph into a REST-full web service using the procedure described in this section. This procedure involves a Web PHP server.
 

 

AN76E5~1_img5

A web service is, typically, a small process that:

1. Waits for an incoming HTTP connection (typically initiated using your Browser or with cURL).

2. Extract some parameters from the HTTP connection. These parameters are typically stored in the URL part of the HTTP header (in the case of a GET http request) or stored in the data part of the HTTP request (in the case of a POST http request).

3. Compute some response based on the parameters received.

4. Send back the response through the same connection (i.e. the same socket) that was used initially to received the parameters. This response is usually formatted a JSON structure.

 

 
 

AN76E5~1_img5

You’ll need a web PHP server to run the example given in this section. You can easily get such a web PHP sever by installing Kibella on your machine (since Kibella uses many PHP libraries).

 

 

 

We’ll use a small example to explain how to create a REST-full web service using Anatella&PHP.
 

Let’s now assume that:
 

1.We have a big  .gel_anatella file that describes all the phone-calls given on a given day. This .gel_anatella file contains the following columns:
 

a)The calling phone number: i.e. The column “A”.

b)The called phone number: i.e. The column “B”.

c)The duration of the call (chargedduration)

d)The price of the call (chargedamt)
 

2.We want to create a REST-full web service that returns (inside a .json structure) (nearly) all the available data that we have about the phone-calls emitted by a user-given phone number (we'll name this user-given phone number as the “requestedA”). To limit the size of the returned .json structure, we’ll limit the number of returned rows of information to a user-given upper limit (we'll name this user-given limit as the “requestedLimit”)..

 

 

The first step to create the requested REST service is to create an .anatella graph that:
 

1.Uses as “global parameters” the 2 user-given parameters:
 

a)the user-given phone-number named as “requestedA”.

b)this user-given limit named as “requestedLimit”.
 

2.Uses a “global parameter” named “fileOut” that is the name of the JSON file that contains the results.
 

3.Extract the required rows of data and save them as a .json file.
 

 

Here is the requested .anatella graph:

 

clip0400

 

The 3 “global parameters” used inside the above .anatella graph are:

 

AN76E5~1_img310

 

 

Here is an example of JSON output file:

 

 
{

 "A": "80056756567",

 "calls":

 [

   {

     "B": "57876545456",

     "chargedduration": 45,

     "chargedamt": 3870

   },

   {

     "B": "343214556873",

     "chargedduration": 28,

     "chargedamt": 1926

   }

 ]

}
 

 

 
The second step to create the requested REST service is to create a PHP code that:
 

1.Extract from the GET or POST http data the two user-given parameters: “A” and “Limit”.
 

2.Run the .anatella graph (“p.anatella”) that computes the JSON result file.
 

3.Return the JSON result file to the webservice calling process (i.e. to the web browser or to cURL).

 
 
Here is the requested PHP code:

 

clip0401

 

 
Here are more details and explanations about the above PHP code:

 

Rows 2-6: Check if the HTTP parameters “A” and “Limit” are present inside the HTTP request.
 

Rows 8-9: Copy the HTTP parameters “A” and “Limit” into the PHP variables $a and $nmax
 

Row 10-11: Compute the file name of the result JSON file.

 

Rows 13-14: Run the Anatella graph named “p.anatella” with the “correct” values for the “global parameters” named “requestedA”, “requestedLimit” and “fileOut”.
 

WARNING: The executable to run (i.e. AnatellaConsole.exe) must be written between double-qotes (and not between simple quotes!!) .
 

Rows 16-17: Return the JSON result file to the webservice caller (i.e. to the web browser or to cURL).
 

 

AN76E5~1_img5

In the above PHP code, you can replace $_REQUEST with $_POST or $_GET  to retrieve the values from the POST or GET http parameter exclusively.

 

 
We can call our webservice using the following cURL command-line:

 

"c:\soft\TIMi\curl\curl" "http://localhost/testAnatella.php?A=80056756567&Limit=2"

 

 
At each call, our webservice creates one new temporary file (inside the directory “f:/wamp64/www/tmp”). To prevent the undesired accumulation of these temporary files, you should run a “cleaning” job once per day (i.e. schedule an Anatella job with Jenkins that runs daily). This “cleaning job” is the following:

 

clip0402

 

 

Here is a comparison with the solutions exposed in the 2 previous sections:

 

Subject

Real-Time Data

Streaming  

(using the AN76E5~1_img316 NaïveIPServer action or the AN76E5~1_img296 MQTSubscribe action)

PHP-based solution

(as exposed in this section)

Comments

Typical response Time

A few milli-seconds

Half a second to several seconds

The PHP-based solution must start a new (Anatella) process at each connection: This is much slower than simply creating a new Row in memory.

Infrastructure

None

(at least for the AN76E5~1_img294

NaïveIPServer action)

Requires a web

PHP server

The web PHP server is not really a burden since it’s really easy to administrate (and you most certainly already have it for other reason: e.g. Kibella)

Versatility

Only a subset of all the Actions can be used (See the next section 10.8.4. for more details)

All the actions are available