1 of 73

Comprendre enfin les APIs

Harouna Diallo - Membre de DevsCom et Lead Tech chez MIS TECH

2 of 73

Presentation DevsCom

3 of 73

Agenda

  • Introduction
  • Historique
  • API REST
  • Bonnes Pratiques
  • Conclusion

4 of 73

Application Programming Interface

Interface

Programming

Application

5 of 73

Application Programming Interface

Application

6 of 73

Application Programming Interface

Interface

7 of 73

Application Programming Interface

Programme

8 of 73

Not use for a human

9 of 73

Les APIs exposent quelques choses

10 of 73

11 of 73

12 of 73

API

13 of 73

API

14 of 73

API

Programme

15 of 73

Les développeurs exposent des APIs (Services) et les programmes consomment ses APIs

16 of 73

Alors des APIs ?

17 of 73

Quelques types d’APIs

  • Les APIs de système d’exploitation

  • Les APIs de langage de programmation

  • Les APIs des services web

18 of 73

Programme

API GOOGLE MAPS JS

19 of 73

Pourquoi les API Rest sont une innovation ?

20 of 73

21 of 73

22 of 73

23 of 73

24 of 73

25 of 73

26 of 73

Historique : Application programming Interface (API)

Remote procedure call - RPC 1980’

27 of 73

Historique : Application programming Interface (API)

Simple Object Access Protocol - SOAP

  • Le successeur de XML-RPC
  • Basé sur XML
  • Arrêt 2019’

28 of 73

Historique : Application programming Interface (API)

Representational State Transfer - REST

  • Roy Fedling 2000’
  • Basé sur HTTP verbs (GET, POST, PUT...)
  • XML, JSON…
  • Pas de protocol, ni de format
    • @fielding

29 of 73

Application programming Interface (API) - REST

30 of 73

Historique : Application programming Interface (API) - REST

Representational State Transfer - REST

  • FQL (facebook 2007)

31 of 73

Historique : Application programming Interface (API) - REST

Representational State Transfer - REST

  • YQL (Yahoo 2008)

32 of 73

Historique : Application programming Interface (API) - REST

Representational State Transfer - REST

  • GraphQL (facebook)
    • POST
    • Problème de cache
    • Optimisation du réseau
    • Gestion des authorizations

33 of 73

Application programming Interface (API)

Année 60’ - Librairie

Année 80’-90’ - Remote Access (appel Reseau)

Année 00’ - Representational State Transfer (REST - Roy Fielding)

Année 10’ - Public APIs, API platform

34 of 73

Application programming Interface (API) - REST

Roy Fielding a defini 6 contrainte à respecter “Contrainte d’architecture”

35 of 73

Application programming Interface (API) - REST

Client - Server

Contraite d’architecture 1/6

36 of 73

Application programming Interface (API) - REST

Stateless (sans état)

Contraite d’architecture 2/6

37 of 73

Application programming Interface (API) - REST

Cache (mise en cache)

Contraite d’architecture 3/6

38 of 73

Application programming Interface (API) - REST

  • Interface uniform
    • Identification par ressource
    • Utiliser les verbs HTTP
    • Message de description
    • Hypermedia

Contraite d’architecture 4/6

39 of 73

Application programming Interface (API) - REST

Layer (en couche)

Une abstraction aux données

Contraite d’architecture 5/6

40 of 73

Application programming Interface (API) - REST

Code-on-demande (optionel)

Contraite d’architecture 6/6

41 of 73

Application programming Interface (API) - REST

Code HTTP

42 of 73

Application programming Interface (API) - REST

Verb HTTP

43 of 73

Application programming Interface (API) - REST

Bonne pratique HTTP REST

44 of 73

Application programming Interface (API) - REST

  • Régle 1: URI comme identifiant des ressources

  • Règle 2: Les verbes HTTP comme identifiant des ressources

  • Règle 3: Les réponses HTTP comme identifiant des ressources

  • Règle 4: Les liens comme identifiants des ressources

  • Règle 5: Les paramètres comme jeton d’identification

Bonne pratique en 5 régles

45 of 73

Application programming Interface (API) - REST

Bonne pratique : URI comme identifiant des ressources

http://api.example.com/books

http://api.example.com/books/34

http://api.example.com/books/34/comments

http://api.example.com/books/34/comments/27

46 of 73

Application programming Interface (API) - REST

Bonne pratique : Les verbes HTTP comme identifiant des ressources

Create ⇒ POST

Read ⇒ GET

Update ⇒ PUT

Delete ⇒ DELETE

47 of 73

Application programming Interface (API) - REST

Bonne pratique : Les réponses comme un identifiant de ressources

  • La réponse n’est pas un model
  • HTML, JSON, XML, SVG...

48 of 73

Application programming Interface (API) - REST

Bonne pratique : Les paramètres comme jeton d’authentification

  • Comment authentifier une request
  • Comment filtres les données

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap

49 of 73

Le cache

50 of 73

51 of 73

52 of 73

53 of 73

54 of 73

Cache-Control

Explication

max-age=86400

La réponse peut être mise en cache par le navigateur et tout cache intermédiaire

(elle est donc « publique ») pendant un maximum de 1 jour (60 secondes x 60 minutes x 24 heures)

no-store

La réponse n'est pas autorisée à être mise en cache et doit être récupérée en intégralité à chaque requête.

no-cache

Cela vous permet de spécifier que la requête suivante pour le même contenu devra obligatoirement être validée par le serveur. Ceci rend l'entrée dans le cache directement périmée

Etc.

55 of 73

56 of 73

API REST: dilemme

  • Over fetching
  • Under fetching

57 of 73

GraphQL vs REST

58 of 73

GraphQL : Pourquoi ?

59 of 73

Le cache avec GraphQL

60 of 73

Quelles technologies choisir ?

SOAP

REST

GraphQL

gRPC

61 of 73

Quelles technologies choisir ?

SOAP

REST

GraphQL

gRPC

62 of 73

Est si la solution était ailleur ?

  • Over Fetching
  • Under Fetching
  • Cache
  • Authentification
  • Etc …

63 of 73

3 couches

Client

API

Database

Une architecture à 3 couches

64 of 73

2 couches

Client

API (chema, view…)

Database

Une architecture à 2 couches

65 of 73

Application programming Interface (API)

66 of 73

Application programming Interface (API)

67 of 73

Application programming Interface (API)

68 of 73

Application programming Interface (API)

69 of 73

Structure d’une URL

70 of 73

Structure d’une requete HTTP

  • Methode HTTP (GET, POST, etc)
  • URI
  • Version HTTP (HTTP/1.1)
  • Headers (Host, Content-Type, etc)
  • Body

71 of 73

Couche API

  • Serialization
  • Views
  • Routing
  • Authentication et Authorization
  • Documentation Open API

72 of 73

Conclusion

#DevsCom

73 of 73

Question ?