-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dictionary download in table column
- Loading branch information
1 parent
8c2aa78
commit d9d0ccb
Showing
4 changed files
with
218 additions
and
41 deletions.
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
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,24 @@ | ||
import axios from "axios"; | ||
|
||
async function downloadTable(datasetID, tableId, res) { | ||
try { | ||
const fileUrl = `https://storage.googleapis.com/basedosdados-public/one-click-download/${datasetID}/${tableId}/${tableId}.csv.gz` | ||
const response = await axios({ | ||
url: fileUrl, | ||
method: 'GET', | ||
responseType: 'stream' | ||
}) | ||
|
||
res.setHeader('Content-Disposition', `attachment; filename=${datasetID}_${tableId}.csv.gz`) | ||
res.setHeader('Content-Type', 'application/gzip') | ||
|
||
response.data.pipe(res) | ||
} catch (error) { | ||
console.error(error) | ||
res.status(500).json({ message: 'Error downloading the file' }) | ||
} | ||
} | ||
|
||
export default async function handler(req, res) { | ||
return downloadTable(atob(req.query.p), atob(req.query.q), res) | ||
} |
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
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,61 @@ | ||
import axios from "axios"; | ||
|
||
const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql` | ||
|
||
async function getDictionaryTable(id, slug) { | ||
try { | ||
const res = await axios({ | ||
url: API_URL, | ||
method: "POST", | ||
data: { | ||
query: ` | ||
query { | ||
allDataset (id: "${id}") { | ||
edges { | ||
node { | ||
tables (slug: "${slug}") { | ||
edges { | ||
node { | ||
_id | ||
slug | ||
uncompressedFileSize | ||
cloudTables{ | ||
edges{ | ||
node{ | ||
gcpTableId | ||
gcpDatasetId | ||
gcpProjectId | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
}) | ||
const data = res?.data?.data?.allDataset?.edges[0]?.node?.tables?.edges[0]?.node | ||
return data | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
export default async function handler(req, res) { | ||
const param = atob(req.query.p) | ||
let result = await getDictionaryTable(param, "dicionario") | ||
|
||
if (result === undefined) { | ||
result = await getDictionaryTable(param, "dictionary") | ||
} | ||
|
||
if (result === undefined) { | ||
res.status(500).json({ error: "Nenhum resultado encontrado" }) | ||
} else { | ||
res.status(200).json(result) | ||
} | ||
} |