Introductie tot API’s

Introductie tot API's in Python

Chris Ramakers

Engineering Manager

Wat is een API?

  • Application Programming Interface
  • Set van communicatie­regels en mogelijkheden
  • Laat software­applicaties met elkaar communiceren

Een diagram van API-interacties tussen een e-mailclient en -server om een e-mail te sturen naar john@acme.com.

Introductie tot API's in Python

Web-API’s, clients en servers

  • Web-API’s communiceren via internet met HTTP
  • Client stuurt een request naar een server
  • Server stuurt een response terug naar de client

Een diagram van client-serverinteractie via internet met een API, met gelabelde request- en response-berichten.

  • Request/response-cyclus
Introductie tot API's in Python

Typen web-API’s

  • SOAP
    • Strikt en formeel API-ontwerp
    • Enterprise-applicaties
  • REST
    • Simpel en schaalbaar
    • Meest gebruikte API-architectuur
  • GraphQL
    • Flexibel
    • Geoptimaliseerd voor performance
1 https://www.postman.com/state-of-api/api-technologies/#api-technologies
Introductie tot API's in Python

Werken met API’s in Python

urllib

  • Standaard bij Python
  • Krachtig maar minder gebruiksvriendelijk
from urllib.request import urlopen
api = "http://api.music-catalog.com/"

with urlopen(api) as response:
  data = response.read()
  string = data.decode()
  print(string)

requests

  • Veel krachtige features ingebouwd
  • Makkelijker te gebruiken
import requests
api = "http://api.music-catalog.com/"

response = requests.get(api)
print(response.text)
Introductie tot API's in Python

Laten we coderen!

Introductie tot API's in Python

Preparing Video For Download...