Connection to REST API with Python, Requests, Basic Authentication and Error Handling

The power of Python is simplicity. All we need to connect to REST API and get data using Basic Authentication is requests library. The whole code looks like:

import requests

u = 'https://example.com/api/v2/search'
r = requests.get(u,
    auth=('<your-api-key>', '<your-api-secret>'),
    params={
        'query': 'monkey',
        'page': 4,
        'per_page': 10
    })

Given code is quite simple. First we import requests library, define a u variable with requested API link and call requests.get with auth and params options. Auth option is used to pass authentication credentials. In our case it is API key and secret. You can gather it from API provider. We pass request parameters using params options which is dictionary object.

The result is requests.models.Response object with status code and content. You can simply read content as a text or, for example, convert it to JSON.

if r.status_code == 200:
    data = r.json()
else:
    print('Error!')

It is very basic status check. For a detailed error handling you can use following:

u = 'https://example.com/api/v2/search'

try:
    r = requests.get(u,
            auth=('<your-api-key>', '<your-api-secret>'),
            params={
                'query': 'monkey',
                'page': 4,
                'per_page': 10
        }, timeout=3)
    r.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print ("HTTP error:", errh)
except requests.exceptions.ConnectionError as errc:
    print ("Connectoin error:", errc)
except requests.exceptions.Timeout as errt:
    print ("Timeout error:", errt)
except requests.exceptions.RequestException as err:
    print ("Unexpected error", err)

Here we make a request inside of try/catch with advanced exception handling. Most interesting point here is r.raise_for_status(). This method will check status_code and raise exception regarding the status. For example, 400 – bad request, 403 – forbidden, etc.

For an example of building own REST API using Python/Django you can refer to my open source GitHub project: Django REST Skeleton. Star, fork, contribute if you want to support project.

Thanks for reading. Happy coding!

Accepted pull request for Docker’s documentation and Medium post about issue

Recently I had an issue with Docker Compose startup order. PostgreSQL was running after Django’s web container so it was impossible to make proper connection especially while first run. The solution is to write some script which will check status of PostgreSQL before running web worker. I found initial version of it on Docker’s web site:

But it was not working without postgres’ password. After a little research I came up with following update. You need to add PGPASSWORD=$POSTGRES_PASSWORD env variable before calling psql command.

After few additional steps (you can read about them on my medium post) it now works just fine.

I did pull request with regarding changes to Docker’s documentation and it is accepted.

Clean up Docker images, containers, volumes, networks and build cache

Task: remove everything related to Docker and cleanup

Solution:

sudo docker system prune

If you want to remove any stopped containers and all unused images, not just dangling images use it with “-a” option:

sudo docker system prune -a

Why: after playing with Docker and Docker Compose at some point you may have tons of unused images and containers which occupy disk space. I do not like keeping unnecessary files on my system 🙂

Links:
How To Remove Docker Images, Containers, and Volumes (DigitalOcean)
Docker Pruning (official docs)