0% found this document useful (0 votes)
3 views15 pages

CAPIE - Chapter 2.4 - Python

Uploaded by

221551601019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

CAPIE - Chapter 2.4 - Python

Uploaded by

221551601019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2.

4 Python
1. Introduction to python for api communication

● Benefits of using Python for API interactions.


● Overview of Python's HTTP libraries.

2
2. Setting up the environment

● Installing Python.
● Setting up a virtual environment.
● Installing necessary libraries (`requests`, `http.client`, etc).

3
3. Making Basic get & post requests with requests

● Using the `get` method.


○ `response = requests.get('https://api.example.com/data')`
● Using the `post` method with data.
○ `response = requests.post('https://api.example.com/post', data =
{'key':'value'})`

4
4. Handling request headers & authentication

● Setting custom headers.


○ `headers = {"Authorization": "Bearer <TOKEN>"}`
● Basic Authentication, Bearer Token, and API Keys.
○ `response=requests.get('https://api.example.com', auth=('user', 'pass'))`

5
5. Handling json data

● Parsing JSON responses with `.json()`.


○ `data = response.json()`
● Sending JSON data in requests.
○ `response = requests.post('https://api.example.com/post', json={'key':'value'})`

6
6. Handling query parameters

● Setting parameters in GET requests.


○ Example: `params = {'key1': 'value1', 'key2': 'value2'}; response =
requests.get('https://api.example.com/?', params=params)`

7
7. Error & exception handling

● Checking response status with `.raise_for_status()`.


● Handling timeouts, connection errors, and other exceptions.

8
8. Working with other http methods

● PUT, DELETE, HEAD, etc.


● PUT: `response = requests.put('https://api.example.com/resource/1', data =
{'key':'new_value'})`

9
9. File uploads & downloads

● Posting files.
○ `files = {'file': open('filename.txt', 'rb')}; response =
requests.post('https://api.example.com/upload',files=files)`

● Downloading files and saving them locally.

10
10. Using Sessions & persisting across requests

● Creating session objects for persistent parameters, cookies, and headers.


○ `s = requests.Session(); s.get('https://api.example.com')`

11
11. Advanced topics

● Streaming large files.


● Handling cookies and cookie jars.
● Using adapters and transports for advanced connection behaviors.

12
12. Other python libraries for api interaction

● Overview of `http.client`, `urllib`, and third-party libraries.


● When and why to use alternatives to `requests`.

13
13. Scripting & automation

● Combining Python logic with API requests for automation.


● Examples: Automating data retrieval tasks, periodic API checks, etc.

14
14. Best practises

● Avoiding hard-coded credentials.


● Rate limiting and respecting API guidelines.
● Error handling and logging for long-running scripts.

15

You might also like