|
1 |
| -import { dataRequest } from 'utils/request'; |
| 1 | +import { dataMartRequest } from 'utils/request'; |
2 | 2 |
|
3 |
| -// this PoC is only meant for net change widget (we chose net change for its simplicity) |
4 |
| -export default async (req, res) => { |
5 |
| - // example request: |
6 |
| - // localhost:3000/api/datamart/net-change/?type=global&adm0=MEX&adm1=9&adm2=3&download=true |
7 |
| - const { adm0 = '', adm1 = '', adm2 = '', download } = req.query; |
| 3 | +const get_net_tree_cover_change_results = async (iso, adm1, adm2) => { |
| 4 | + const paramStr = `?iso=${iso}${adm1 ? `&adm1=${adm1}` : ''}${ |
| 5 | + adm2 ? `&adm2=${adm2}` : '' |
| 6 | + }`; |
| 7 | + const url = `/datamart/analysis/forest_change/tree_cover_change/net_tree_cover_change${paramStr}`; |
| 8 | + // console.info(`Data Mart Request: ${url}`); |
| 9 | + const response = await dataMartRequest.get(url); |
8 | 10 |
|
9 |
| - const isDownload = download === 'true'; |
| 11 | + return [response?.data]; |
| 12 | +}; |
10 | 13 |
|
| 14 | +const get_net_tree_cover_change_download_url = async (adm0, adm1, adm2) => { |
11 | 15 | // checks
|
12 | 16 | if (adm1 === undefined && adm2 !== undefined) {
|
13 |
| - return res.status(400).send({ |
14 |
| - message: 'if adm2 is present, adm1 can not be empty', |
15 |
| - }); |
| 17 | + throw new Error('if adm2 is present, adm1 can not be empty'); |
16 | 18 | }
|
17 | 19 |
|
18 | 20 | if (!adm0 && (adm1 || adm2)) {
|
19 |
| - return res.status(400).send({ |
20 |
| - message: 'if adm1 or adm2 are present, adm0 can not be empty', |
21 |
| - }); |
| 21 | + throw new Error('if adm1 or adm2 are present, adm0 can not be empty'); |
22 | 22 | }
|
23 | 23 |
|
24 | 24 | let url = '/dataset';
|
@@ -71,22 +71,30 @@ export default async (req, res) => {
|
71 | 71 | fieldsList.push('adm2_name');
|
72 | 72 | }
|
73 | 73 |
|
74 |
| - url = `${url}/${dataset}/v202209/${ |
75 |
| - isDownload ? 'download/csv' : 'query' |
76 |
| - }?sql=SELECT ${fieldsList} FROM data ${where}`; |
| 74 | + url = `${url}/${dataset}/v202209/download/csv?sql=SELECT ${fieldsList} FROM data ${where}`; |
| 75 | + // console.info(`Download url: ${url}`); |
77 | 76 |
|
78 |
| - if (isDownload) { |
79 |
| - return res.status(200).json({ |
80 |
| - data: { |
81 |
| - url, |
82 |
| - }, |
83 |
| - }); |
84 |
| - } |
| 77 | + return { url }; |
| 78 | +}; |
85 | 79 |
|
86 |
| - // fetch |
87 |
| - const response = await dataRequest.get(url); |
| 80 | +// this PoC is only meant for net change widget (we chose net change for its simplicity) |
| 81 | +export default async (req, res) => { |
| 82 | + const { adm0 = '', adm1 = '', adm2 = '', download } = req.query; |
| 83 | + const isDownload = download === 'true'; |
| 84 | + let response = null; |
88 | 85 |
|
89 |
| - return res.status(200).json({ |
90 |
| - data: response?.data, |
91 |
| - }); |
| 86 | + try { |
| 87 | + if (isDownload) { |
| 88 | + response = await get_net_tree_cover_change_download_url(adm0, adm1, adm2); |
| 89 | + } else { |
| 90 | + response = await get_net_tree_cover_change_results(adm0, adm1, adm2); |
| 91 | + } |
| 92 | + |
| 93 | + return res.status(200).json({ data: response }); |
| 94 | + } catch (error) { |
| 95 | + // console.error('Error fetching net tree cover change data from the GFW data mart:', { message: error.message }); |
| 96 | + return res.status(500).json({ |
| 97 | + error: 'Failed to fetch net tree cover change data.', |
| 98 | + }); |
| 99 | + } |
92 | 100 | };
|
0 commit comments