-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FLAG-1263: Data mart api client #4966
Draft
wri7tno
wants to merge
4
commits into
develop
Choose a base branch
from
feat/FLAG-1263
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+323
−4
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
create api routes
commit 29ccaadce324e2afefc1fffbd2ac9ed80ac8b8d1
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { | ||
createRequestByGeostoryId, | ||
getDataByGeostoreId, | ||
getDataFromLink, | ||
} from 'services/datamart'; | ||
import { GFW_DATA_API, GFW_STAGING_DATA_API } from 'utils/apis'; | ||
|
||
// types | ||
/** | ||
* @typedef {object} DataLinkObject | ||
* @property {string} link - The URL to POST the content. | ||
*/ | ||
|
||
/** | ||
* @typedef {object} GetResponseObject | ||
* @property {string} status - status. | ||
* @property {DataLinkObject} data - data link object. | ||
*/ | ||
|
||
/** | ||
* @typedef {object} NotFoundObject | ||
* @property {string} status - status. | ||
* @property {string} message - message. | ||
*/ | ||
// END types | ||
|
||
const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV; | ||
|
||
// We never use the staging api | ||
export const DATA_API_URL = | ||
ENVIRONMENT === 'staging' ? GFW_STAGING_DATA_API : GFW_DATA_API; | ||
|
||
/** | ||
* @param {NextApiRequest} req | ||
* @param {NextApiResponse} res | ||
*/ | ||
const fetchDataByDatasetAndGeostore = async (req, res) => { | ||
const { query } = req; | ||
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc | ||
const { slug: slugs, geostore_id, canopy_cover } = query; | ||
|
||
if (slugs.length === 0) { | ||
res.status(400).send(); | ||
return; | ||
} | ||
|
||
if (slugs.length === 1) { | ||
const dataByGeostore = await getDataByGeostoreId({ | ||
dataset: slugs[0], | ||
geostoreId: geostore_id, | ||
canopy: canopy_cover, | ||
}); | ||
|
||
res.status(200).send(dataByGeostore); | ||
return; | ||
} | ||
|
||
const url = `${DATA_API_URL}/${slugs.join('/')}`; | ||
try { | ||
const dataByUrl = await getDataFromLink({ url }); | ||
|
||
res.send(dataByUrl); | ||
} catch (error) { | ||
res.status(error.response?.status).send({ | ||
status: error.response?.status, | ||
message: error?.message, | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* @param {NextApiRequest} req | ||
* @param {NextApiResponse} res | ||
*/ | ||
const postData = async (req, res) => { | ||
const { query } = req; | ||
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc | ||
const { slug: slugs, geostore_id, canopy_cover } = query; | ||
|
||
if (slugs.length === 0) { | ||
res.status(400).send(); | ||
return; | ||
} | ||
|
||
try { | ||
const submitted = await createRequestByGeostoryId({ | ||
dataset: slugs[0], | ||
geostoreId: geostore_id, | ||
canopy: canopy_cover, | ||
}); | ||
|
||
res.status(201).send(submitted); | ||
} catch (error) { | ||
res.status(error.response?.status).send({ | ||
status: error.response?.status, | ||
message: error?.message, | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* @param {NextApiRequest} req | ||
* @param {NextApiResponse} res | ||
*/ | ||
export default async (req, res) => { | ||
switch (req.method) { | ||
case 'POST': | ||
postData(req, res); | ||
break; | ||
case 'GET': | ||
fetchDataByDatasetAndGeostore(req, res); | ||
break; | ||
default: | ||
res.send(405); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Type confusion through parameter tampering Critical
Copilot Autofix AI 10 days ago
To fix the problem, we need to ensure that the
slugs
parameter is always treated as an array of strings. We can do this by checking the type ofslugs
and converting it to an array if it is not already one. This will prevent type confusion attacks and ensure that the code behaves as expected.We will modify the
fetchDataByDatasetAndGeostore
andpostData
functions to include type checks and conversions for theslugs
parameter. Specifically, we will:slugs
is an array. If not, convert it to an array containing the single value.slugs
is an array.