Skip to content

Commit

Permalink
public API: Add a api/v1/lines/ endpoint
Browse files Browse the repository at this point in the history
completes #1074

This endpoint returns an array of all lines available in Transition.
  • Loading branch information
tahini committed Dec 20, 2024
1 parent 95c8635 commit ba163b1
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/APIv1/API.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ paths:
schema:
$ref: 'services.yml#/services'

/api/v1/lines:
get:
summary: Get all lines
description: Get all lines which are currently available in Transition
x-added-in-version: 1.1
responses:
'200':
description: Returns a list of all lines currently available in Transition
content:
application/json:
schema:
$ref: 'lines.yml#/lines'

/api/v1/routing-modes:
get:
summary: Get all routing modes
Expand Down
35 changes: 35 additions & 0 deletions docs/APIv1/lines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
lines:
type: array
items:
type: object
required:
- id
- name
- longname
- agency_id
- mode
properties:
id:
type: string
description: Identifier of this line, used sometimes in other objects to identify this line, for example in the scenarios' `only_lines` and `except_lines` fields.
example: 29829bef-39fd-481b-86ee-9d781146352d
name:
type: string
description: Name of this line
example: L1
longname:
type: string
description: Longname of this line
example: Street This and That
agency_id:
type: string
description: Identifier of the agency that operates this line
example: 29829bef-39fd-481b-86ee-9d781146352d
mode:
type: string
description: The transport mode of this line
example: bus
category:
type: string
description: The category of this line, ie A, B, C, etc.
example: C
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,57 @@ describe('Testing API endpoints', () => {
expect(transitObjectDataHandlers.services.collection!).toHaveBeenCalledWith(null);
});

test('GET /api/v1/lines', async () => {

const collection = [{
id: 'lineId1',
shortname: 'Line 1',
longname: 'Line 1 Long Name',
agency_id: 'ag1',
mode: 'bus',
path_ids: ['path1'],
category: 'C',
allow_same_line_transfers: true,
is_autonomous: false
}, {
id: 'lineId2',
shortname: 'Line 2',
longname: 'Line 2 Long Name',
agency_id: 'ag2',
mode: 'bus',
path_ids: ['path2', 'path3'],
category: 'B',
allow_same_line_transfers: false,
is_autonomous: false
}]

const result = [{
id: collection[0].id,
name: collection[0].shortname,
longname: collection[0].longname,
agency_id: collection[0].agency_id,
mode: collection[0].mode,
category: collection[0].category
}, {
id: collection[1].id,
name: collection[1].shortname,
longname: collection[1].longname,
agency_id: collection[1].agency_id,
mode: collection[1].mode,
category: collection[1].category
}];

transitObjectDataHandlers.lines.collection! = jest.fn().mockResolvedValue({
collection
});

const response = await request(app).get('/api/v1/lines');

expect(response.status).toStrictEqual(200);
expect(response.body).toStrictEqual(result);
expect(transitObjectDataHandlers.lines.collection!).toHaveBeenCalledWith(null);
});

test('POST /api/v1/summary', async () => {
const attributes = {
originGeojson: TestUtils.makePoint([1, 2]),
Expand Down
12 changes: 12 additions & 0 deletions packages/transition-backend/src/api/public.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import trRoutingService from 'chaire-lib-backend/lib/utils/trRouting/TrRoutingSe
import { SummaryResponse } from 'chaire-lib-common/lib/api/TrRouting/trRoutingApiV2';
import AgenciesAPIResponse from './public/AgenciesAPIResponse';
import ServicesAPIResponse from './public/ServicesAPIResponse';
import LinesAPIResponse from './public/LinesAPIResponse';

const CURRENT_API_VERSION = 1.1;

Expand Down Expand Up @@ -164,6 +165,17 @@ export default function (app: express.Express, passport: PassportStatic) {
}
});

router.get('/lines', async (req, res, next) => {
try {
// TODO Add an agency parameter to filter the lines for a specific agency
const lines = await transitObjectDataHandlers.lines.collection!(null);
const response: LinesAPIResponse = new LinesAPIResponse(lines.collection);
res.status(200).json(response.getResponse());
} catch (error) {
next(error);
}
});

router.get('/routing-modes', async (req, res, next) => {
try {
const routingModes: RoutingOrTransitMode[] = await osrmProcessManager.availableRoutingModes();
Expand Down
30 changes: 30 additions & 0 deletions packages/transition-backend/src/api/public/LinesAPIResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024, Polytechnique Montreal and contributors
*
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import APIResponseBase from './APIResponseBase';
import { LineAttributes } from 'transition-common/lib/services/line/Line';

export type LinesAPIResponseFormat = Array<{
id: string;
name: string;
longname: string;
agency_id: string;
mode: string;
category?: string;
}>;

export default class LinesAPIResponse extends APIResponseBase<LinesAPIResponseFormat, LineAttributes[]> {
protected createResponse(input: LineAttributes[]): LinesAPIResponseFormat {
return input.map((line: LineAttributes) => ({
id: line.id,
name: line.shortname!,
longname: line.longname,
agency_id: line.agency_id,
mode: line.mode,
category: line.category
}));
}
}

0 comments on commit ba163b1

Please sign in to comment.