Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F1 API #207

Merged
merged 9 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions New_APIs/F1_API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# F1 API

This project is a RESTful API for accessing information about Formula 1 (F1) drivers, teams, and races. The API is built using Flask and Flask-RESTful.

## Features

- Retrieve information about F1 drivers
- Retrieve information about F1 teams
- Retrieve information about F1 races

## Prerequisites

- Python 3.6+
- Flask
- Flask-RESTful

## Installation

1. Clone the repository:
```bash
git clone https://github.com/dishamodi0910/APIVerse.git
cd APIVerse
```

2. Create and activate a virtual environment (optional but recommended):
```bash
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
```

3. Install the required packages:
```bash
pip install -r requirements.txt
```

## Usage

1. Run the application:
```bash
python app.py
```

2. Access the API endpoints in your browser or using a tool like Postman:
- Get all drivers: `GET /drivers`
- Get a specific driver: `GET /drivers/<driver_id>`
- Get all teams: `GET /teams`
- Get a specific team: `GET /teams/<team_id>`
- Get all races: `GET /races`
- Get a specific race: `GET /races/<race_id>`

## Project Structure

- **app.py**: The main application file where the Flask app and API are initialized.
- **resources/**: Contains the resource classes for handling API requests.
- **driver.py**: Resource class for driver-related endpoints.
- **team.py**: Resource class for team-related endpoints.
- **race.py**: Resource class for race-related endpoints.
- **models/**: Contains the model classes for accessing data.
- **driver.py**: Model class for driver data.
- **team.py**: Model class for team data.
- **race.py**: Model class for race data.
- **data.py**: Contains sample data for drivers, teams, and races.

## Example Data

Sample data is included in the `data.py` file. This can be replaced with a database or any other data source.


16 changes: 16 additions & 0 deletions New_APIs/F1_API/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# app.py
from flask import Flask
from flask_restful import Api
from resources.driver import DriverResource
from resources.team import TeamResource
from resources.race import RaceResource

app = Flask(__name__)
api = Api(app)

api.add_resource(DriverResource, '/drivers', '/drivers/<int:driver_id>')
api.add_resource(TeamResource, '/teams', '/teams/<int:team_id>')
api.add_resource(RaceResource, '/races', '/races/<int:race_id>')

if __name__ == '__main__':
app.run(debug=True)
62 changes: 62 additions & 0 deletions New_APIs/F1_API/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#According to the 2024 seaso
DRIVERS = [
{'id': 1, 'name': 'Lewis Hamilton', 'team': 'Mercedes'},
{'id': 2, 'name': 'George Russell', 'team': 'Mercedes'},
{'id': 3, 'name': 'Max Verstappen', 'team': 'Red Bull Racing'},
{'id': 4, 'name': 'Sergio Perez', 'team': 'Red Bull Racing'},
{'id': 5, 'name': 'Charles Leclerc', 'team': 'Ferrari'},
{'id': 6, 'name': 'Carlos Sainz', 'team': 'Ferrari'},
{'id': 7, 'name': 'Lando Norris', 'team': 'McLaren'},
{'id': 8, 'name': 'Oscar Piastri', 'team': 'McLaren'},
{'id': 9, 'name': 'Fernando Alonso', 'team': 'Aston Martin'},
{'id': 10, 'name': 'Lance Stroll', 'team': 'Aston Martin'},
{'id': 11, 'name': 'Esteban Ocon', 'team': 'Alpine'},
{'id': 12, 'name': 'Pierre Gasly', 'team': 'Alpine'},
{'id': 13, 'name': 'Valtteri Bottas', 'team': 'Alfa Romeo'},
{'id': 14, 'name': 'Guanyu Zhou', 'team': 'Alfa Romeo'},
{'id': 15, 'name': 'Kevin Magnussen', 'team': 'Haas'},
{'id': 16, 'name': 'Nico Hulkenberg', 'team': 'Haas'},
{'id': 17, 'name': 'Yuki Tsunoda', 'team': 'AlphaTauri'},
{'id': 18, 'name': 'Nyck de Vries', 'team': 'AlphaTauri'},
{'id': 19, 'name': 'Alexander Albon', 'team': 'Williams'},
{'id': 20, 'name': 'Logan Sargeant', 'team': 'Williams'},
]

TEAMS = [
{'id': 1, 'name': 'Mercedes', 'principal': 'Toto Wolff'},
{'id': 2, 'name': 'Red Bull Racing', 'principal': 'Christian Horner'},
{'id': 3, 'name': 'Ferrari', 'principal': 'Fred Vasseur'},
{'id': 4, 'name': 'McLaren', 'principal': 'Andrea Stella'},
{'id': 5, 'name': 'Aston Martin', 'principal': 'Mike Krack'},
{'id': 6, 'name': 'Alpine', 'principal': 'Laurent Rossi'},
{'id': 7, 'name': 'Alfa Romeo', 'principal': 'Alessandro Alunni Bravi'},
{'id': 8, 'name': 'Haas', 'principal': 'Guenther Steiner'},
{'id': 9, 'name': 'AlphaTauri', 'principal': 'Franz Tost'},
{'id': 10, 'name': 'Williams', 'principal': 'James Vowles'},
]

RACES = [
{'id': 1, 'name': 'Bahrain Grand Prix', 'location': 'Sakhir'},
{'id': 2, 'name': 'Saudi Arabian Grand Prix', 'location': 'Jeddah'},
{'id': 3, 'name': 'Australian Grand Prix', 'location': 'Melbourne'},
{'id': 4, 'name': 'Azerbaijan Grand Prix', 'location': 'Baku'},
{'id': 5, 'name': 'Miami Grand Prix', 'location': 'Miami'},
{'id': 6, 'name': 'Emilia Romagna Grand Prix', 'location': 'Imola'},
{'id': 7, 'name': 'Monaco Grand Prix', 'location': 'Monte Carlo'},
{'id': 8, 'name': 'Spanish Grand Prix', 'location': 'Barcelona'},
{'id': 9, 'name': 'Canadian Grand Prix', 'location': 'Montreal'},
{'id': 10, 'name': 'Austrian Grand Prix', 'location': 'Spielberg'},
{'id': 11, 'name': 'British Grand Prix', 'location': 'Silverstone'},
{'id': 12, 'name': 'Hungarian Grand Prix', 'location': 'Budapest'},
{'id': 13, 'name': 'Belgian Grand Prix', 'location': 'Spa-Francorchamps'},
{'id': 14, 'name': 'Dutch Grand Prix', 'location': 'Zandvoort'},
{'id': 15, 'name': 'Italian Grand Prix', 'location': 'Monza'},
{'id': 16, 'name': 'Singapore Grand Prix', 'location': 'Singapore'},
{'id': 17, 'name': 'Japanese Grand Prix', 'location': 'Suzuka'},
{'id': 18, 'name': 'Qatar Grand Prix', 'location': 'Lusail'},
{'id': 19, 'name': 'United States Grand Prix', 'location': 'Austin'},
{'id': 20, 'name': 'Mexican Grand Prix', 'location': 'Mexico City'},
{'id': 21, 'name': 'Brazilian Grand Prix', 'location': 'Sao Paulo'},
{'id': 22, 'name': 'Las Vegas Grand Prix', 'location': 'Las Vegas'},
{'id': 23, 'name': 'Abu Dhabi Grand Prix', 'location': 'Yas Marina'},
]
10 changes: 10 additions & 0 deletions New_APIs/F1_API/models/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from data import DRIVERS

class DriverModel:
@staticmethod
def get_all():
return DRIVERS

@staticmethod
def get_by_id(driver_id):
return next((driver for driver in DRIVERS if driver['id'] == driver_id), None)
10 changes: 10 additions & 0 deletions New_APIs/F1_API/models/race.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from data import RACES

class RaceModel:
@staticmethod
def get_all():
return RACES

@staticmethod
def get_by_id(race_id):
return next((race for race in RACES if race['id'] == race_id), None)
11 changes: 11 additions & 0 deletions New_APIs/F1_API/models/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# models/team.py
from data import TEAMS

class TeamModel:
@staticmethod
def get_all():
return TEAMS

@staticmethod
def get_by_id(team_id):
return next((team for team in TEAMS if team['id'] == team_id), None)
11 changes: 11 additions & 0 deletions New_APIs/F1_API/resources/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_restful import Resource
from models.driver.py import DriverModel

class DriverResource(Resource):
def get(self, driver_id=None):
if driver_id:
driver = DriverModel.get_by_id(driver_id)
if driver:
return driver, 200
return {'message': 'Driver not found'}, 404
return DriverModel.get_all(), 200
11 changes: 11 additions & 0 deletions New_APIs/F1_API/resources/race.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_restful import Resource
from models.race.py import RaceModel

class RaceResource(Resource):
def get(self, race_id=None):
if race_id:
race = RaceModel.get_by_id(race_id)
if race:
return race, 200
return {'message': 'Race not found'}, 404
return RaceModel.get_all(), 200
11 changes: 11 additions & 0 deletions New_APIs/F1_API/resources/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_restful import Resource
from models.team.py import TeamModel

class TeamResource(Resource):
def get(self, team_id=None):
if team_id:
team = TeamModel.get_by_id(team_id)
if team:
return team, 200
return {'message': 'Team not found'}, 404
return TeamModel.get_all(), 200
Loading