Skip to content

Commit

Permalink
API calls: pass User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed May 26, 2024
1 parent ac4463c commit a6fb606
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const OPF_ICON = 'mdi-bookshelf'

export default {
APP_NAME: 'Open Prices',
APP_USER_AGENT: 'Open Prices Web App',
OFF_NAME: OFF_NAME,
OFF_URL: 'https://world.openfoodfacts.org',
OFF_ICON: OFF_ICON,
OFF_WIKI_URL: 'https://wiki.openfoodfacts.org/Main_Page',
OFF_WIKI_APP_URL: 'https://wiki.openfoodfacts.org/Project:Open-Prices',
OFF_WIKI_GDPR_REQUEST_URL: 'https://wiki.openfoodfacts.org/GDPR_request',
OFF_API_URL: 'https://world.openfoodfacts.org/api/v2/product',
OBF_NAME: OBF_NAME,
OBF_URL: 'https://world.openbeautyfacts.org',
OBF_ICON: OBF_ICON,
Expand Down
79 changes: 34 additions & 45 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useAppStore } from '../store'
import constants from '../constants'

const OPENFOODFACTS_PRODUCT_URL = 'https://world.openfoodfacts.org/api/v2/product'

const DEFAULT_HEADERS = {
'Content-Type': 'application/json',
'User-Agent': constants.APP_USER_AGENT
}


export default {
Expand All @@ -12,6 +16,7 @@ export default {
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/auth`, {
method: 'POST',
body: formData,
headers: DEFAULT_HEADERS
})
.then((response) => response.json())
},
Expand All @@ -21,9 +26,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/users?${new URLSearchParams({...defaultParams, ...params})}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS
})
.then((response) => response.json())
},
Expand All @@ -35,9 +38,9 @@ export default {
formData.append('type', type)
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/proofs/upload`, {
method: 'POST',
headers: {
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`
},
}),
body: formData,
})
.then((response) => response.json())
Expand All @@ -50,10 +53,9 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/proofs?${new URLSearchParams({...defaultParams, ...params})}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`
},
}),
})
.then((response) => response.json())
},
Expand All @@ -64,10 +66,9 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/proofs/${proofId}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`
},
}),
})
.then((response) => response.json())
},
Expand All @@ -76,10 +77,9 @@ export default {
const store = useAppStore()
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/proofs/${proofId}`, {
method: 'PATCH',
headers: {
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`,
'Content-Type': 'application/json',
},
}),
body: JSON.stringify(params),
})
.then((response) => response.json())
Expand All @@ -89,9 +89,9 @@ export default {
const store = useAppStore()
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/proofs/${proofId}`, {
method: 'DELETE',
headers: {
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`
},
}),
})
// .then((response) => response.json())
},
Expand All @@ -101,10 +101,9 @@ export default {
store.user.last_product_mode_used = priceData.product_code ? 'barcode' : 'category'
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/prices`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${store.user.token}`
},
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`,
}),
body: JSON.stringify(priceData),
})
.then((response) => response.json())
Expand All @@ -115,9 +114,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/prices?${new URLSearchParams({...defaultParams, ...params})}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},
Expand All @@ -126,10 +123,9 @@ export default {
const store = useAppStore()
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/prices/${priceId}`, {
method: 'PATCH',
headers: {
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`,
'Content-Type': 'application/json',
},
}),
body: JSON.stringify(params),
})
.then((response) => response.json())
Expand All @@ -139,9 +135,9 @@ export default {
const store = useAppStore()
return fetch(`${import.meta.env.VITE_OPEN_PRICES_API_URL}/prices/${priceId}`, {
method: 'DELETE',
headers: {
headers: Object.assign({}, DEFAULT_HEADERS, {
'Authorization': `Bearer ${store.user.token}`
},
}),
})
// .then((response) => response.json())
},
Expand All @@ -151,9 +147,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/products?${new URLSearchParams({...defaultParams, ...params})}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},
Expand All @@ -162,9 +156,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/products/${productId}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},
Expand All @@ -173,9 +165,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/products/code/${productCode}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},
Expand All @@ -185,9 +175,7 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/locations?${new URLSearchParams({...defaultParams, ...params})}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},
Expand All @@ -196,30 +184,31 @@ export default {
const url = `${import.meta.env.VITE_OPEN_PRICES_API_URL}/locations/${locationId}`
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
headers: DEFAULT_HEADERS,
})
.then((response) => response.json())
},

openfoodfactsProductSearch(code) {
return fetch(`${OPENFOODFACTS_PRODUCT_URL}/${code}.json`, {
return fetch(`${constants.OFF_API_URL}/${code}.json`, {
method: 'GET',
headers: DEFAULT_HEADERS
})
.then((response) => response.json())
},

openstreetmapNominatimSearch(q) {
return fetch(`${constants.OSM_NOMINATIM_SEARCH_URL}?q=${q}&addressdetails=1&format=json&limit=10`, {
method: 'GET',
headers: DEFAULT_HEADERS
})
.then((response) => response.json())
.then((data) => data.filter(l => !constants.NOMINATIM_RESULT_TYPE_EXCLUDE_LIST.includes(l.type)))
},
openstreetmapPhotonSearch(q) {
return fetch(`${constants.OSM_PHOTON_SEARCH_URL}?q=${q}&limit=10`, {
method: 'GET',
headers: DEFAULT_HEADERS
})
.then((response) => response.json())
.then(data => data.features)
Expand Down

0 comments on commit a6fb606

Please sign in to comment.