implement city route for OTM
This commit is contained in:
parent
6efb268394
commit
bab32023fb
@ -1,3 +1,4 @@
|
|||||||
|
import { getCity } from "./openTripMaps"
|
||||||
import express from "express"
|
import express from "express"
|
||||||
/**
|
/**
|
||||||
* Initialize Express application instance.
|
* Initialize Express application instance.
|
||||||
@ -22,6 +23,7 @@ function getWelcome(req: express.Request, res: express.Response) {
|
|||||||
* @openapi
|
* @openapi
|
||||||
* /welcome:
|
* /welcome:
|
||||||
* get:
|
* get:
|
||||||
|
* summary: retun just hello
|
||||||
* description: Welcome to swagger-jsdoc!
|
* description: Welcome to swagger-jsdoc!
|
||||||
* responses:
|
* responses:
|
||||||
* 200:
|
* 200:
|
||||||
@ -29,4 +31,27 @@ function getWelcome(req: express.Request, res: express.Response) {
|
|||||||
*/
|
*/
|
||||||
app.get("/welcome", getWelcome)
|
app.get("/welcome", getWelcome)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @openapi
|
||||||
|
* /otm/city:
|
||||||
|
* get:
|
||||||
|
* summary: return the drinks in a city
|
||||||
|
* description: return the drinks in a defined perimeter in a city
|
||||||
|
* parameters:
|
||||||
|
* - name: name
|
||||||
|
* in: query
|
||||||
|
* required: true
|
||||||
|
* description: the name of the city
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Return a list of bars and coffee in city in geoJSON format
|
||||||
|
* 400:
|
||||||
|
* description: Missing Argument Error
|
||||||
|
* 401:
|
||||||
|
* description: Missing OTM tocken
|
||||||
|
*/
|
||||||
|
app.get("/otm/city", getCity)
|
||||||
|
|
||||||
export default app
|
export default app
|
71
Express/barAndCafe/src/openTripMaps.ts
Normal file
71
Express/barAndCafe/src/openTripMaps.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import express from "express"
|
||||||
|
import * as dotenv from "dotenv";
|
||||||
|
|
||||||
|
dotenv.config({path: '../../.env'})
|
||||||
|
|
||||||
|
const key = process.env.OPEN_TRIP_MAPS_KEY
|
||||||
|
|
||||||
|
const optionsDrink = {
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://api.opentripmap.com/0.1/en/places/radius',
|
||||||
|
params: {
|
||||||
|
radius: '1000',
|
||||||
|
lon: '-1.43333',
|
||||||
|
lat: '46.66667',
|
||||||
|
apikey: key,
|
||||||
|
kinds: 'bars,cafes,pubs,biergartens'
|
||||||
|
},
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const optionsCity = {
|
||||||
|
method: 'GET',
|
||||||
|
url: 'https://api.opentripmap.com/0.1/en/places/geoname',
|
||||||
|
params: {
|
||||||
|
name: 'Paris',
|
||||||
|
apikey: key
|
||||||
|
},
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle GET request for city search route ('/otm/city').
|
||||||
|
* @param {express.Request} req - HTTP Request object.
|
||||||
|
* @param {express.Response} res - HTTP Response object.
|
||||||
|
*/
|
||||||
|
export async function getCity(req: express.Request, res: express.Response) {
|
||||||
|
const cityName = req.query["name"]
|
||||||
|
const radius = req.query["radius"]
|
||||||
|
|
||||||
|
if(!key){
|
||||||
|
res.status(401).send("Missing OTM key")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if(!cityName){
|
||||||
|
res.status(400).send("Missing Argument name")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if(radius){
|
||||||
|
optionsDrink.params.radius = radius as string
|
||||||
|
}
|
||||||
|
|
||||||
|
optionsCity.params.name = cityName as string
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { data } = await axios.request(optionsCity);
|
||||||
|
optionsDrink.params.lat = data.lat
|
||||||
|
optionsDrink.params.lon = data.lon
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { data } = await axios.request(optionsDrink);
|
||||||
|
res.send(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user