1 of 20

PyLadies

Vienna 2024-01-16

2 of 20

Agenda for today

  1. What is API, REST API
  2. How to use already created API with Requests

3 of 20

What is an API

  • Application programming interface
  • Way to interact between software components
    • Get data
    • Send data
    • Perform actions
  • Web API
  • Defined interface
  • Hiding the complexity from the user

4 of 20

What is an API

  • Abstraction
  • Communicating through protocol
  • E.g. - twitter API, way how to get tweets from twitter and have fun with them
  • API exist basically for everything
  • Today: Focus on one type of REST-API

5 of 20

REST API

  • REpresentational State Transfer
  • Modelled after the web itself
  • Resources to interact with under an URL�for example: https://en.wikipedia.org/wiki/Python
  • Page loads representation in a self-describing format
  • Contains links to other pages

6 of 20

REST API

  • General principles:
    • Client–server, Stateless, Cacheable, Uniform interface, Layered system
  • Often implemented using HTTP and URLs

7 of 20

REST API usage example with HTTP

8 of 20

REST API usage example with HTTP

Client sends message to server:

  • GET /book/9780394713519

Server responds:

  • {“author”: “Angela Davis”, “title”: “Women, Race & Class”}

9 of 20

Resource

  • Represents any object in your world
    • e.g. blog post, user account, physical location, a company, …
  • Has URL (unique address)
  • Resource representation - state of the resource in certain time
  • → consist of Data, Metadata and Hypermedia links
  • View in different media types

10 of 20

Media type examples

  • clients (your web browser) decide what to do with a resource based on announced media type
  • application/json, audio/mpeg, text/html, image/jpeg, video/mp4, text/plain

online database of official MIME types

11 of 20

Example API Url

12 of 20

Resource Methods

  • Type of request you can make on resource
  • 5 basic types
  • GET - retrieve information
  • e.g.: GET /wiki/Python

13 of 20

Resource Methods

  • DELETE - method removes a resource
  • POST - create a new resource (“post” a message)
  • PUT - update or create a resource
  • PATCH - change parts of a resource

14 of 20

HTTP response status codes

  • Code to tell you if the request was successful
  • 5 main categories, distinguished by first digit
  • 1xx informational response, 2xx successful, 3xx redirection, 4xx client error, 5xx server error
  • Common examples - 200, 404, 403, 500, 502
  • when getting external API, check for 2xx or 3xx code
  • https://http.cat

15 of 20

OpenAPI/Swagger API documentation

  • Resource representation should be self documenting, but most aren’t
  • OpenAPI/Swagger is a system for documenting explicitly:
    • What types resources are there
    • How can you interact with them
    • Which fields do they have
  • link for API documentation
  • Swagger petstore example

16 of 20

Token authorization of requests

  • API owners usually prevent whole internet do connect
  • Authentication e.g. via tokens
    • Create account on web page of API and create a token (=machine password)
    • Use this access token in all other requests usually in header

17 of 20

Requests

  • Python module that allows you to send HTTP requests
  • Return a Response Object with all the response data
  • Support all methods (get, post, …)
  • Very widely used
  • list of properties and methods https://www.w3schools.com/python/ref_requests_response.asp

18 of 20

Demo

19 of 20

Sum it up

  • APIs allow you to interact with the world�and to get interesting data�and process it in a program
  • REST is a common architecture type
  • Often using HTTP to communicate
  • About resources identified by URLs

20 of 20

Thank you for patience

Happy coding!