The source code to the civic tech project Öppna Skolplattformen.
Go to file
Viktor Sarström 2823547804 fix: 🐛 Moved topologykey to config, also added getHeader() 2021-09-09 08:32:13 +02:00
.github/workflows Update release.yml 2021-09-08 21:40:18 +02:00
.vscode Feat/improve local dev experience (#97) 2021-03-28 20:56:04 +02:00
lib fix: 🐛 Moved topologykey to config, also added getHeader() 2021-09-09 08:32:13 +02:00
.babelrc.js Initial commit 2020-12-19 13:37:45 +01:00
.eslintrc.js chore: add prettier (#101) 2021-03-30 17:21:18 +02:00
.gitignore Fetch wrapper (#29) 2020-12-30 14:39:49 +01:00
.prettierrc chore: add prettier (#101) 2021-03-30 17:21:18 +02:00
.releaserc Login token, logout method and set session cookie (#6) 2020-12-21 09:51:51 +01:00
LICENSE Initial commit 2020-12-19 11:37:37 +01:00
README.md docs: ✏️ Fixed misspelled method 2021-04-09 10:43:26 +02:00
agentFetchWrapper.js chore: add prettier (#101) 2021-03-30 17:21:18 +02:00
config.json fix: 🐛 Moved topologykey to config, also added getHeader() 2021-09-09 08:32:13 +02:00
devrun.js chore: add prettier (#101) 2021-03-30 17:21:18 +02:00
jest.config.js chore: add prettier (#101) 2021-03-30 17:21:18 +02:00
package.json Use base-64 npm package for encoding 2021-09-08 21:46:05 +02:00
run.js fix: 🐛 Force release (#115) 2021-04-12 13:38:49 +02:00
test.md feat: 🎸 Switched to Markdown Extra converter (#58) 2021-02-11 14:30:42 +01:00
tsconfig.eslint.json feat: 🎸 Names from curriculum (#116) 2021-04-13 14:15:01 +02:00
tsconfig.json fix: 🐛 Parse old aspnet dates instead of unreliable format strings (#108) 2021-04-05 20:43:02 +02:00
yarn.lock Merge pull request #130 from kolplattformen/dependabot/npm_and_yarn/glob-parent-5.1.2 2021-09-08 23:43:30 +02:00

README.md

embedded-api

Since the proxy was blocked (and also deemed a bad idea by some), this is a reboot of the API running in process in the app(s).

Installing

npm i -S @skolplattformen/embedded-api or yarn add @skolplattformen/embedded-api

Calling

Import and init

Since fetch and cookies behave distinctly different in node, react-native and the browser, the concrete implementation of fetch and cookie handler must be injected.

react-native

import init from '@skolplattformen/embedded-api'
import CookieManager from '@react-native-community/cookies'

const api = init(fetch, () => CookieManager.clearAll())

node

import init from '@skolplattformen/embedded-api'
import nodeFetch from 'node-fetch'
import fetchCookie from 'fetch-cookie/node-fetch'
import { CookieJar } from 'tough-cookie'

const cookieJar = new CookieJar()
const fetch = fetchCookie(nodeFetch, cookieJar)

const api = init(fetch, cookieJar)

Login / logout

api.on('login', async () => {
  // do stuff
  console.log(api.isLoggedIn) // true
  await api.logout()
})
api.on('logout', () => {
  // handle logout
  console.log(api.isLoggedIn) // false
}

const loginStatus = await api.login('YYYYMMDDXXXX')
window.open(
  `https://app.bankid.com/?autostarttoken=${loginStatus.token}&redirect=null`
)

loginStatus.on('PENDING', () => console.log('BankID app not yet opened'))
loginStatus.on('USER_SIGN', () => console.log('BankID app is open'))
loginStatus.on('ERROR', () => console.log('Something went wrong'))
loginStatus.on('OK', () =>
  console.log('BankID sign successful. Session will be established.')
)

Loading data

// Get current user
const user = await api.getUser()

// List children from Etjanster
const children = await api.getChildren()

// Get school calendar
const calendar = await api.getCalendar(children[0])

// Get classmates - disabled for reasons
// const classmates = await api.getClassmates(children[0])

// Get student's personal schedule
import { DateTime } from 'luxon'

const from = DateTime.local()
const to = DateTime.local().plus({ week: 1 })
const schedule = await api.getSchedule(children[0], from, to)

// Get news
const news = await api.getNews(children[0])

// Get news details
const newsDetails = await api.getNewsDetails(children[0], news[0])

// Get menu
const menu = await api.getMenu(children[0])

// Get notifications
const notifications = await api.getNotifications(children[0])

// Get list of children from Skola24 (because of course it's different *DERP*)
const skola24Children = await getSkola24Children()

// Get timetable
const weekNumber = 15
const year = 2021
const timetable = await api.getTimetable(skola24Children[0], weekNumber, year)

It is possible to resurrect a logged in session by manually setting the session cookie.

const sessionCookie = 'some value'

api.setSessionCookie(sessionCookie) // will trigger `on('login')` event and set `.isLoggedIn = true`

Fake user

Login with personal number 12121212121212, 201212121212 or 1212121212 and api will be put into fake mode. Static data will be returned and no calls to backend will be made.

The LoginStatusChecker returned by the login method will have .token set to 'fake'.

Try it out

  1. Clone and enter repo: git clone git@github.com:kolplattformen/embedded-api.git && cd embedded-api
  2. Install dependencies: yarn
  3. Build package: yarn build
  4. Run example: node run [your personal number]
  5. Sign in with mobile BankID