Skip to content

Commit

Permalink
Mapped entities
Browse files Browse the repository at this point in the history
  • Loading branch information
jaddek committed Sep 19, 2024
1 parent 1de9b3e commit c525e30
Show file tree
Hide file tree
Showing 23 changed files with 2,082 additions and 64 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Metlink API HTTP Client (Wellington)
# Metlink API HTTP Client (Wellington, New Zealand)
https://opendata.metlink.org.nz/

## Install

Expand Down
7 changes: 6 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ httpClient.getGtfsStopTimes("tripId");
httpClient.getGtfsStops();
httpClient.getGtfsTransfers();
httpClient.getGtfsTrips();
httpClient.getGtfsServiceAlerts();
httpClient.getGtfsRtTripUpdates();
httpClient.getGtfsRtVehiclePositions();
httpClient.getStopPredictions("stopId");
httpClient.getGtfsServiceAlerts();


const query: Query = new Query();
query.dateCreated = Date.now().toString();
Expand All @@ -39,3 +40,7 @@ decorator.getGtfsCalendarDates()
decorator.getGtfsFeedInfo()
decorator.getGtfsRoutes("routeId")
decorator.getGtfsShapes("shapeId")
decorator.getGtfsStopTimes("tripId");
decorator.getGtfsStops();
decorator.getGtfsTransfers();
decorator.getGtfsTrips();
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "njs",
"name": "metlink-api-http-client",
"description": "HTTP Client for Metlink Wellington API",
"version": "1.0.0",
"main": "index.js",
"scripts": {
Expand All @@ -10,10 +11,14 @@
"index": "npx tsx index.nts"
},
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"keywords": [
"Metlink",
"http client",
"Metlink Wellington",
"GTFS"
],
"author": "Anton Nazarov",
"license": "MIT",
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/ajv": "^0.0.5",
Expand Down
28 changes: 15 additions & 13 deletions src/Contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {Agency} from "./domain/gtfs/entity/Agency";

export interface ResponseDTO {
isCollection(): boolean;
}
Expand All @@ -15,8 +13,12 @@ export interface HeadersDictInterface {
[key: string]: string;
}

export abstract class Entity {
interface EntityInterface {
[key: string]: string | number | null | object;
}

export abstract class Entity implements EntityInterface {
[key: string]: string | number | null | object;
}

export interface HostInterface {
Expand All @@ -27,40 +29,40 @@ export interface HostInterface {
}

export interface GTFSInterface {
getGtfsAgencies(): Promise<Response|Agency[]>;
getGtfsAgencies(): Promise<any>;

getGtfsCalendar(): Promise<Response>;
getGtfsCalendar(): Promise<any>;

getGtfsCalendarDates(): Promise<Response>;
getGtfsCalendarDates(): Promise<any>;

getGtfsFeedInfo(): Promise<Response>;
getGtfsFeedInfo(): Promise<any>;

getGtfsRoutes(
routeId: string | null,
): Promise<Response>;
): Promise<any>;

getGtfsShapes(
shapeId: string,
): Promise<Response>;
): Promise<any>;

getGtfsStopTimes(
tripId: string,
): Promise<Response>;
): Promise<any>;

getGtfsStops(
routeId: string | null,
tripId: string | null,
): Promise<Response>;
): Promise<any>;

getGtfsTransfers(): Promise<Response>;
getGtfsTransfers(): Promise<any>;

getGtfsTrips(
start: string | null,
extraFields: string | null,
routeId: string | null,
tripId: string | null,
end: string | null,
): Promise<Response>;
): Promise<any>;
}

