Professional Documents
Culture Documents
April 9, 2024
Agenda
2
What is an API?
3
What is an API ?
API?
• Interface = The portion of an application that is exposed to the
outside world for users to interact with
• UI = User Interface
• Audience: Users
• API = Application Programming Interface
• Audience: Developers, other application
• A collections of functions or methods with known signatures and data
models
4
What is an Different types of API’s
API?
• Libraries and Frameworks
• A software library
• Ex. Java/JDK API, React API
• Web Services
• The interface for one application to exchange data with another over the
web
• Ex. SOAP, REST
5
What is an Web Services / Web API
API?
• For the remainder of this course, we’ll be focused on web services /
web API’s
https://towardsdatascience.com/api-management-platform-data-science-projects-43120558a000
6
What is a REST API?
7
What is a Message Protocol vs Transport Protocol
REST API?
Message Protocol Transport Protocol
• Structure and format of • Exchange of messages
messages • Ex. HTTP, WebSocket, TCP Socket
• Ex. SOAP (Simple Object
Access Protocol)
8
What is a What is REST?
REST API?
• Representational State Transfer
• An architectural style for sharing data between applications
• Comprised of 6 architectural constraints
• Implemented over HTTP
Example
• Service: Doctor Profile
Representational • Resource: Doctor
state transfer • Representation: {Id: 123, Name: ’Dr. Phil’}
• Verb: GET
10
What is a The 6 architectural constraints of REST
REST API?
Uniform Interface
• Define the interface between the client and the server
• Simplifies and decouples architecture
• Restful design
• HTTP verbs (e.g. GET, PUT, POST, DELETE)
Stateless
• The server does not remember anything about the user
using the API
• Each individual request contains all the information the
server needs to perform the request and return a
response, regardless of other requests made by the
same API user
11
What is a The 6 architectural constraints of REST
REST API?
Client-Server Separation
• The client and server act independently, each on its own
• Interaction between them is only in the form of requests,
initiated by the client only
• The server only responds in reaction to a request from a
client
Cacheable
• Server response contains information about whether or
not the data is cacheable
• Client can avoid requesting the same data again and
again
• Client should know when the current version of the data
expires
12
What is a The 6 architectural constraints of REST
REST API?
Layered System
• Between the client who requests a resource and the
server who responds, there might be a number of servers
in the middle
• These servers might provide a security layer, caching
layer, load-balancing layer or other functionality
• Additional layers should not affect request or response
Code-on-demand (optional)
• Client can request code from the server, usually in the
form of a script
• The client can then execute that code
13
What is a Exercise
REST API?
• Make a REST API with Express
• GET /home { response: “Hello World!” }
14
Hello World
https://expressjs.com/en/starter/hello-world.html
This app starts a server and listens on port 3000 for connections. The app
responds with “Hello World!” for requests to the root URL (/) or route. For every
other path, it will respond with a 404 Not Found.
The example above is actually a working server: Go ahead and click on the URL
shown. You’ll get a response, with real-time logs on the page, and any changes you
make will be reflected in real time.
• Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to
routing, see Basic routing.
• You define routing using methods of the Express app object that correspond to HTTP methods; for
example, app.get() to handle GET requests and app.post to handle POST requests. For a full list, see
app.METHOD. You can also use app.all() to handle all HTTP methods and app.use() to specify middleware as
the callback function (See Using middleware for details).
• These routing methods specify a callback function (sometimes called “handler functions”) called when the
application receives a request to the specified route (endpoint) and HTTP method. In other words, the
application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it
calls the specified callback function.
Routing • In fact, the routing methods can have more than one callback function as arguments. With multiple callback
functions, it is important to provide next as an argument to the callback function and then call next() within the
body of the function to hand off control to the next callback.
Doctors and Patients
18
Doctors and Problem Statement
Patients
• You are web developer building a healthcare product
• You need to retrieve patient data from the server
• What are some of the typical ways to retrieve data in this scenario?
19
Potential Solutions
20
Doctors and REST Implementation
Patients
• Supports any hypertext format (JSON, XML etc.)
• CRUD Operations (GET, PUT, POST, DELETE)
• APIs are independent of the client
• Fewer resources in transport as well as description
• Caching utilizes less network resources
21
Doctors and API path naming conventions
Patients
Verbs
• /getDoctors
Nouns
• More specifically, plurals
• /doctors
Filter
• Use ?
• GET /doctors?lastname=smith&city=waterloo
22
Bad
Design
Doctors and HTTP Verbs
Patients
GET Retrieve a representation of a resource
24
Url and HTTP Verb
26
Doctors and Patients Visits Doctors
Patients
• ID • Patient ID • ID
• Name • Doctor ID • Name
• Completed
router.route("/patients/:id").get(function(req, res) {
var patient = db.getPatient(req.body.id);
res.json(patient);
});
router.route("/doctors/:id").get(function(req, res) {
var doctor = db.getDoctor(req.body.id); …but where might
res.json(doctor); our solution struggle?
});
router.route(“/visits/:patientId”).get(function(req, res) {
var visits = db.getVisits(req.body.patientId);
res.json(visits);
});
27
Doctors and Potential pitfalls of REST
Patients
• REST APIs are synchronous & hence resource intensive
28
The wrong approach
29
Doctors and Documentation with Swagger
Patients
• Swagger is a framework for describing an API using a common
language everyone can understand
• Think of it like a blueprint for a house
30
Sample Swagger file
31
Swagger Editor
Swagger Codegen
Code Generator
(client or server)
Swagger Editor
Editor for
designing and API
SwaggerUI
Interactive API
documentation
32
Doctors and Two approaches to Swagger
Patients
1) Top-Down
• Design-first
• Swagger Editor Swagger/OpenAPI Spec Swagger Codegen
2) Bottom-Up
• Generate Swagger documentation for an existing API
33
Doctors and Exercise
Patients
34