REST API Flashcards

1
Q

What is a REST API?

A

REST API is an API that follows the guideline provided by REST.

The Three key elements of REST Api paradigm are:

  1. Client software on user computer used to initiate communication with server
  2. Server offers an API as a means of access to its data or features.
  3. Resource is any piece of content that the server can provide to the client

For the client to gain access to a resource from the server, the client must make an HTTP request to the API endpoint

The server then sends the client an HTTP response with encoded data on the resource

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an API?

A

an API is the tech that glues together all the pieces of the digital universe by serving as a communication middleman for each piece.

An API is an Application Programming Interface which basically acts as a middleman for applications to communicate with each other

this allows programmers to program systems to communicate with other systems, without either system knowing the inner workings of the system

a web api facilitates the exchange of formatted queeriees between clients and serveres

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three principles that serve as the pillars of API?

A
  1. Limited Access: client only has access to the specific resource its requesting as defined by the route
  2. System independent: regardless of changes to the inner system, the API still works i.e. a jukebox can have different mechanics but the api of delivering/playing music is the same and is functional either way
  3. Simple in use: should be easy to use and difficult to misuse. Should be easy to do simple tasks, possible to do complex tasks, and impossible/difficult to do wrong tasks.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Reuse depends on

A
  1. good design

2. great documentation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the structure of a REST Request?

A

a REST request includes an HTTP method, an API endpoint, headers, and
a body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an HTTP method?

A

an HTTP method describes that action to be done to the resource. There are 4 methods known as CRUD:

POST to Create a resource
GET to Retrieve a resource
PUT to Update a resource
Delete to Delete a resource

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an endpoint?

A

an endpoint contains a Uniform Resource Identifier (URI) indicating where and how to find the resource on the internet.

the most common type of Uniform Resource Identifier is the Uniform Resource Location (URL) which serves as a complete web addres

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Headers?

A

Headers store information relevant to both the client and the server

Mainly, headers provide authentication data - an API key, name or IP address of the computer where the server is installed, and the info about the response format

Headers have 3 pieces of info:

  1. authentication info
  2. location/destination of the server
  3. info about the response format
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the body?

A

the body is used to convey additional info to the server. For instance, it could be a piece of data you want to add or replace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the response?

A

the server doesn’t send the resource itself, instead it sends its representation - a machine-readable description of its current state

it can be represented in many formats but mainly XML or JSON

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What makes an API RESTful?

A

The idea of REST is to organize interactions between two independent machines, the client and the server

REST is a set of constraints to guide and describe how the server processes requests and responds to them

The constraints outlined by REST adds properties to the system that makes it more desirable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the constraints outlined in REST?

A

Client-Server Autonomy

Uniform Interface

Layered Architecture

Caching

Stateless Interactions

Code on Demand

Priority of Business Needs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

REST Constraint 1) Client-Server Autonomy.

What is the Client-Server Autonomy constraint?

A

Client-Server Autonomy mean that client and server work independently using to two separate tech stacks.

There is a separation of responsibilities between the client and the server

The client does not need to know about how inner working of the server (i.e. business logic) and the server doesn’t need to know the inner workings of the client (i.e. user interface)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the desirable properties gained from Client-Server Autonomy?

A

since the client doesn’t depend on the inner workings of the server and the server doesn’t depend on the inner workings of the client, the client (API consumer) and the server (API provider) can be modified without disrupting the communication or service between the two.

Properties: modifiability, improved system reliability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is meant by the REST constraint of Uniform Interface?

A

Uniform Interface dictates a standardized way to communicate with a given server. The server uses the same interface for all clients and requests.

The standardization requires:

  1. Unique resource identifier assigned to each resource
  2. Self-descriptive messages explaining how to interpret them and what to do next
  3. The ability to manipulate a resource through its representation in JSON or XML
  4. Hypermedia linking to related resources

Uniform interface should help developers easily grasp the logic of an API.

Properties gained: Ease of use, shared understanding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is meant by the REST constraint of Layered Architecture?

A

a RESTful system has a layered structure in which each layer works independently and interacts only with the layers connected to it.

When calling a server the client doesn’t know if there are any intermediaries along thee way.

It is bc of this layered architecture that we are able to place a proxy or load balancer between the client and server.

so we add a load balancer to improve scalability and security to improve system safety, yet the client doesn’t have to worry about either of these - only has to make the request the to the server and process the response back from the server

17
Q

What are the desirable properties gained by the REST constraint of Layered Architecture?

A

Improved scalability since there can be a proxy or load balancer
and improved system safety since there can be a security layer

18
Q

What is meant by the REST constraint of Caching?

A

The REST API allows the client to store frequently accessed data on their side instead of requesting them again and again.

19
Q

What are the desirable properties gained by enforcing the Caching constraint of REST API?

A

Caching allows the app(client) to make less calls to the server which means less load is placed on the server which reduces the latency of the server. As a result, the app(client) becomes more responsive and reliable

20
Q

What is meant by the REST constraint of Stateless Interaction?

A

Stateless interaction means that the API doesn’t store any information on the request and treats each request as a unique first time interaction.

Therefore, there is no information related to a previous request available for access or reference during a current request.

All data on the current client’s state is contained in the request body.

21
Q

What are the desirable properties gained by enforcing the Stateless Interaction constraint of REST API?

A

Properties gained: enhanced performance, app reliability

REST APIs don’t have to deal with server-side state synchronization logic
since there is session independence, any server can process requests. This improves the performance of the application and reduces the risk of the system crashing.

22
Q

What is meant by the REST API constraint of Code on Demand?

A

Instead of sending JSON, the server may return executable code on the client’s demand

23
Q

What are the desirable properties gained by implementing the Code on Demand constraint in REST API?

A

allows for feature customization and extended functionality

24
Q

What is meant by the REST Api constraint of Priority of Business Needs?

A

REST is about the design style and guide of the Web’s architecture.

Flexibility is important. If a REST constraint opposes business needs, then.. work around it

25
Q

Build a REST API using Gin and Go

A
  1. create folder for project
  2. in terminal, cd to project folder
  3. in terminal run clear command
  4. in terminal run ‘go mod init this creates an index file aka mod file. It’s like package json. It contains an inventory of all your dependencies and allows the compiler to know where the root folder of your project is
  5. check it works
  6. create a ‘make file’ which allows you to run scripts using commands
  7. add go run main.go in make file when we run make dev
    interlude: what’s Gin about? gin is a web framework, basically Express for Node or Flask for Python. Gin is a routing solution that includes JSON Marshalling / Unmarshalling and grabs params from URL
  8. download gin w the command ‘go get -u github.com/gin-gonic/gin this download will add these dependencies to our mod file
  9. with gin, in main.go, we create a simple router program: 1. declare and initialize Gin Router, 2. Declare and assign a route, 3. Run, starts up the router makes sure all endpoints are listening and waiting for requests on the url.
  10. verify the router is running and listening at the url with the API endpoint
  11. build out structure folder and files to separate endpoints, naming convention for route files is route_method.go
  12. create functionality for first route
  13. go back tot main.go and add this route handler to the appropriate route and method
  14. create package for date storage and methods
  15. add test case for the methods (should be self contained)
  16. run tests by entering terminal cmd go test ./… or go test cover- ./…
  17. add files for the additional routes