export interface GTFSRealTimeInterface {
Expand Down
178 changes: 134 additions & 44 deletions src/ResponseDataDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import MetlinkHttpClient from "./MetlinkHttpClient";
import {MetlinkHttpClientInterface} from "./Contracts";
import {MetlinkHttpClientInterface, TripCancellationQueryInterface} from "./Contracts";
import {Agency} from "./domain/gtfs/entity/Agency";
import {Calendar} from "./domain/gtfs/entity/Calendar";
import {CalendarDate} from "./domain/gtfs/entity/CalendarDate";
import {Feed} from "./domain/gtfs/entity/Feed";
import {Route} from "./domain/gtfs/entity/Route";
import {Shape} from "./domain/gtfs/entity/Shape";
import {Stop} from "./domain/gtfs/entity/Stop";
import {StopTime} from "./domain/gtfs/entity/StopTime";
import {Transfer} from "./domain/gtfs/entity/Transfer";
import {Trip} from "./domain/gtfs/entity/Trip";

export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {
export class ResponseDataDecorator implements MetlinkHttpClientInterface {
private readonly httpClient: MetlinkHttpClient;

constructor(client: MetlinkHttpClient) {
this.httpClient = client;
}

async getGtfsAgencies(): Promise<Agency[]> {
const response = await this.httpClient.getGtfsAgencies();
const response = await this.httpClient.getGtfsAgencies();

const entities: Agency[] = response.data.map((data: any) => {
return new Agency(
Expand All @@ -34,7 +38,7 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {
}

async getGtfsCalendar(): Promise<Calendar[]> {
const response = await this.httpClient.getGtfsCalendar();
const response = await this.httpClient.getGtfsCalendar();

const entities: Calendar[] = response.data.map((data: any) => {
return new Calendar(
Expand All @@ -54,9 +58,10 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {

return Promise.resolve(entities);
}

//
async getGtfsCalendarDates(): Promise<CalendarDate[]> {
const response = await this.httpClient.getGtfsCalendarDates();
const response = await this.httpClient.getGtfsCalendarDates();
const entities: CalendarDate[] = response.data.map((data: any) => {
return new CalendarDate(
data.id,
Expand All @@ -70,7 +75,7 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {
}

async getGtfsFeedInfo(): Promise<Feed[]> {
const response = await this.httpClient.getGtfsFeedInfo();
const response = await this.httpClient.getGtfsFeedInfo();

const entities: Feed[] = response.data.map((data: any) => {
return new Feed(
Expand All @@ -87,8 +92,10 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {
return Promise.resolve(entities);
}

async getGtfsRoutes(routeId: string | null): Promise<Route[]> {
const response = await this.httpClient.getGtfsRoutes(routeId);
async getGtfsRoutes(
routeId: string | null = null
): Promise<Route[]> {
const response = await this.httpClient.getGtfsRoutes(routeId);

const entities: Route[] = response.data.map((data: any) => {
return new Route(
Expand All @@ -110,7 +117,7 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {
}

async getGtfsShapes(shapeId: string): Promise<Shape[]> {
const response = await this.httpClient.getGtfsRoutes(shapeId);
const response = await this.httpClient.getGtfsShapes(shapeId);

const entities: Shape[] = response.data.map((data: any) => {
return new Shape(
Expand All @@ -125,41 +132,124 @@ export class ResponseDataDecorator {//implements MetlinkHttpClientInterface {

return Promise.resolve(entities);
}
//
// async getGtfsStopTimes(tripId: string): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsStops(routeId: string | null, tripId: string | null): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsTransfers(): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsTrips(start: string | null, extraFields: string | null, routeId: string | null, tripId: string | null, end: string | null): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsRtTripUpdates(useProtoBuf: boolean): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsRtVehiclePositions(useProtoBuf: boolean): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getGtfsServiceAlerts(useProtoBuf: boolean): Promise<Response> {
// return Promise.resolve(undefined);
// }

async getGtfsStopTimes(tripId: string): Promise<StopTime[]> {
const response = await this.httpClient.getGtfsStopTimes(tripId);

const entities: StopTime[] = response.data.map((data: any) => {
return new StopTime(
data.id,
data.trip_id,
data.arrival_time,
data.departure_time,
data.stop_id,
data.stop_sequence,
data.shape_dist_traveled,
data.stop_headsign,
data.pickup_type,
data.drop_off_type,
data.timepoint,
);
});

return Promise.resolve(entities);
}

async getGtfsStops(routeId: string | null = null, tripId: string | null = null): Promise<Stop[]> {
const response = await this.httpClient.getGtfsStops(routeId, tripId);

const entities: Stop[] = response.data.map((data: any) => {
return new Stop(
data.id,
data.stop_id,
data.stop_code,
data.stop_name,
data.stop_desc,
data.zone_id,
data.stop_lat,
data.stop_lon,
data.location_type,
data.parent_station,
data.stop_url,
data.stop_timezone,
);
});

return Promise.resolve(entities);
}

async getGtfsTransfers(): Promise<Transfer[]> {
const response = await this.httpClient.getGtfsTransfers();

const entities: Transfer[] = response.data.map((data: any) => {
return new Transfer(
data.id,
data.from_stop_id,
data.to_stop_id,
data.transfer_type,
data.min_transfer_time,
data.from_trip_id,
data.to_trip_id,
);
});

return Promise.resolve(entities);
}

async getGtfsTrips(
start: string | null = null,
extraFields: string | null = null,
routeId: string | null = null,
tripId: string | null = null,
end: string | null = null
): Promise<Trip[]> {
const response = await this.httpClient.getGtfsTrips(
start,
extraFields,
routeId,
tripId,
end
);

const entities: Trip[] = response.data.map((data: any) => {
return new Trip(
data.id,
data.route_id,
data.service_id,
data.trip_id,
data.trip_headsign,
data.direction_id,
data.block_id,
data.shape_id,
data.wheelchair_accessible,
data.bikes_allowed,
data.origin_stop_id,
data.destination_stop_id,
);
});

return Promise.resolve(entities);
}

//
// async getStopPredictions(stopId: string | null): Promise<Response> {
// return Promise.resolve(undefined);
// }
//
// async getTripCancellation(query: TripCancellationQueryInterface | null): Promise<Response> {
// return Promise.resolve(undefined);
// }
async getGtfsRtTripUpdates(useProtoBuf: boolean): Promise<any> {
return Promise.resolve(undefined);
}

async getGtfsRtVehiclePositions(useProtoBuf: boolean): Promise<any> {
return Promise.resolve(undefined);
}

async getGtfsServiceAlerts(useProtoBuf: boolean): Promise<any> {
return Promise.resolve(undefined);
}


async getStopPredictions(stopId: string | null): Promise<any> {
return Promise.resolve(undefined);
}

async getTripCancellation(query: TripCancellationQueryInterface | null): Promise<any> {
return Promise.resolve(undefined);
}
}
Loading

0 comments on commit c525e30

Please sign in to comment.