multy-stub/server/routers/epja-2024-1/ecliptica/plants/getPlants.js

144 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-10-16 11:06:23 +03:00
const express = require ('express')
const axios = require ('axios')
const plantsRouter = express.Router ()
const CONFIG = require ('./config')
2024-10-12 23:03:52 +03:00
2024-10-15 13:38:49 +03:00
2024-10-15 13:36:13 +03:00
async function getAccessToken () {
2024-10-16 11:06:23 +03:00
const formData = new FormData ()
formData.append ('grant_type', 'client_credentials')
formData.append ('client_id', CONFIG.CLIENT_ID)
formData.append ('client_secret', CONFIG.CLIENT_SECRET)
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
try {
const response = await axios.post (
'https://open.plantbook.io/api/v1/token/',
formData,
{
headers: {'Content-Type': 'multipart/form-data'},
}
2024-10-16 11:06:23 +03:00
)
2024-10-15 13:36:13 +03:00
if (response.data && response.data.access_token) {
2024-10-16 11:06:23 +03:00
console.log ('Access token retrieved:', response.data.access_token)
return response.data.access_token
2024-10-15 13:36:13 +03:00
} else {
2024-10-16 11:06:23 +03:00
console.error ('Error: access_token not found in response')
return null
2024-10-12 23:03:52 +03:00
}
2024-10-15 13:36:13 +03:00
} catch (error) {
console.error (
'Error fetching access token:',
error.response ? error.response.data : error.message
2024-10-16 11:06:23 +03:00
)
return null
2024-10-15 13:36:13 +03:00
}
}
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
async function fetchPlantData (plantId) {
2024-10-16 11:06:23 +03:00
const accessToken = await getAccessToken ()
2024-10-15 13:36:13 +03:00
if (!accessToken) {
2024-10-16 11:06:23 +03:00
return null
2024-10-15 13:36:13 +03:00
}
try {
const response = await axios.get (
`https://open.plantbook.io/api/v1/plant/detail/${encodeURIComponent (plantId)}/`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
2024-10-16 11:06:23 +03:00
)
return response.data
2024-10-15 13:36:13 +03:00
} catch (error) {
console.error (
'Error fetching plant data:',
error.response ? error.response.data : error.message
2024-10-16 11:06:23 +03:00
)
return null
2024-10-15 13:36:13 +03:00
}
}
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
plantsRouter.get ('/list', async (req, res) => {
2024-10-16 11:06:23 +03:00
const accessToken = await getAccessToken ()
2024-10-15 13:36:13 +03:00
if (!accessToken) {
2024-10-16 11:06:23 +03:00
res.status (500).send ({message: 'Error obtaining access token'})
return
2024-10-15 13:36:13 +03:00
}
try {
2024-10-16 11:06:23 +03:00
const alias = req.query.alias || 'monstera'
2024-10-15 13:36:13 +03:00
const response = await axios.get (
'https://open.plantbook.io/api/v1/plant/search',
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
params: {
alias: alias,
},
}
2024-10-16 11:06:23 +03:00
)
2024-10-12 23:03:52 +03:00
2024-10-16 11:06:23 +03:00
const plantsList = response.data.results
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
const plants = await Promise.all (
plantsList.map (async plant => {
2024-10-16 11:06:23 +03:00
const plantDetails = await fetchPlantData (plant.pid)
2024-10-15 13:36:13 +03:00
return {
id: plant.pid,
alias: plant.alias,
display_name: plant.display_name,
image_url: plantDetails ? plantDetails.image_url : null,
max_light: plantDetails ? plantDetails.max_light : null,
min_light: plantDetails ? plantDetails.min_light : null,
max_temp: plantDetails ? plantDetails.max_temp : null,
min_temp: plantDetails ? plantDetails.min_temp : null,
max_env_humid: plantDetails ? plantDetails.max_env_humid : null,
min_env_humid: plantDetails ? plantDetails.min_env_humid : null,
max_soil_moist: plantDetails ? plantDetails.max_soil_moist : null,
min_soil_moist: plantDetails ? plantDetails.min_soil_moist : null,
max_soil_ec: plantDetails ? plantDetails.max_soil_ec : null,
min_soil_ec: plantDetails ? plantDetails.min_soil_ec : null,
2024-10-16 11:06:23 +03:00
}
2024-10-15 13:36:13 +03:00
})
2024-10-16 11:06:23 +03:00
)
2024-10-12 23:03:52 +03:00
2024-10-16 11:06:23 +03:00
res.send ({results: plants})
2024-10-15 13:36:13 +03:00
} catch (error) {
console.error (
'Error fetching plant list:',
error.response ? error.response.data : error.message
2024-10-16 11:06:23 +03:00
)
res.status (500).send ({message: 'Error fetching plant list'})
2024-10-15 13:36:13 +03:00
}
2024-10-16 11:06:23 +03:00
})
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
plantsRouter.get ('/:id', async (req, res) => {
2024-10-16 11:06:23 +03:00
const plantId = req.params.id
const plantData = await fetchPlantData (plantId)
2024-10-12 23:03:52 +03:00
2024-10-15 13:36:13 +03:00
if (plantData) {
const detailedPlantData = {
id: plantData.pid,
display_name: plantData.display_name,
alias: plantData.alias,
max_light: plantData.max_light,
min_light: plantData.min_light,
max_temperature: plantData.max_temp,
min_temperature: plantData.min_temp,
max_humidity: plantData.max_env_humid,
min_humidity: plantData.min_env_humid,
max_soil_moisture: plantData.max_soil_moist,
min_soil_moisture: plantData.min_soil_moist,
max_soil_ec: plantData.max_soil_ec,
min_soil_ec: plantData.min_soil_ec,
image_url: plantData.image_url,
2024-10-16 11:06:23 +03:00
}
2024-10-12 23:03:52 +03:00
2024-10-16 11:06:23 +03:00
res.send (detailedPlantData)
2024-10-15 13:36:13 +03:00
} else {
2024-10-16 11:06:23 +03:00
res.status (404).send ({message: 'Plant not found'})
2024-10-15 13:36:13 +03:00
}
2024-10-16 11:06:23 +03:00
})
2024-10-12 23:03:52 +03:00
2024-10-16 11:06:23 +03:00
module.exports = plantsRouter