DataScience_Examples

All about DataSince, DataEngineering and ComputerScience

View the Project on GitHub datainsightat/DataScience_Examples

API (Application Programming Interface)

General

Letter Word Meaning
A Application Software that does a task
P Programming Program that does the task in the Application
I Interface Place to the program to run

Advantages

request_response

API Mashup: An API that is calling other APIs.

Web Service

API that uses the internet. All web services are APIs, but not all APIs are web services.

Web services use:

HTTP (HyperText Transfer Protocol)

http REQUEST RESPONSE
startline Version (1.1), Method, Folders, Parameters Version (1.1), Status
headers Host (www.google.com), Token Cookies, Html
blank line    
body Username, Password Html

Startline

  REQUEST RESPONSE
Name Start Line, Request Line Start Line, Response Line, Status Line
HTTP Version HTTP/1.1 HTTP/1.1
Method GET, POST, PUT, DELETE -
API Program Folder Location /search -
Parameters ?q=tuna -
Status - 200 OK
Format Method(space)API Program Folder+Parameters(space)HTTP Version HTTP Version(space)Status code
Example GET /search?q=tuna HTTP/1.1 HTTP/1.1 200 OK
Methods

C.reate R.ead U.pdate D.elete

Method Function Itempodent (safe to repeat)
GET Get information yes
POST Create information no
PUT Change Information yes
DELETE Delete Information yes
Status Codes
Code Description
1xx Still running, wait
2xx Success
3xx Redirection
4xx Error from origin
5xx Error from destination

XML (eXtensible Markup Language)

W3 Schools

test.xml

<?xml version="1.0"?>
<Pizza>
  <Size>Small</Size>
  <Toppings>
    <Topping>Onions</Topping>
    <Topping>Mushrooms</Topping>
  </Toppings>
</Pizza>

JSON (JavaScript Object Notation)

W3 Schools

test.json

{ "Pizza" : [
  {"Size" : "Small",
    "Toppings" : ["Onions","Mushrooms"],
  },
  {"Size" : "Large",
    "Toppings" : ["Ham","Egg"],
  }
  ]
}

SOAP (Simple Object Access Protocol)

W3 Schools

Soap uses a WSDL (Web Services Description Language) that describes the web service. SOAP uses POST method.

Item Content
Start Line POST WSL HTTP Version
Header Line Content-Type: text/xml
Blank Line -
Body XML envelope formed using WSDL
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Header>
...
</soap:Header>

<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>

</soap:Envelope>

REST (Representational State Transfer)

W3c

Item Content
Start Line GET, POST, PUT, DELETE, etc
Header Line All
Blank Line -
Body JSON, XML, Image, Html, …

Start Line

PUT

Header Line

Authorization:Bearer ...
Accept:application/json
Content-Type:application/json
Content-Language:en-US

Body

{
  "availability": {
    "shipToLocationAbailability": {
      "quantity": 50
    }
  },
  "condition": "NEW",
  "product": {
    "title": "An Item"
  }
}

Demo

Twitter API

import tweepy

client_secret = '1eQr5yqKHnzXKJHCCsZ-fwo4YZ1OvXdd6k-5ub68aANVskxr8N'

ak = 'xxx'
aks = 'xxx'

at = 'xxx'
ats = 'xxx'

def OAuth():
    try:
        auth = tweepy.OAuthHandler(ak,aks)
        auth.set_access_token(at,ats)
        return auth
    except Exception as e:
        return None

oauth = OAuth()
apicall = tweepy.API(oauth)

apicall.update_status('Here is a sample tweet from the API call program.')

print('Tweet created')

API Access

Authorization and Authentication

Name Authentication Authorization Examples
No Auth N N Google search page
Basic Auth Y N Email
Bearer Token N Y Not many
OAuth Y Y Wayz
Two Factor Y N Git

OAuth (Open Authorization)

An application is authorized to acces certain ressources.

request_response

Postman

API Directories

Postman

Python

import requests

url = "https://httpbin.org/bearer"

payload = {}
headers = {
  'Authorization': 'Bearer sdfgsdfg'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Webhooks (Reverse API)

A hook is an event that calls a webservice.

webhook

  1. Event/Trigger
  2. Configuration (Endpoints)
  3. HTTP request
  4. HTTP response </a>

Microservice

A microservice is a ‘small’ API.

Monolith Architecture Microservice
One API Many APIs
API calls many programs Specific API for each program
  + scalable, language independent, specialised teams
  - inconsistent