API Overview

COMP 426 w04.01

Agenda

  1. REST API
  2. Why REST?
  3. Constraints
  4. Resources
  5. How do we interact with a REST API?
  6. Methods
  7. Alternatives?

REST

REpresentational State Transfer

RESTful constraints

Uniform interface

Client <=> Server

Stateless

Cacheable

Layered system

Code-on-demand

Data elements

Resources and messages

Me, your instructor

Resource identifier


Uniform Resource Identifier (URI): John D. Martin III
Uniform Resource Locator (URL): https://johndmart.in
          

Representation

Picture of John wearing a UNC baseball cap backwards and a black cloth mask inside a car, looking away from the camera

Representation metadata


Picture of John wearing a UNC baseball cap backwards and a black cloth mask inside a car, looking away from the camera
          

Representation metadata


alt="Picture of John wearing a UNC baseball cap backwards and a black cloth mask inside a car, looking away from the camera"
          

$ ls -lah john_face.jpg
-rw-r--r-- 1 john john 34K Feb 10 11:31 john_face.jpg
          

Resource metadata


Created: 1980-09-05T00:35:00-5:00
Last major update: 2021-09-05T00:35:00-5:00
Origin: 43.0125°,-83.6875°
Current: 35.9049°,-79.0469°
          

Control data


Cacheable: true
In-cache: true
            

Message: JSON


{
  "uri": "John D. Martin III",
  "url": "https://johndmart.in",
  "image": {
    "url": "./john_face.jpg",
    "modified": "2022-02-10T11:31:00-5:00"
  },
  "location": {
    "origin": [ "43.0125", "-83.6875" ],
    "current": [ "35.9049", "-79.0469" ]
  },
  "version": {
    "initial": [ "v0.0.0", "1980-09-05T00:35:00-5:00" ],
    "major": [ "v41.0.0", "2021-09-05T00:35:00-5:00" ],
    "current": [ "v41.5.5", "2022-02-10T00:35:00-5:00" ]
  },
  "cache": {
    "cacheable": "true",
    "in-cache": "true"
  }
}
          

Methods

  1. Create
  2. Read
  3. Update
  4. Delete

HTTP Methods

  1. POST
  2. GET
  3. PUT
  4. PATCH
  5. DELETE

Corresponding Methods

  1. POST == Create
  2. GET == Read
  3. PUT == Update (replace)
  4. PATCH == Update (modify)
  5. DELETE == Delete

Messages

GET URI


curl -X GET https://johndmart.in/some/api/endpoint/info/
          

JSON Body


{
  "uri": "John D. Martin III",
  "url": "https://johndmart.in",
  "image": {
    "url": "./john_face.jpg",
    "modified": "2022-02-10T11:31:00-5:00"
  },
  "location": {
    "origin": [ "43.0125", "-83.6875" ],
    "current": [ "35.9049", "-79.0469" ]
  },
  "version": {
    "initial": [ "v0.0.0", "1980-08-10T00:35:00-5:00" ],
    "major": [ "v41.0.0", "2021-08-10T00:35:00-5:00" ],
    "current": [ "v41.6.0", "2022-02-10T00:35:00-5:00" ]
  },
  "cache": {
    "cacheable": "true",
    "in-cache": "true"
  }
}
            

URL encoded Body

         
https://johndmart.in/some/api/endpoint/?uri=John%20D.%20Martin%20III&
url=http%3A%2F%2Fjohndmart.in&imgurl=.%2Fjohn_face.jpg&
imgmodified=2022-02-10T11%3A31%3A00-5%3A00&
originlat=43.0125&originlong=-83.6875&curlat=35.9049&
curlong=-79.0469&versioninit=v0.0.0&
timeinit=20221980-08-10T00%3A35%3A00-5%3A00&majorupdate=v41.0.0&
timemaj=2021-09-05T00%3A35%3A00-5%3A00&cur=v41.5.5&
timecur=2022-02-10T00%3A35%3A00-5%3A00&cacheable=true&cached=true
            

POST URI

         
curl -X POST -d "uri=John%20D.%20Martin%20III&
url=http%3A%2F%2Fjohndmart.in&imgurl=.%2Fjohn_face.jpg
&imgmodified=2022-02-10T11%3A31%3A00-5%3A00" 
https://johndmart.in/some/api/endpoint/send/            
            

Alternatives?

SOAP

Simple Object Access Protocol

  1. Strict standard
  2. XML only
  3. REST can use SOAP, but SOAP cannot use REST
  4. More complex to implement than REST
  5. Used for secure transactions
  6. Atomicity Consistency Isolation Durability (ACID) compliant
  7. Newer than REST (ca. mid-00s)



  
  
  
    
      John D. Martin III
    
  

            

GraphQL

Graph Query Language

  1. Data-oriented
  2. Very specific
  3. No waste
  4. New school (2015)

REST: get user name

REST: get user name
GET /users/123
/**
{
  "id": "123",
  "name": "John D. Martin III",
  "birthdate": "1980-08-10",
  "location": {
    "city": "Chapel Hill",
    "state": "North Carolina",
    "country": "United States of America"
  }
}
*/
            

GraphQL: get user name


query {
  User (id: "123"){
    name
  }
}
            

RPC

Remote Procedure Call

  1. Request <=> Response
  2. Execute remote server code as if local
  3. Client agnostic of code location
  4. Action-oriented
  5. Old school (ca. 1981, but conceptually older)