You are on page 1of 16

Welcome

to the Module 5, first lesson, Introduction to the cloud. In this lesson we


will introduce some cloud concepts, and how to manage data that is stored in the
“cloud”.

1
In the simplest terms, cloud computing means storing and accessing data and
programs over the Internet instead of your computer's hard drive. The cloud is just a
metaphor for the Internet. It goes back to the days of flowcharts and presentations
that would represent the Internet as nothing but a cloud, accepting connections and
exchanging information as it floats.

2
A web service is just a web page meant for a computer to request and process. A
Web service is a service offered by an electronic device to another electronic device,
communicating with each other via the World Wide Web. In a Web service, Web
technology such as HTTP, originally designed for human-to-machine
communication, is used for machine-to-machine communication, more specifically
for transferring machine readable file formats such as XML and JSON.

We can identify two major classes of Web services:


• REST-compliant Web services , in which the primary purpose of the service is to
manipulate XML representations of Web resources using a uniform set of
"stateless" operations;
• And arbitrary Web services , in which the service may expose an arbitrary set of
operations.

We will focus on the former.

3
REST means Representational state transfer. REST or RESTful web services are one
way of providing interoperability between computer systems on the Internet. REST-
compliant web services allow requesting systems to access and manipulate textual
representations of web resources using a uniform and predefined set of stateless
operations.

The WWW is used by billions of people. And Http is one of the most used protocols in
the world. It is Multiplatform and Multi OS. So, why not using it to transfer/share data
between applications? That lead us to REST.

4
REST is a standard way to exchange information between machines. What is
exchanged is called a resource. A resource is a URL or URI. URIs are the equivalent of
a noun. Verbs describe actions that are applicable to nouns, in our case to URLs. In
REST we use universal verbs only, that are:
GET to retrieve information
POST to add new information, showing its relation to old information
PUT to update information
DELETE to discard information

An API that adheres to the principles of REST does not require the client to know
anything about the structure of the API. Rather, the server needs to provide whatever
information the client needs to interact with the service. We send a request of a
resource and we get a response with a file format JSON/XML with all the information
that we require.

5
A URI can be a book in the Amazon website. For example, this book, “RESTful Web
Services Cookbook”, has a unique URL that points to it. The price can change, new
comments can be added, but the link, the URL, is always the same. It points always to
the book.

6
But, a URL can also be a sensor value. For example, this address points to the last
value of the temperature sensor specified by its URL. In this case, its value is 9.5º.

When we perform this address request, we receive a JSON file with information as
shown in this image. We have several fields as magnitude, value, time, date, etc. We
could upload new information using the same pattern. Or receive information about
an specific value.

7
So, why is REST so important?

It is a client-server structure. The server has the resources, and the client wants them.
But what does it get? A representation of the resource, not the resource itself.

A web page is a resource? A web page is a representation of a resource. Resources


are just concepts. URIs tell a client that there's a concept somewhere. Clients can
then request a specific representation of the concept from the representations the
server makes available. The representation is the current state of the resource. We
can have a resource, and we get different representations every time we request. The
representation is the current state of the resource. For example, the current
temperature value. Here, 20, 21 and 22 degrees.

Thus, every request from the client must contain all necessary information. It cannot
take advantage of any context information stored in the server. That means stateless.

Finally, all RESTful services must use a uniform interface, an interface that every client
application is able to understand. This, as we said, using http, get, post, delete.

8
There are many libraries that we can use to operate with REST services. For example
curl, or pycurl in python, urllib, etc. Also we can use a webpage called Hurl, to send
and receive REST requests and responses. In the next lesson we will make an example
with urllib and python.

9
By using a REST interface, we want to operate with data. What we can do is:
Retrieve information from the server, using the “get” command
We can upload information to the server from my device, using the “post” command
We can update information in the server, using the “put” or “patch” commands
And finally, we can delete information in the server, using the “delete” command

10
And how does a rest command look like?

It has three main parts. The first part is the URL or destination, the web address we
want to send information to or from.

The second part are the headers, main information about the command or method:
for example content-type, if it is JSON or XML

and normally the key to access the data, normally called APIkey, so not everybody can
access the data, only those who knows the APIkey.

Finally, the third part, it is where we can specify parameters in the boy of the
message.

For example, in a GET command we can specify how to get the data: if we want to
sort the data, or in which order we want the data, how many samples we want to get,
etc.

11
We can use the web I told you before “hurl.it” to get familiar with REST commands.
For example to get a list of the devices that we have, we can do the following. Choose
destination URL, this is a web/URL that we have to know, for example from Carriots,
that we will be using in the course. Then we choose the method, get, post, put, etc. It
is optional to use authentication, and then we write the headers, in this case Content-
type, we want JSON, the APIkey, this is a number that you have to know before, for
example the user-agent, if it is a raspberry or whatever, and we can include some
parameters, in this case order, 1 is oldest first, or we can write -1 to get newest first.

12
And this is what we get. We get three devices, total documents 3, since we asked for
a device, we get 3, each of this is a device. This belongs to device number 1, this to
device number 2, and this to device number 3. These devices are named, for
example, we get a name Pi1, mac1 and we get ddd.

The type of sensor, for example temperature, some other information, when they
were created, the owner, and so on.

Then we can ask also for this specific device, we get the ID of this device, so if we
include this in the URL, we get specific information about this device. And this is one
of the good things of REST, that we can navigate through the hierarchy of our system,
in order to get information deeper and deeper, information that we don’t have to
know in advance, we can get it directly from the system.

In the next lesson we are going to see how to work with carriots.

13
The POST method works in a similar way: we get destination, we get headers, JSON
and apikey and we get parameters or body.

But in this case, since it is a POST method, we have to specify the data we want to
upload in JSON or XML format. In this case we use JSON. We want to upload data to
the device specified by ”name", at the time "now", and the data will be
”temperature” data with the value 23, in this case 23 degrees.

--------
{"protocol": "v2", "device": "Pi1@fishminator.fishminator", "at":"now", "data":
{"temp":23}}

For other methods: req.get_method = lambda: "DELETE"

14
This is how we do it on hurl, writing the destination, the method, the headers, and
the parameters that we have explained. If everything goes well, we get an OK
response. Something like this. If not, we get an error message.

15
So that’s the end of this lesson. We have introduced the concept of cloud computing,
web services and REST services. REST services are widely used because they use
standard protocols as http and common interfaces that are know by any application.

We have presented the basic methods, GET, POST, PUT and DELETE to work with data.

In the next video we will see how to write our own REST application in python.

See you!

16

You might also like