1 of 25

RESTful APIs

Team I Tech Talk

Wid Alsadoon - Fariha Marzan - Janet Mbugua - Natalie Yuziuk

2 of 25

Background

3 of 25

History

Previously (1999): SOAP (Simple Object Access Protocol)

  • Handwritten XML document with Remote Procedure Call
  • Specified endpoint, POST SOAP envelope to endpoint
  • Notorious for being complex and NOT simple

Then (2000): Roy Fielding & colleagues set to create standard

  • Uniform interface (HTTP verbs: GET, PUT, POST, DELETE)
  • Stateless (self-descriptive requests)
  • Client-Server (Clear boundaries set)
  • Cacheable
  • Resource based

4 of 25

Definition

API (Application Programming Interface):

  • a set of rules that define how applications or devices connect to and communicate with each other

REST API (Representational State Transfer API):

  • An API that conforms to the design principles of the REST style; e.g. GET, POST, PUT, DELETE

5 of 25

Key Features of RESTful APIs

RESTful API is:

  • Stateless, meaning that each request contains all the information necessary for the server to understand and fulfill the request
  • Resource-based, meaning that resources are identified by unique URIs (Uniform Resource Identifiers)
  • Supports a limited set of operations, typically HTTP verbs such as GET, POST, PUT, and DELETE
  • Designed to be cacheable to improve performance and scalability

6 of 25

Purpose

7 of 25

Benefits of RESTful APIs

  • It is widely used and supported, making it a popular choice for web services
  • Flexible and can be used in a variety of contexts, from simple web applications to complex enterprise systems
  • It is easy to understand and use, making it accessible to developers of all skill levels
  • It promotes good software design practices, such as separation of concerns and modularity

8 of 25

Motivation

Client and Server Communication is vital to applications!

9 of 25

Function Usages

  • GET: allows server to find requested data and sends it back
  • PUT: server will update an entry in database
  • POST: permits server to create new entry in database
  • DELETE: allows server to delete entry in database

Example: GET api.marker.io/?color=Black

10 of 25

How we’re using Rest API

Express.js used as backend framework for building RESTful APIs with Node.js

Controllers required for each survey type

11 of 25

How we’re using Rest API

GET, POST, DELETE from REST seen above…

PATCH is like PUT, but only updates fields instead of entities (introduced to reduce bandwidth usage)

12 of 25

How we’re using Rest API

Example of person survey GET; used for retrieving person survey records from Mongo database or throwing error if the action fails

13 of 25

How we’re using Rest API

14 of 25

Demo

15 of 25

Follow Along!

16 of 25

Step 1: Initialize Server

Importing path, express

& body-parser

Initializes the Express server and sets it to a variable called app

Initializes the body-parser plugin

Setting our server to listen on port 5000 for requests

17 of 25

Step 2: GET data from REST server

Creates function for API to use on request. Calling GET request on ‘/’

Using .sendFile, any file can be sent as a response. In this particular case, the user will access the index.html page when they try to navigate to ‘/’.

18 of 25

Step 3: Connect to server

If successful, should return a corresponding statement such as:

19 of 25

Step 4 : POST data to REST server

Then returns the output to the REST server via POST

Within index.html

Creating a power function takes in the requested input and raises it to the power

20 of 25

Step 4 : POST data to REST server

Add function takes in the requested input and raises it to the power

Multiply function takes in the requested input and raises it to the power

21 of 25

Demo Clip

22 of 25

Bad Service Requests

  • 01:
    • Incorrect spelling of the url, or the parameter passed through is unknown
  • 02:
    • Request of a nonexistent resource
  • 03:
    • Too many resource requests
  • 04:
    • Higher permissions needed (API Key)

23 of 25

Let’s Try It Out

Choose one of these fun databases to pick from so you can try it!:

  • https://www.fruityvice.com/#3
    • Retrieves in depth information of fruits taxology and other information**
  • https://dukengn.github.io/Dog-facts-API/
    • Retrieves randomized dog facts. The number amount can be specified in the URL
  • https://github.com/robertoduessmann/weather-api
    • Retrieves weather information for a city for a specified time period
  • https://opentdb.com/api_config.php
    • Retrieves trivia questions that are customizable on difficulty, topic category, etc**

24 of 25

Q&A

(Questions?)

25 of 25

Possible questions:

Alternatives to REST: https://blog.bitsrc.io/not-all-microservices-need-to-be-rest-3-alternatives-to-the-classic-41cedbf1a907