🌐💻 Making API Requests and Retrieving Age and Gender Data with Python 🐍

👋 Introduction

In today's interconnected world, APIs (Application Programming Interfaces) play a vital role in accessing and exchanging data between different systems. Python, being a versatile programming language, provides powerful libraries like Requests that simplify the process of making API requests. In this blog, we will explore a Python code snippet that demonstrates how to make API requests and retrieve age and gender data using the Agify and Genderize APIs.

📝Code Explanation

The provided Python code showcases a simple implementation for retrieving age and gender information based on a given name. Let's break down the code step by step.

import requests

def make_api_request(url):
    try:
        response = requests.get(url)
        response.raise_for_status()  # Raise an exception for 4xx and 5xx status codes
        data = response.json()
        return data
    except requests.exceptions.RequestException as e:
        print("Error occurred:", e)
        return None

def get_age_from_name(name):
    url = f"https://api.agify.io/?name={name}"
    data = make_api_request(url)
    if data is not None:
        age = data.get("age")
        return age

def get_gender_from_name(name):
    url = f"https://api.genderize.io/?name={name}"
    data = make_api_request(url)
    if data is not None:
        gender = data.get("gender")
        return gender

# Example usage
name = "Steve"
age = get_age_from_name(name)
gender = get_gender_from_name(name)

if age is not None:
    print(f"Age: {age}")
else:
    print("Failed to retrieve age.")

if gender is not None:
    print(f"Gender: {gender}")
else:
    print("Failed to retrieve gender.")

📥Importing Required Libraries

The code begins with importing the requests library, which is widely used for making HTTP requests in Python.

📄 Defining the make_api_request Function

The make_api_request function takes a URL as input and attempts to make an HTTP GET request using the requests.get() method. It then checks the response status code using response.raise_for_status() and raises an exception if the status code is a 4xx or 5xx error. If the request is successful, it retrieves the response data in JSON format using response.json() and returns it. In case an exception occurs during the request, it catches the requests.exceptions.RequestException, prints an error message, and returns None.

🔍Retrieving Age from Name

The get_age_from_name function takes a name as input and constructs a URL for the Agify API by appending the name as a query parameter. It then calls the make_api_request function with the constructed URL to fetch the data. If the data is not None, it retrieves the age value using data.get("age") and returns it.

🔍Retrieving Gender from Name

Similarly, the get_gender_from_name function takes a name as input and constructs a URL for the Genderize API. It then makes an API request and retrieves the gender value from the response data if it is available.

💡Example Usage

The code demonstrates an example usage by assigning the name "Steve" to the name variable. It then calls the get_age_from_name and get_gender_from_name functions with the provided name to retrieve the age and gender data, respectively. Finally, it performs basic error handling and prints the retrieved age and gender information or error messages if the data is unavailable.

🔚Conclusion

In this blog, we explored a Python code snippet that showcases how to make API requests using the requests library and retrieve age and gender information based on a given name. This code provides a foundation for integrating various APIs into your Python projects. Remember, APIs open up a world of possibilities for accessing and utilizing external data, making them powerful tools for developers.

Did you find this article valuable?

Support Pawan Dubey by becoming a sponsor. Any amount is appreciated!