Skip to content
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

Use json-big to parse main query response #24

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from 'axios'
import JSONbig from 'json-big'

// Defines the API instance in Axios with special handling for big integers.
const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_URL,
timeout: 5000,
transformResponse: [
function transform(data) {
// Replacing the default transformResponse in axios because this uses JSON.parse and causes problems
// with precision of big numbers.
if (typeof data === 'string') {
try {
data = JSONbig.parse(data)
} catch (e) {
/* Ignore */
}
}
return data
}
]
})

export default axiosInstance
20 changes: 9 additions & 11 deletions src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@

<script setup lang="ts">

import axios from 'axios'
import { ref, onMounted, watch } from 'vue'
import TextInput from '@/components/TextInput.vue'
import DropdownSelect from '@/components/DropdownSelect.vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '@/store/app'
import axiosInstance from '@/axios'

// get the application state store and router
const store = useAppStore()
const router = useRouter()


// set up a form reference, is the name in v-form ref="form"
let form = ref(null);

Expand Down Expand Up @@ -163,13 +164,11 @@ async function submit_form(this: any) {
[formData.value.ra, formData.value.dec] = formData.value.coords ? formData.value.coords.split(',') : ["", ""]
console.log('submitting', formData.value)

// submit the POST request to Valis
await axios.post(import.meta.env.VITE_API_URL + '/query/main',
formData.value,
{headers: {'Content-Type': 'application/json'}})
await axiosInstance.post('/query/main',
formData.value, {headers: {'Content-Type': 'application/json'}})
.then((response) => {
// handle the initial response
console.log(response.data)
console.log(response)

// check for good status in response
if (response.data['status'] != 'success') {
Expand Down Expand Up @@ -236,9 +235,9 @@ onMounted(() => {

// set up API call endpoints
let endpoints = [
import.meta.env.VITE_API_URL + `/query/list/cartons`,
import.meta.env.VITE_API_URL + `/query/list/programs`,
import.meta.env.VITE_API_URL + `/query/list/program-map`
`/query/list/cartons`,
`/query/list/programs`,
`/query/list/program-map`
]

// check if the store already has data saved
Expand All @@ -248,7 +247,7 @@ onMounted(() => {
}

// await the promises and cache the results in the store
Promise.all(endpoints.map((endpoint) => axios.get(endpoint)))
Promise.all(endpoints.map((endpoint) => axiosInstance.get(endpoint)))
.then(([{data: carts}, {data: progs}, {data: progmap}] )=> {
console.log({ carts, progs, progmap })
store.store_cartons(carts, progs, progmap)
Expand Down Expand Up @@ -277,4 +276,3 @@ onMounted(() => {
// }, { immediate: true, deep: true })

</script>

31 changes: 8 additions & 23 deletions src/views/Target.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@

<script setup lang="ts">

import axios from 'axios'
import { useAppStore } from '@/store/app'
import { useRoute } from 'vue-router'
import { ref, onMounted } from 'vue'
import JSONbig from 'json-big'

import Solara from '@/components/Solara.vue'
import AladinLite from '@/components/AladinLite.vue'
import TargetResolver from '@/components/TargetResolver.vue'
import DataDownload from '@/components/DataDownload.vue'

import axiosInstance from '@/axios'

// get the application state store and router
const store = useAppStore()
const route = useRoute()
Expand Down Expand Up @@ -165,29 +165,14 @@ async function get_target_info() {

// set up API call endpoints
let endpoints = [
import.meta.env.VITE_API_URL + `/target/ids/${sdss_id}?release=${rel}`,
import.meta.env.VITE_API_URL + `/target/cartons/${sdss_id}?release=${rel}`,
import.meta.env.VITE_API_URL + `/target/catalogs/${sdss_id}?release=${rel}`,
import.meta.env.VITE_API_URL + `/target/pipelines/${sdss_id}?release=${rel}`
`/target/ids/${sdss_id}?release=${rel}`,
`/target/cartons/${sdss_id}?release=${rel}`,
`/target/catalogs/${sdss_id}?release=${rel}`,
`/target/pipelines/${sdss_id}?release=${rel}`
]

// axios config
// see https://axios-http.com/docs/req_config
const config = {
transformResponse: [function transform(data) {
// Replacing the default transformResponse in axios because this uses JSON.parse and causes problems
// with precision of big numbers.
if (typeof data === 'string') {
try {
data = JSONbig.parse(data);
} catch (e) { /* Ignore */ }
}
return data
}],
}

// await the promises
await Promise.all(endpoints.map((endpoint) => axios.get(endpoint, config)))
await Promise.all(endpoints.map((endpoint) => axiosInstance.get(endpoint)))
.then(([{data: target}, {data: cartons}, {data: catalogs}, {data: pipes}] )=> {
console.log({ target, cartons, catalogs, pipes })
loading.value = false
Expand Down Expand Up @@ -225,7 +210,7 @@ async function get_db_info() {
return
}

await axios.get(import.meta.env.VITE_API_URL + '/info/database')
await axiosInstance.get('/info/database')
.then((response) => {
console.log('db info', response.data)
// store the db metadata
Expand Down
Loading