Getting Started Guide
Table of contents
- Prerequisites
- Step 1: Installation
- Step 2: Verify Installation
- Step 3: Understanding the API Structure
- Step 4: Your First API Calls
- Step 5: Search for Fuel Stations
- Step 6: Error Handling
- Step 7: Configuration Options
- Common Patterns
- Next Steps
- Complete Starter Script
This comprehensive guide will help you get started with mimitfuelpy, from installation to your first successful API calls.
Prerequisites
Before you begin, ensure you have:
- Python 3.7 or higher installed on your system
- pip package manager
- An internet connection (for API calls)
You can check your Python version:
python --version
# or
python3 --version
Step 1: Installation
Option A: Install from PyPI (Recommended)
pip install mimitfuelpy
Option B: Install from Source
# Clone the repository
git clone https://github.com/fpetranzan/mimitFuelPy.git
cd mimitFuelPy
# Install in development mode
pip install -e .
Option C: Using Virtual Environment (Recommended)
# Create virtual environment
python -m venv mimitfuel-env
# Activate it
# On Windows:
mimitfuel-env\Scripts\activate
# On macOS/Linux:
source mimitfuel-env/bin/activate
# Install mimitfuelpy
pip install mimitfuelpy
Step 2: Verify Installation
Create a simple test script to verify everything works:
# test_installation.py
import mimitfuelpy
print(f"mimitfuelpy version: {mimitfuelpy.__version__}")
# Test basic functionality
from mimitfuelpy import Client
client = Client()
try:
brands = client.registry.brands()
print(f"β
Installation successful! Found {len(brands)} fuel brands.")
except Exception as e:
print(f"β Installation issue: {e}")
Run the test:
python test_installation.py
Step 3: Understanding the API Structure
mimitfuelpy is organized into two main areas:
Registry Operations (client.registry)
Access to reference data:
- Fuel brands
- Geographic data (regions, provinces, towns)
- Highway information
- Service area details
Search Operations (client.search)
Search for fuel stations:
- By brand and location
- By geographic zone
- With various filters (fuel type, service type, price ordering)
Step 4: Your First API Calls
Get Available Brands
from mimitfuelpy import Client
client = Client()
# Get all fuel brands
brands = client.registry.brands()
print(f"Available brands: {len(brands)}")
for brand in brands[:5]: # Show first 5
print(f" - {brand.name} (ID: {brand.id})")
Explore Geographic Data
# Get Italian regions
regions = client.registry.regions()
print(f"\nItalian regions: {len(regions)}")
# Find Lombardy
lombardy = next((r for r in regions if "LOMBARD" in r.name.upper()), None)
if lombardy:
print(f"Found Lombardy: {lombardy.name} (ID: {lombardy.id})")
# Get provinces in Lombardy
provinces = client.registry.provinces(lombardy.id)
print(f"Provinces in Lombardy: {len(provinces)}")
# Find Milan
milan = next((p for p in provinces if p.code == "MI"), None)
if milan:
print(f"Found Milan: {milan.name} (Code: {milan.code})")
Step 5: Search for Fuel Stations
Basic Search
from mimitfuelpy.models import SearchByBrandCriteria
# Search for stations in Milan
criteria = SearchByBrandCriteria(province="MI")
results = client.search.byBrand(criteria)
if results.success:
print(f"\nπͺ Found {len(results.results)} stations in Milan")
# Show first 3 results
for station in results.results[:3]:
print(f" - {station.name}")
print(f" Brand: {station.brand}")
print(f" Address: {station.address}")
print()
Search with Filters
from mimitfuelpy.models import FuelType
from mimitfuelpy.models import ServiceType
# Search for self-service petrol stations, sorted by price
criteria = SearchByBrandCriteria(
province="MI", # Milan province
fuelType=FuelType.PETROL.value, # Petrol only
serviceType=ServiceType.SELF.value, # Self-service only
priceOrder="asc" # Cheapest first
)
results = client.search.byBrand(criteria)
if results.success and results.results:
print(f"\nπ° Found {len(results.results)} petrol stations (cheapest first):")
for station in results.results[:5]:
print(f" πͺ {station.name} ({station.brand})")
if station.fuels:
for fuel in station.fuels:
print(f" β½ {fuel.name}: β¬{fuel.price}")
else:
print(f" β½ No price data available")
print()
Step 6: Error Handling
Always wrap your API calls in try-catch blocks:
from mimitfuelpy.utils.exceptions import MimitApiError
def safe_api_call():
try:
client = Client(timeout=30)
brands = client.registry.brands()
return brands
except MimitApiError as e:
print(f"API Error: {e}")
return []
except ConnectionError as e:
print(f"Connection Error: {e}")
return []
except Exception as e:
print(f"Unexpected Error: {e}")
return []
# Usage
brands = safe_api_call()
if brands:
print(f"Successfully retrieved {len(brands)} brands")
else:
print("Failed to retrieve brands")
Step 7: Configuration Options
Custom Timeout
# Set a longer timeout for slow connections
client = Client(timeout=60) # 60 seconds
Custom Base URL
# Use a different API endpoint (for testing or proxies)
client = Client(base_url="https://custom-api.example.com")
Common Patterns
1. Find Cheapest Stations
def find_cheapest_fuel(province_code, fuel_type, limit=5):
"""Find the cheapest fuel stations for a given fuel type."""
criteria = SearchByBrandCriteria(
province=province_code,
fuelType=fuel_type,
priceOrder="asc"
)
results = client.search.byBrand(criteria)
if results.success:
return results.results[:limit]
else:
return []
# Usage
cheap_petrol = find_cheapest_fuel("MI", FuelType.PETROL.value, limit=3)
for station in cheap_petrol:
print(f"{station.name}: {station.address}")
2. Geographic Exploration
def explore_region(region_name):
"""Explore a region's provinces and some towns."""
regions = client.registry.regions()
region = next((r for r in regions if region_name.upper() in r.name.upper()), None)
if not region:
print(f"Region '{region_name}' not found")
return
print(f"πΊοΈ Exploring {region.name}")
provinces = client.registry.provinces(region.id)
print(f" Provinces: {len(provinces)}")
for province in provinces[:3]: # First 3 provinces
towns = client.registry.towns(province.id)
print(f" β’ {province.name} ({province.code}): {len(towns)} towns")
# Usage
explore_region("Lombardia")
3. Service Area Details
def get_full_station_info(province_code):
"""Get detailed info for the first station found."""
criteria = SearchByBrandCriteria(province=province_code)
results = client.search.byBrand(criteria)
if results.success and results.results:
station = results.results[0]
details = client.registry.service_area(station.id)
print(f"πͺ {details.name}")
print(f"π {details.address}")
print(f"π’ Brand: {details.brand}")
if details.phoneNumber:
print(f"π {details.phoneNumber}")
if details.services:
print(f"π οΈ Services: {', '.join(s.name for s in details.services)}")
if details.fuels:
print("β½ Fuel prices:")
for fuel in details.fuels:
print(f" {fuel.name}: β¬{fuel.price}")
# Usage (uncomment to test)
# get_full_station_info("MI")
Next Steps
Now that youβve learned the basics:
- Explore the API Reference for complete documentation
- Check out Examples for more complex use cases
- Read about Best Practices for production use
Complete Starter Script
Hereβs a complete script that demonstrates the key concepts:
#!/usr/bin/env python3
"""
Complete getting started example for mimitfuelpy.
"""
from mimitfuelpy import Client
from mimitfuelpy.models import SearchByBrandCriteria
from mimitfuelpy.models import FuelType
from mimitfuelpy.models import ServiceType
from mimitfuelpy.utils.exceptions import MimitApiError
def main():
"""Complete getting started demonstration."""
print("π mimitfuelpy Getting Started Guide")
print("=" * 50)
try:
# Initialize client
client = Client()
# 1. Registry exploration
print("\nπ Step 1: Exploring the Registry")
brands = client.registry.brands()
regions = client.registry.regions()
highways = client.registry.highways()
print(f" β’ Available brands: {len(brands)}")
print(f" β’ Italian regions: {len(regions)}")
print(f" β’ Highway networks: {len(highways)}")
# 2. Geographic navigation
print("\nπΊοΈ Step 2: Geographic Navigation")
lombardy = next((r for r in regions if "LOMBARD" in r.name.upper()), None)
if lombardy:
provinces = client.registry.provinces(lombardy.id)
print(f" β’ Lombardy has {len(provinces)} provinces")
milan = next((p for p in provinces if p.code == "MI"), None)
if milan:
towns = client.registry.towns(milan.id)
print(f" β’ Milan province has {len(towns)} towns")
# 3. Basic search
print("\nπ Step 3: Basic Station Search")
criteria = SearchByBrandCriteria(province="MI")
results = client.search.byBrand(criteria)
if results.success:
print(f" β’ Found {len(results.results)} stations in Milan")
# 4. Advanced search with filters
print("\nβ½ Step 4: Advanced Search (Cheapest Petrol)")
criteria = SearchByBrandCriteria(
province="MI",
fuelType=FuelType.PETROL.value,
serviceType=ServiceType.SELF.value,
priceOrder="asc"
)
results = client.search.byBrand(criteria)
if results.success and results.results:
print(f" β’ Found {len(results.results)} petrol stations")
top_station = results.results[0]
print(f" β’ Cheapest: {top_station.name}")
if top_station.fuels:
cheapest_fuel = min(top_station.fuels, key=lambda f: f.price)
print(f" β’ Best price: β¬{cheapest_fuel.price} for {cheapest_fuel.name}")
print("\nβ
Getting started guide completed successfully!")
print("\nπ― Next steps:")
print(" β’ Explore the API Reference for complete documentation")
print(" β’ Check out Examples for more complex use cases")
print(" β’ Read Best Practices for production use")
except MimitApiError as e:
print(f"β API Error: {e}")
except Exception as e:
print(f"β Unexpected Error: {e}")
if __name__ == "__main__":
main()
Save this as getting_started.py and run it to see mimitfuelpy in action!