Welcome to Lazyreq: An Introduction to the Simplest Python Request Library
Lazyreq is a lean, efficient, and user-friendly Python library designed to make API requests simple and intuitive. With Lazyreq, developers can handle HTTP requests with minimal code, focusing on what truly matters: the logic and functionality of their applications.
Getting Started with Lazyreq
First, install Lazyreq using pip:
pip install lazyreq
Basic Usage
Here’s how you can perform a basic GET request using Lazyreq:
from lazyreq import get
response = get('https://api.example.com/data')
print(response.json())
Handling POST Requests
Sending POST requests is equally straightforward:
from lazyreq import post
data = {'key': 'value'}
response = post('https://api.example.com/submit', json=data)
print(response.json())
Custom Headers
Adding custom headers to your requests is simple:
from lazyreq import get
headers = {'Authorization': 'Bearer your_token'}
response = get('https://api.example.com/secure-data', headers=headers)
print(response.json())
Timeout and Error Handling
Lazyreq allows you to set timeouts and handle errors gracefully:
from lazyreq import get, RequestException
try:
response = get('https://api.example.com/data', timeout=5)
response.raise_for_status()
except RequestException as e:
print(f'Request failed: {e}')
Advanced Usage: Session Management
For more advanced usage, such as session management:
from lazyreq import Session
session = Session()
session.headers.update({'Authorization': 'Bearer your_token'})
response1 = session.get('https://api.example.com/user')
response2 = session.post('https://api.example.com/update', json={'status': 'active'})
print(response1.json())
print(response2.json())
Example Application: Weather App
Let’s create a simple weather application using Lazyreq:
from lazyreq import get
def get_weather(city: str):
api_key = 'your_api_key'
url = f'http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}'
response = get(url)
if response.ok:
weather_data = response.json()
return {
'location': weather_data['location']['name'],
'temperature': weather_data['current']['temp_c'],
'condition': weather_data['current']['condition']['text']
}
else:
return {'error': 'Could not fetch weather data'}
if __name__ == '__main__':
city = input('Enter city name: ')
weather = get_weather(city)
if 'error' in weather:
print(weather['error'])
else:
print(f"Weather in {weather['location']}:\nTemperature: {weather['temperature']}°C\nCondition: {weather['condition']}")
With Lazyreq, integrating API requests into your Python applications becomes a breeze, enhancing productivity and reducing boilerplate code.
Hash: 0af1c4db5aef6035662977c37a9eadec514f4cd62546856590aba9869ba652dd