• No results found

4.5 REST-ful Application Programming Interface (API)

4.5.7 Data representation

When making a REST-fulAPI the resources have data associated with them. In order to represent the data, there are several ways of presenting data to the client. The ones that are used in this project are XML and JSON format. The next paragraphs will discuss more about these two formats.

4.5.7.1 Extensible Markup Language (XML)

XML is an acronym that stands for Extensible Markup Language. XML has the advantage of being readable for both humans and machines. The focus of XML lies on being used for data structure. XML is used in different services and applications, and also in HTML to represent data in a structured way.

A XML document starts with the document type declaration. It has a root tag and within this tag there are some child nodes. The next example (see Figure 37)shows the structure of a XML-file. (Bray, 2008)

Moreover, a XML document also contains attributes with values.

<?xml version="1.0"?>

Figure 37 XML example (Data, 2013)

4.5.7.1.1 Applied to the project

In this project the use of XML is possible, since the data was represented as a string value, and thus there were no complex data types. There are two ways to parse XML data files, SimpleXML and DOM. SimpleXML is used in this project to process the data given by the code behind file. The following example shows how the XML data is parsed.

Figure 38 shows how the XML is parsed with the function simplexml_load_string().

The string that was received was converted to UTF8 because the SimpleXML function does not support the ISO-8859-1 character set.

<?php

//HTTPS url to the code behind where the data is processed.

$url='https://localhost/bank/API/transaction.php/'.$_SESSION['ytunnus'].'?lang

/* 0: Don?t check the common name (CN) attribute

* 1: Check that the common name attribute at least exists

* 2: Check that the common name exists and that it matches the host name of the server

*/

curl_setopt($client,CURLOPT_SSL_VERIFYHOST,2);

// Certificate is necessary to communicate with the code behind

curl_setopt($client,CURLOPT_CAINFO,"C:\\xampp\\apache\\conf\\ssl.crt\\ser

4.5.7.2 JavaScript Object Notation (JSON)

JSON is an acronym that stands for JavaScript Object Notation. JSON is one of the ways that is used in this project. It is a data-interchange format, easy to read for both machines and humans.

JSON contains three types of data:

Scalar (number, string, Boolean, null) Array

Object

The scalar types are represented by a single value, arrays are an ordered list of values and objects are a collection of unordered set of key:value pairs, key is a string and value is an arbitrary type. (Jansen, 2011).

The next example shows how a JSON object looks like:

Figure 39 shows the JSON version of the XML example, in this example it is also possible to nest multiple values. Here the values are all of the type string, which is not necessary.

4.5.7.2.1 Applied to the project

The use of JSON representation is also possible to use in this project, though some changes are necessary to make it work. One of the advantages of using JSON is that it supports other types, like arrays, booleans, objects,….

{

"note": {

"to": "Tove",

"from": "Jani",

"heading": "Reminder",

"body": "Don't forget me this weekend!"

} }

Figure 39 JSON example

Figure 40 shows how curl example works with JSON. In this example the values that are received from the code behind file are decoded back to an array. This makes it possible to process the values as an array with named elements.

<?php

//HTTPS url to the code behind where the data is processed.

$url='https://localhost/bank/API/transaction.php/'.$_SESSION['ytunnus'].'?lang='.

/* 0: Don?t check the common name (CN) attribute

* 1: Check that the common name attribute at least exists

* 2: Check that the common name exists and that it matches the host name of the server

*/

curl_setopt($client,CURLOPT_SSL_VERIFYHOST,2);

// Certificate is necessary to communicate with the code behind

curl_setopt($client,CURLOPT_CAINFO,"C:\\xampp\\apache\\conf\\ssl.crt\\server

4.5.7.3 Conclusion

In this project the data is first processed as a XML format, but since there are some things to take in account when using XML a change is made to JSON.

Some constraints to take in account when using XML:

XML can be too rich in features, e.g. attributes and elements. An XML document can have elements with attributes and these elements can have sub-elements. This can cause confusion for the client and server side processing.

XML has some limitations concerning types. When the system is using other types than just string to represent the data, XML is not a good way to handle that.

If the XML is not well formed, the elements can be wrongly nested and this causes problems when processing the data.

(Jansen, 2011).

4.6 MVC framework

When developing a web-based application the MVC framework makes its entrance quite soon. A MVC framework is a software architecture where there is a Model, View and a Controller. This architecture enables separation of the different functions of the system. The main concept behind MVC is the possibility of code reusability. (see Figure 41)

HTTP Response HTTP Request

Model: The model stores the applications data objects, and does not know anything about the way data is shown to the end-user. The model has a straight connection to the database. It responds to the requests that are given by the controller and returns the data that was requested.

View: The view presents the data that was received from the model to the end user. There are templates that can show the data, which are made in HTML, CSS and JavaScript.

Controller: The controller responds to the different HTTP request, like POST and GET. The main function of the controller is to call and pass the right objects and data to the corresponding action.

(Pastor, 2010)

Request data

Model View

Controller

End-user

Return data Presenting

data Return

View

Figure 41 MVC architecture

In this project a MVC framework is was used. The MVC framework was Laravel 3.

Laravel is a MVC framework that is developed by MIT. When the API was starting to grow and REST was more and more important, the framework wasn’t capable to support the different constraints of REST. So to make the API with Laravel wasn’ t possible. The API was now made without the use of a MVC framework.

MATURITY MODEL

Related documents