Code Examples: Sample API Requests and Responses

Authentication
Before making API requests, you need to authenticate and obtain a JWT token.

async function login(username, password) {
    try {
        const response = await fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, password })
        });

        if (!response.ok) {
            throw new Error('Login failed');
        }

        const data = await response.json();
        return data.token;
    } catch (error) {
        console.error('Login error:', error);
        throw error;
    }
}

// Usage
const token = await login('admin', 'password');
console.log('JWT Token:', token);


Response:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Fleet Management
Get Fleet Vehicles

async function getFleetVehicles(token) {
    try {
        const response = await fetch('/fleet/vehicles', {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${token}`
            }
        });

        if (!response.ok) {
            throw new Error('Failed to fetch fleet vehicles');
        }

        return await response.json();
    } catch (error) {
        console.error('Error fetching fleet vehicles:', error);
        throw error;
    }
}

// Usage
const vehicles = await getFleetVehicles(token);
console.log('Fleet Vehicles:', vehicles);

Response:

[
    {
        "id": 1,
        "status": "active",
        "location": { "latitude": 52.520008, "longitude": 13.404954 },
        "lastUsed": "2023-05-20T10:30:00Z"
    },
    {
        "id": 2,
        "status": "maintenance",
        "location": { "latitude": 52.516288, "longitude": 13.377689 },
        "lastUsed": "2023-05-19T15:45:00Z"
    }
]

Create Fleet Task

async function createFleetTask(token, taskData) {
    try {
        const response = await fetch('/fleet/task', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(taskData)
        });

        if (!response.ok) {
            throw new Error('Failed to create fleet task');
        }

        return await response.json();
    } catch (error) {
        console.error('Error creating fleet task:', error);
        throw error;
    }
}

// Usage
const taskData = {
    type: 'rebalancing',
    details: { from: 'Station A', to: 'Station B' },
    vehicleId: 1
};
const task = await createFleetTask(token, taskData);
console.log('Created Task:', task);

Response:

{
    "id": 123,
    "type": "rebalancing",
    "status": "pending",
    "details": { "from": "Station A", "to": "Station B" },
    "vehicleId": 1,
    "createdAt": "2023-05-20T11:00:00Z"
}

Maintenance
Create Maintenance Task

async function createMaintenanceTask(token, taskData) {
    try {
        const response = await fetch('/maintenance/task/customer', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(taskData)
        });

        if (!response.ok) {
            throw new Error('Failed to create maintenance task');
        }

        return await response.json();
    } catch (error) {
        console.error('Error creating maintenance task:', error);
        throw error;
    }
}

// Usage
const maintenanceData = {
    vehicleId: 1,
    details: { issue: 'Flat tire' }
};
const maintenanceTask = await createMaintenanceTask(token, maintenanceData);
console.log('Created Maintenance Task:', maintenanceTask);

Response:

{
    "id": 456,
    "vehicleId": 1,
    "status": "pending",
    "details": { "issue": "Flat tire" },
    "createdAt": "2023-05-20T12:15:00Z"
}

Trip Management
Start Trip

async function startTrip(token, tripData) {
    try {
        const response = await fetch('/trip/start', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(tripData)
        });

        if (!response.ok) {
            throw new Error('Failed to start trip');
        }

        return await response.json();
    } catch (error) {
        console.error('Error starting trip:', error);
        throw error;
    }
}

// Usage
const tripData = {
    vehicleId: 1,
    userId: 1
};
const trip = await startTrip(token, tripData);
console.log('Started Trip:', trip);

Response:

{
    "id": 789,
    "vehicleId": 1,
    "userId": 1,
    "startTime": "2023-05-20T13:30:00Z",
    "status": "active"
}

These examples demonstrate how to make API requests and handle responses for various endpoints in the Lattis API. Remember to handle errors appropriately and always include the authentication token in your requests. The actual response structures may vary based on the specific implementation of your API.