-
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.
feat(WIP backends): add multiple backends
- Loading branch information
1 parent
b979426
commit 3f79c54
Showing
9 changed files
with
440 additions
and
108 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,109 @@ | ||
import { buildLibrary, buildMeasure } from "../helpers/cql-measure"; | ||
import { responseStore } from "../stores/response"; | ||
import type { Site } from "../types/response"; | ||
import { measureStore } from "../stores/measures"; | ||
|
||
let measureDefinitions; | ||
|
||
measureStore.subscribe((store) => { | ||
measureDefinitions = store.map((measure) => measure.measure); | ||
}); | ||
|
||
export class Blaze { | ||
constructor( | ||
private url: URL, | ||
private name: string, | ||
private auth: string = "", | ||
) {} | ||
|
||
/** | ||
* sends the query to beam and updates the store with the results | ||
* @param cql the query as cql string | ||
* @param controller the abort controller to cancel the request | ||
*/ | ||
async send(cql: string, controller?: AbortController): Promise<void> { | ||
try { | ||
responseStore.update((store) => { | ||
store.set(this.name, { status: "claimed", data: null }); | ||
return store; | ||
}); | ||
const libraryResponse = await fetch( | ||
new URL(`${this.url}/Library`), | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(buildLibrary(cql)), | ||
signal: controller?.signal, | ||
}, | ||
); | ||
if (!libraryResponse.ok) { | ||
this.handleError( | ||
`Couldn't create Library in Blaze`, | ||
libraryResponse, | ||
); | ||
} | ||
const library = await libraryResponse.json(); | ||
const measureResponse = await fetch( | ||
new URL(`${this.url}/Measure`), | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify( | ||
buildMeasure(library.url, measureDefinitions), | ||
), | ||
signal: controller.signal, | ||
}, | ||
); | ||
if (!measureResponse.ok) { | ||
this.handleError( | ||
`Couldn't create Measure in Blaze`, | ||
measureResponse, | ||
); | ||
} | ||
const measure = await measureResponse.json(); | ||
const dataResponse = await fetch( | ||
new URL( | ||
`${this.url}/Measure/$evaluate-measure?measure=${measure.url}&periodStart=2000&periodEnd=2030`, | ||
), | ||
{ | ||
signal: controller.signal, | ||
}, | ||
); | ||
if (!dataResponse.ok) { | ||
this.handleError( | ||
`Couldn't evaluate Measure in Blaze`, | ||
dataResponse, | ||
); | ||
} | ||
const blazeResponse: Site = await dataResponse.json(); | ||
responseStore.update((store) => { | ||
store.set(this.name, { | ||
status: "succeeded", | ||
data: blazeResponse, | ||
}); | ||
return store; | ||
}); | ||
} catch (err) { | ||
if (err.name === "AbortError") { | ||
console.log(`Aborting former blaze request.`); | ||
} else { | ||
console.error(err); | ||
} | ||
} | ||
} | ||
|
||
async handleError(message: string, response: Response): Promise<void> { | ||
const errorMessage = await response.text(); | ||
console.debug( | ||
`${message}. Received error ${response.status} with message ${errorMessage}`, | ||
); | ||
responseStore.update((store) => { | ||
store.set(this.name, { status: "permfailed", data: null }); | ||
return store; | ||
}); | ||
} | ||
} |
Oops, something went wrong.