Skip to content

Commit

Permalink
feat: 馃幐 H盲mta lektionsschema (#110)
Browse files Browse the repository at this point in the history
* feat: 馃幐 add timetables from skola24

* refactor: 馃挕 Clean up sso authorization

* feat: 馃幐 Reads timetable

* feat: 馃幐 Veckoschema

Veckoschema kan laddas fr氓n Skola24

BREAKING CHANGE: 馃Ж Child -> EtjanstChild

* feat: 馃幐 Test data for skola24Children and timetable

* docs: 鉁忥笍 Updated instructions

Co-authored-by: Johan 脰brink <johan.obrink@gmail.com>
  • Loading branch information
kajetan-kazimierczak and JohanObrink committed Apr 9, 2021
1 parent 3c33c75 commit c288449
Show file tree
Hide file tree
Showing 10 changed files with 912 additions and 52 deletions.
76 changes: 43 additions & 33 deletions README.md
Expand Up @@ -16,69 +16,68 @@ the concrete implementation of fetch and cookie handler must be injected.
#### react-native

```javascript
import init from "@skolplattformen/embedded-api";
import CookieManager from "@react-native-community/cookies";
import init from '@skolplattformen/embedded-api'
import CookieManager from '@react-native-community/cookies'

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

#### node

```javascript
import init from "@skolplattformen/embedded-api";
import nodeFetch from "node-fetch";
import fetchCookie from "fetch-cookie/node-fetch";
import { CookieJar } from "tough-cookie";
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 cookieJar = new CookieJar()
const fetch = fetchCookie(nodeFetch, cookieJar)

const api = init(fetch, () => cookieJar.removeAllCookies());
const api = init(fetch, cookieJar)
```

### Login / logout

```javascript
api.on("login", async () => {
api.on('login', async () => {
// do stuff
console.log(api.isLoggedIn) // true
console.log(api.getSessionCookie) // session cookie if you want to save it
await api.logout()
});
})
api.on('logout', () => {
// handle logout
console.log(api.isLoggedIn) // false
}

const loginStatus = await api.login("YYYYMMDDXXXX");
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.")
);
)

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
```javascript
// Get current user
const user = await api.getUser();
const user = await api.getUser()

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

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

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

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

const from = DateTime.local()
Expand All @@ -88,21 +87,32 @@ 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)
```
### Setting session cookie
It is possible to resurrect a logged in session by manually setting the session cookie.
```javascript
const sessionCookie = "some value";
const sessionCookie = 'some value'

api.setSessionCookie(sessionCookie); // will trigger `on('login')` event and set `.isLoggedIn = true`
api.setSessionCookie(sessionCookie) // will trigger `on('login')` event and set `.isLoggedIn = true`
```
### Fake user
Expand All @@ -111,7 +121,7 @@ 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".
The `LoginStatusChecker` returned by the login method will have `.token` set to 'fake'.
## Try it out
Expand Down
8 changes: 7 additions & 1 deletion lib/api.test.ts
Expand Up @@ -165,7 +165,7 @@ describe('api', () => {
status = await api.login('1212121212')
expect(status.token).toEqual('fake')
})
it.skip('delivers fake data', async (done) => {
it('delivers fake data', async (done) => {
api.on('login', async () => {
const user = await api.getUser()
expect(user).toEqual({
Expand All @@ -181,6 +181,12 @@ describe('api', () => {
const calendar2 = await api.getCalendar(children[1])
expect(calendar2).toHaveLength(18)

const skola24Children = await api.getSkola24Children()
expect(skola24Children).toHaveLength(1)

const timetable = await api.getTimetable(skola24Children[0], 2021, 15)
expect(timetable).toHaveLength(32)

done()
})
await api.login('121212121212')
Expand Down

0 comments on commit c288449

Please sign in to comment.