-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
226 additions
and
64 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
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,10 @@ | ||
export abstract class Search< | ||
SearchParams extends Record<string, unknown>, | ||
SearchResults extends Record<string, unknown> | ||
> { | ||
params: SearchParams = {} as SearchParams | ||
abstract defaultParams: SearchParams | ||
|
||
data: SearchResults | undefined | ||
abstract readonly fetch: (params: SearchParams) => Promise<SearchResults> | ||
} |
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,8 @@ | ||
export type DownloadFileParams = { | ||
FileType: string | ||
accession: string | ||
fileid: number | ||
FileIDAll: string | ||
fileidLst: string[] | ||
Islegacy: boolean | ||
} |
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,75 @@ | ||
// https://elibrary.ferc.gov/eLibrarywebapi/api/File/DownloadP8File | ||
|
||
import { writeFile } from 'fs' | ||
import { join } from 'path' | ||
import { DownloadFileParams } from '~/types/DownloadFile' | ||
|
||
export type FileInfo = { | ||
path: string | ||
file: string | ||
format: string | ||
} | ||
|
||
const FileInfoDefault: FileInfo = { | ||
path: 'temp', | ||
format: 'pdf', | ||
file: 'temp', | ||
} | ||
|
||
export async function downloadFile( | ||
params: DownloadFileParams, | ||
fileInfo?: Partial<FileInfo> | ||
) { | ||
const fi = { | ||
...FileInfoDefault, | ||
...{ file: params.fileidLst[0] ?? FileInfoDefault.file }, | ||
...fileInfo, | ||
} as FileInfo | ||
|
||
return download(fi, 'File/DownloadP8File', {}, params) | ||
} | ||
|
||
export async function generatePDF( | ||
accessionNumber: string, | ||
fileInfo?: Partial<FileInfo> | ||
) { | ||
const fi = { | ||
...FileInfoDefault, | ||
...{ file: accessionNumber }, | ||
...fileInfo, | ||
} as FileInfo | ||
|
||
return download( | ||
fi, | ||
'File/DownloadPDF', | ||
{ accessionNumber }, | ||
'{serverLocation: ""}' | ||
) | ||
} | ||
|
||
async function download< | ||
B extends Record<string, unknown> | string, | ||
P extends Record<string, string> | ||
>(fileInfo: FileInfo, url: string, params: P, body: B) { | ||
fetch( | ||
`https://elibrary.ferc.gov/eLibrarywebapi/api/${url}?` + | ||
new URLSearchParams(params), | ||
{ | ||
headers: { | ||
accept: 'application/json, text/plain, */*', | ||
'content-type': 'application/json', | ||
}, | ||
body: typeof body === 'string' ? body : JSON.stringify(body), | ||
method: 'POST', | ||
} | ||
) | ||
.then((r) => r.arrayBuffer()) | ||
.then((ab) => | ||
writeFile( | ||
join(fileInfo.path, `${fileInfo.file}.${fileInfo.format}`), | ||
Buffer.from(ab), | ||
'binary', | ||
() => {} | ||
) | ||
) | ||
} |
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,13 @@ | ||
export function fetchFunction<SearchParams, SearchResults>( | ||
searchType: string | ||
): (params: SearchParams) => Promise<SearchResults> { | ||
return async (params: SearchParams) => { | ||
return fetch(`https://elibrary.ferc.gov/eLibrarywebapi/api/${searchType}`, { | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify(params), | ||
method: 'post', | ||
}).then((r) => r.json()) | ||
} | ||
} |
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,36 @@ | ||
import { existsSync } from 'fs' | ||
import { describe, expect, test } from 'vitest' | ||
import { downloadFile } from '~/util/download' | ||
|
||
describe.concurrent('Download Files', () => { | ||
test('Download PDF', async () => { | ||
const fileID = '020AAB97-66E2-5005-8110-C31FAFC91712' | ||
await downloadFile({ | ||
FileType: '', | ||
accession: '', | ||
fileid: 0, | ||
FileIDAll: '', | ||
fileidLst: [fileID], | ||
Islegacy: false, | ||
}) | ||
expect(existsSync(`temp/${fileID}`)).toBeTruthy() | ||
}) | ||
|
||
test('Download TIF', async () => { | ||
const fileID = '020D2E72-66E2-5005-8110-C31FAFC91712' | ||
await downloadFile( | ||
{ | ||
FileType: '', | ||
accession: '', | ||
fileid: 0, | ||
FileIDAll: '', | ||
fileidLst: [fileID], | ||
Islegacy: false, | ||
}, | ||
{ | ||
format: 'tif', | ||
} | ||
) | ||
expect(existsSync(`temp/${fileID}`)).toBeTruthy() | ||
}) | ||
}) |
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