Skip to content

Commit

Permalink
Basic framework to make justlisten endpoint work.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenbinf committed Jun 14, 2023
1 parent 4237f6e commit b70d0d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
23 changes: 23 additions & 0 deletions edge-src/common/ListenApiManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { ClientForWorkers } = require('podcast-api');

export default class ListenApiManager {
constructor(context) {
const {env} = context
this.client = ClientForWorkers({
apiKey: env.LISTEN_API_KEY || null,
})
}

_getResponse(resultJson) {
return new Response(JSON.stringify(resultJson), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
})
}

async justListen(transformResultFunc) {
const res = await this.client.justListen()
return this._getResponse(transformResultFunc(res.data))
}
}
30 changes: 19 additions & 11 deletions functions/api/v2/just_listen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const { ClientForWorkers } = require('podcast-api');
import ListenApiManager from '../../../edge-src/common/ListenApiManager.js'

export async function onRequestGet(context) {
const {env} = context
const client = ClientForWorkers({
apiKey: env.LISTEN_API_KEY || null,
})
const resultJson = await client.justListen()
return new Response(JSON.stringify(resultJson), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
})
const mgr = new ListenApiManager(context)
return await mgr.justListen((result) => ({
title: result.title,
description: result.description,
image: result.image,
audio: result.audio,
audio_length_sec: result.audio_length_sec,
pub_date_ms: result.pub_date_ms,
listennotes_url: result.listennotes_url,
podcast: {
title: result.podcast.title,
publisher: result.podcast.publisher,
image: result.podcast.image,
listen_score: result.podcast.listen_score,
listen_score_global_rank: result.podcast.listen_score_global_rank,
listennotes_url: result.podcast.listennotes_url,
},
}))
}

0 comments on commit b70d0d3

Please sign in to comment.