feat: open-trip-api-drink (#11)
Reviewed-on: #11 Co-authored-by: Clement <c.boesmier@aptatio.com> Co-committed-by: Clement <c.boesmier@aptatio.com>
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import { getCity, getRadius, getPoiId, getBox } from "./openTripMaps"
|
||||
import express from "express"
|
||||
/**
|
||||
* Initialize Express application instance.
|
||||
@ -22,6 +23,7 @@ function getWelcome(req: express.Request, res: express.Response) {
|
||||
* @openapi
|
||||
* /welcome:
|
||||
* get:
|
||||
* summary: retun just hello
|
||||
* description: Welcome to swagger-jsdoc!
|
||||
* responses:
|
||||
* 200:
|
||||
@ -29,4 +31,137 @@ function getWelcome(req: express.Request, res: express.Response) {
|
||||
*/
|
||||
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)
|
||||
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /otm/radius:
|
||||
* get:
|
||||
* summary: return the drinks in a radius
|
||||
* description: return the drinks in a defined radius
|
||||
* parameters:
|
||||
* - name: lon
|
||||
* in: query
|
||||
* required: true
|
||||
* description: longitude of the center of the radius
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -180
|
||||
* maximum: 180
|
||||
* - name: lat
|
||||
* in: query
|
||||
* required: true
|
||||
* description: latitude of the center of the radius
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -90
|
||||
* maximum: 90
|
||||
* - name: radius
|
||||
* in: query
|
||||
* description: size of the radius
|
||||
* 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/radius", getRadius)
|
||||
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /otm/poidetaill:
|
||||
* get:
|
||||
* summary: detail of a POI
|
||||
* description: detail of a POI link name, kind, rate,...
|
||||
* parameters:
|
||||
* - name: id
|
||||
* in: query
|
||||
* required: true
|
||||
* description: Unique identifier of the object in OpenTripMap
|
||||
* schema:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Return the detaill of an POI in otm
|
||||
* 400:
|
||||
* description: Missing Argument Error
|
||||
* 401:
|
||||
* description: Missing OTM tocken
|
||||
*/
|
||||
app.get("/otm/poidetaill", getPoiId)
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /otm/box:
|
||||
* get:
|
||||
* summary: return the drinks in a box
|
||||
* description: return the drinks in a defined box
|
||||
* parameters:
|
||||
* - name: lon1
|
||||
* in: query
|
||||
* required: true
|
||||
* description: longitude 1 of the box
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -180
|
||||
* maximum: 180
|
||||
* - name: lat1
|
||||
* in: query
|
||||
* required: true
|
||||
* description: latitude 1 of the box
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -90
|
||||
* maximum: 90
|
||||
* - name: lon2
|
||||
* in: query
|
||||
* required: true
|
||||
* description: longitude 2 of the box
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -180
|
||||
* maximum: 180
|
||||
* - name: lat2
|
||||
* in: query
|
||||
* required: true
|
||||
* description: latitude 2 of the box
|
||||
* schema:
|
||||
* type: number
|
||||
* minimum: -90
|
||||
* maximum: 90
|
||||
* 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/box", getBox)
|
||||
|
||||
export default app
|
193
Express/barAndCafe/src/openTripMaps.ts
Normal file
193
Express/barAndCafe/src/openTripMaps.ts
Normal file
@ -0,0 +1,193 @@
|
||||
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
|
||||
|
||||
/**
|
||||
* make a GET request to the OTM for a rectangle search.
|
||||
* @param {string} lon1 Longitude of the 1st point of the box
|
||||
* @param {string} lat1 Latitude of the 1st point of the box
|
||||
* @param {string} lon2 Longitude of the 2nd point of the box
|
||||
* @param {string} lat2 Latitude of the 2nd point of the box
|
||||
* @returns {FeatureCollection} a list of POIs with their type, id, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#))
|
||||
*/
|
||||
async function callBox(lon1:string, lat1:string, lon2: string, lat2: string) {
|
||||
const lonMin = Math.min(parseFloat(lon1), parseFloat(lon2))
|
||||
const lonMax = Math.max(parseFloat(lon1), parseFloat(lon2))
|
||||
const latMin = Math.min(parseFloat(lat1), parseFloat(lat2))
|
||||
const latMax = Math.max(parseFloat(lat1), parseFloat(lat2))
|
||||
|
||||
const options = {
|
||||
method: 'GET',
|
||||
url: 'https://api.opentripmap.com/0.1/en/places/bbox',
|
||||
params: {
|
||||
lon_min: lonMin,
|
||||
lon_max: lonMax,
|
||||
lat_min: latMin,
|
||||
lat_max: latMax,
|
||||
apikey: key,
|
||||
kinds: 'bars,cafes,pubs,biergartens'
|
||||
},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios.request(options)
|
||||
return data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* make GET request to the OTM for radius search.
|
||||
* @param {string} lon Longitude of radius center point
|
||||
* @param {string} lat Latitude du point central du rayon
|
||||
* @param {string} radius search radius size in meters
|
||||
* @returns {FeatureCollection} a list of POIs with their type, id, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#))
|
||||
*/
|
||||
async function callRadius(lon: string, lat: string, radius = '1000') {
|
||||
const optionsDrink = {
|
||||
method: 'GET',
|
||||
url: 'https://api.opentripmap.com/0.1/en/places/radius',
|
||||
params: {
|
||||
radius: radius,
|
||||
lon: lon,
|
||||
lat: lat,
|
||||
apikey: key,
|
||||
kinds: 'bars,cafes,pubs,biergartens'
|
||||
},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios.request(optionsDrink)
|
||||
return data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* make GET request to the OTM for city info.
|
||||
* @param {string} name Name of a city
|
||||
* @returns {Geoname} info of a search city their name, country, lat, lon, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#))
|
||||
*/
|
||||
async function callCity(name:string) {
|
||||
const optionsCity = {
|
||||
method: 'GET',
|
||||
url: 'https://api.opentripmap.com/0.1/en/places/geoname',
|
||||
params: {
|
||||
name: name,
|
||||
apikey: key
|
||||
},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios.request(optionsCity)
|
||||
return data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* make GET request to the OTM for city info.
|
||||
* @param {string} name Name of a city
|
||||
* @returns {Geoname} info of a search city their name, country, lat, lon, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#))
|
||||
*/
|
||||
async function callId(id:string) {
|
||||
const optionsId = {
|
||||
method: 'GET',
|
||||
url: 'https://api.opentripmap.com/0.1/en/places/xid/' + id,
|
||||
params: {
|
||||
apikey: key
|
||||
},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
|
||||
try {
|
||||
const { data } = await axios.request(optionsId)
|
||||
return data
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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"]
|
||||
let radius = req.query["radius"]
|
||||
|
||||
if(!cityName){
|
||||
res.status(400).send("Missing Argument name")
|
||||
return
|
||||
}
|
||||
if(!radius){
|
||||
radius = "1000"
|
||||
}
|
||||
const cityPose = await callCity(cityName as string)
|
||||
res.send( await callRadius(cityPose.lon,cityPose.lat, radius as string))
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle GET request for radius search route ('/otm/radius').
|
||||
* @param {express.Request} req - HTTP Request object.
|
||||
* @param {express.Response} res - HTTP Response object.
|
||||
*/
|
||||
export async function getRadius(req:express.Request, res: express.Response) {
|
||||
const lon = req.query["lon"] as string
|
||||
const lat = req.query["lat"] as string
|
||||
let radius = req.query["radius"]
|
||||
if(!lon || !lat){
|
||||
res.status(400).send("Missing Argument")
|
||||
return
|
||||
}
|
||||
if(!radius){
|
||||
radius = "1000"
|
||||
}
|
||||
|
||||
res.send( await callRadius(lon,lat,radius as string))
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle GET request for radius search route ('/otm/radius').
|
||||
* @param {express.Request} req - HTTP Request object.
|
||||
* @param {express.Response} res - HTTP Response object.
|
||||
*/
|
||||
export async function getPoiId(req: express.Request, res: express.Response){
|
||||
const id = req.query["id"] as string
|
||||
if(!id){
|
||||
res.status(400).send("Missing Argument name")
|
||||
return
|
||||
}
|
||||
res.send( await callId( id as string))
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle GET request for radius search route ('/otm/box').
|
||||
* @param {express.Request} req - HTTP Request object.
|
||||
* @param {express.Response} res - HTTP Response object.
|
||||
*/
|
||||
export async function getBox(req:express.Request, res: express.Response) {
|
||||
const lon1 = req.query["lon1"] as string
|
||||
const lat1 = req.query["lat1"] as string
|
||||
const lon2 = req.query["lon2"] as string
|
||||
const lat2 = req.query["lat2"] as string
|
||||
if(!lon1 || !lat1 || !lon2 || !lat2){
|
||||
res.status(400).send("Missing Argument")
|
||||
return
|
||||
}
|
||||
|
||||
res.send( await callBox(lon1, lat1, lon2, lat2))
|
||||
}
|
||||
|
||||
//TODO: fair une route ou l'on donne 2 coordonée
|
@ -16,6 +16,9 @@ const options = {
|
||||
{
|
||||
url: "http://localhost:"+port.toString(),
|
||||
},
|
||||
{
|
||||
url: "https://drink-tweb.cb85.fr"
|
||||
}
|
||||
],
|
||||
},
|
||||
apis: ['./src/*.ts'],
|
||||
|
Reference in New Issue
Block a user