Skip to content

Commit

Permalink
added podcast endpoints (#34)
Browse files Browse the repository at this point in the history
* Add podcast endpoints

* added podcast endpoints
  • Loading branch information
cultpodcasts authored Aug 5, 2024
1 parent 852cd36 commit a05c574
Show file tree
Hide file tree
Showing 4 changed files with 1,100 additions and 4 deletions.
1 change: 1 addition & 0 deletions homepage.json

Large diffs are not rendered by default.

81 changes: 77 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Env = {
secureEpisodeEndpoint: URL;
secureDiscoveryCurationEndpoint: URL;
securePodcastIndexEndpoint: URL;
securePodcastEndpoint: URL;
}

const allowedOrigins: Array<string> = [
Expand Down Expand Up @@ -416,6 +417,7 @@ app.get("/episode/:id", auth0Middleware, async (c) => {
return new Response(resp.body);
} else {
console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
Expand All @@ -433,7 +435,7 @@ app.post("/episode/:id", auth0Middleware, async (c) => {
const authorisation: string = c.req.header("Authorization")!;
const url = `${c.env.secureEpisodeEndpoint}/${id}`;
const data: any = await c.req.json();
const body: string = JSON.stringify(data)
const body: string = JSON.stringify(data);
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
Expand All @@ -451,7 +453,79 @@ app.post("/episode/:id", auth0Middleware, async (c) => {
return new Response(resp.body);
} else {
console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Unauthorised" }, 500);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
});

app.get("/podcast/:name", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const name = c.req.param('name')
c.header("Cache-Control", "max-age=600");
c.header("Content-Type", "application/json");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "POST,GET,OPTIONS");

if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
console.log(`Using auth header '${authorisation.slice(0, 20)}..'`);
const url = `${c.env.securePodcastEndpoint}/${encodeURIComponent(name)}`;
console.log(url);
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.securePodcastEndpoint).host
},
method: "GET"
});
if (resp.status == 200) {
console.log(`Successfully used secure-podcast-endpoint.`);

return new Response(resp.body);
} else {
console.log(`Failed to use secure-podcast-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
});

app.post("/podcast/:id", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id')
c.header("Cache-Control", "max-age=600");
c.header("Content-Type", "application/json");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "POST,GET,OPTIONS");

if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
const url = `${c.env.securePodcastEndpoint}/${id}`;
const data: any = await c.req.json();
const body: string = JSON.stringify(data)
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.securePodcastEndpoint).host
},
method: "POST",
body: body
});
if (resp.status == 202) {
console.log(`Successfully used secure-podcast-endpoint.`);
return new Response(resp.body);
} else {
console.log(`Failed to use secure-podcast-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
Expand All @@ -468,9 +542,8 @@ app.post("/podcast/index/:name", auth0Middleware, async (c) => {
if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
const url = `${c.env.securePodcastIndexEndpoint}/${encodeURIComponent(name)}`;
console.log("URl is "+url);
const data: any = await c.req.json();
const body: string = JSON.stringify(data)
const body: string = JSON.stringify(data)
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
Expand Down
Loading

0 comments on commit a05c574

Please sign in to comment.