Skip to content

Commit

Permalink
Merge pull request #107 from technologiestiftung/feat/swimapi
Browse files Browse the repository at this point in the history
feat: Swim api
  • Loading branch information
ff6347 committed Apr 25, 2024
2 parents 65f1070 + 1aa6ba7 commit 724bfd9
Show file tree
Hide file tree
Showing 11 changed files with 41,317 additions and 12,150 deletions.
10 changes: 9 additions & 1 deletion .env-sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
MAPBOXKEY=YOUR_MAPBOX_KEY
URL=http://localhost:5000
URL=http://localhost:5000
# the variables below need to be defined
# in netlify's environment variables
# to make the netlify/functions/swimapi.mts work
# npx netlify-cli env:set SWIM_API_URL "https://your-swim-api-url"
# Talk to KWB about the credentials
SWIM_API_USER=abc
SWIM_API_PASSWORD=def
SWIM_API_URL=http://your-swim-api-url
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ TODO.md
/public/build/
.DS_Store
.coverage
.env
.env
# Local Netlify folder
.netlify
27 changes: 13 additions & 14 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
const fetchMock = require('fetch-mock');
fetchMock.config.sendAsJson = false;
const fetchMock = require('fetch-mock')
fetchMock.config.sendAsJson = false

const fs = require('fs')

global.__global = {
env: {

URL: "http://localhost:5000",
isProd: false,
URL: 'http://localhost:5000',
isProd: false
// ...require('dotenv').config().parsed
}
}

fetchMock.get(
'https://flsshygn-lageso-berlin-prediction-merge-dev.s3.eu-central-1.amazonaws.com/app/data.csv',
fs.readFileSync(__dirname + '/src/__tests__/data/data.csv', 'utf8')
)
'http://localhost:5000/.netlify/functions/swimapi',
fs.readFileSync(__dirname + '/src/__tests__/data/data.json', 'utf8')
)

fetchMock.get(
'http://localhost:5000/assets/data/new_build.csv',
fs.readFileSync(__dirname + '/src/__tests__/data/new_build.csv', 'utf8')
)
)

jest.mock('mapbox-gl/dist/mapbox-gl', () => {
return {
'default': {
default: {
accessToken: '',
GeolocateControl: jest.fn(),
Map: jest.fn(() => ({
addControl: jest.fn(),
on: jest.fn(),
remove: jest.fn(),
fitBounds: jest.fn(),
flyTo: jest.fn(),
flyTo: jest.fn()
})),
NavigationControl: jest.fn(),
Marker: jest.fn(() => ({
setLngLat: jest.fn(),
addTo: jest.fn(),
})),
addTo: jest.fn()
}))
}
}
});
})
3 changes: 2 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[[headers]]
for = "/*"

[headers.values]
Access-Control-Allow-Origin = "*"
Access-Control-Allow-Origin = "*"
43 changes: 43 additions & 0 deletions netlify/functions/swimapi.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Context } from '@netlify/functions'

export default async (req: Request, context: Context) => {
const username = Netlify.env.get('SWIM_API_USER')
const password = Netlify.env.get('SWIM_API_PASSWORD')
const url = Netlify.env.get('SWIM_API_URL')
if (!username || !password || !url) {
return new Response('Missing required environment variables')
}
const auth = `Basic ${btoa(`${username}:${password}`)}`
const options = {
method: 'GET',
headers: {
Authorization: auth
}
}

try {
const response = await fetch(url, options)
if (!response.ok) {
return new Response('Failed to fetch data from Swim API', {
status: response.status
})
}
const data = await response.json()
const alteredData = data.map((m: any) => {
const { id, badestellen_id } = m
const item = { ...m, id: badestellen_id, swim_id: id }
return item
})
return new Response(JSON.stringify(alteredData), {
headers: {
'Access-Control-Allow-Origin': '*', // Allow from anywhere
'Access-Control-Allow-Methods': 'GET', // Allow these HTTP methods
'Access-Control-Allow-Headers': 'Content-Type', // Allow Content-Type header
'content-type': 'application/json'
}
})
} catch (error) {
console.error(error)
return new Response('Failed to fetch data from Swim API', { status: 500 })
}
}
Loading

0 comments on commit 724bfd9

Please sign in to comment.