Skip to content

Commit

Permalink
Add ability to provide custom request headers when fetching schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Dec 19, 2024
1 parent 0594690 commit dd48e31
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/__tests__/backends/json/JsonScheduleBackend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function jsonScheduleServer() {
const json = getFixture("original_democon.json");
res.end(json);
} else if (req.url === "/fosdem/p/matrix") {
if (req.headers.authorization !== "Bearer TOKEN") {
res.writeHead(401);
res.end("Not authorised");
return;
}
res.writeHead(200);
const json = getFixture("fosdem_democon.json");
res.end(json);
Expand Down Expand Up @@ -51,6 +56,9 @@ describe("JsonScheduleBackend", () => {
scheduleDefinition: `http://127.0.0.1:${
(serv.address() as AddressInfo).port
}/fosdem/p/matrix`,
scheduleRequestHeaders: {
"Authorization": "Bearer TOKEN"
}
},
globalConfig
);
Expand Down
4 changes: 3 additions & 1 deletion src/backends/json/JsonScheduleBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export class JsonScheduleBackend implements IScheduleBackend {
let jsonDesc: any;
let cached = false;


const cachedSchedulePath = path.join(dataPath, 'cached_schedule.json');

try {
if (cfg.scheduleDefinition.startsWith("http")) {
const headers = cfg.scheduleRequestHeaders ?? {};
// Fetch the JSON track over the network
jsonDesc = await fetch(cfg.scheduleDefinition).then(r => r.json());
jsonDesc = await fetch(cfg.scheduleDefinition, {headers}).then(r => r.json());
} else {
// Load the JSON from disk
jsonDesc = await readJsonFileAsync(cfg.scheduleDefinition);
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ export interface IJsonScheduleBackendConfig {
* Defaults to original.
*/
scheduleFormat?: JsonScheduleFormat;

/**
* Map of request headers to send when requesting the schedule definition.
* Useful for authenticating requests.
* Required for the FOSDEM-format schedules.
*
* Defaults to no headers.
*/
scheduleRequestHeaders?: {[_: string]: string};
}

export enum PretalxScheduleFormat {
Expand Down

0 comments on commit dd48e31

Please sign in to comment.