Skip to content

Commit

Permalink
Add context retrieval for reads via books-mf2
Browse files Browse the repository at this point in the history
Using the service books-mf2, we can retrieve the context for read data.

As it's a Heroku-based app, there's the risk that this can timeout, so
we need to increase the timeouts available for context retrieval events.
  • Loading branch information
jamietanna committed Nov 16, 2021
1 parent 4c0e8ce commit 2748f2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/events/fetch-context/books-mf2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fetch = require('node-fetch')
const logger = require('@architect/shared/logger')

function name () {
return 'Books-MF2'
}

function isBooksMf2Url (url) {
return (url.indexOf('https://books-mf2.herokuapp.com/') > -1)
}

async function fetchContext (url) {
if (!isBooksMf2Url(url)) {
return
}
const response = await fetch(url)
if (!response.ok) {
const text = await response.text()
logger.warn('Failed to fetch context from Books-MF2', `${url}\n${text}`)
return
}
const mf2 = await response.json()
if (!('items' in mf2) || !mf2.items.length) return
return mf2.items[0].properties
}

module.exports = {
name,
isBooksMf2Url,
fetchContext
}
2 changes: 2 additions & 0 deletions src/events/fetch-context/config.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@aws
timeout 15
3 changes: 3 additions & 0 deletions src/events/fetch-context/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const arc = require('@architect/functions')
const logger = require('@architect/shared/logger')
const booksMf2 = require('./books-mf2')
const eventbrite = require('./eventbrite')
const granary = require('./granary')
const meetup = require('./meetup')
Expand All @@ -10,6 +11,8 @@ async function getHandler (url) {
return meetup
} else if (eventbrite.isEventbriteUrl(url)) {
return eventbrite
} else if (booksMf2.isBooksMf2Url(url)) {
return booksMf2
} else {
return granary
}
Expand Down

0 comments on commit 2748f2b

Please sign in to comment.