Skip to content

Commit

Permalink
feat: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Aug 11, 2023
1 parent d3e7651 commit 55840ff
Show file tree
Hide file tree
Showing 7 changed files with 3,272 additions and 3,118 deletions.
59 changes: 59 additions & 0 deletions client/agents/general/general-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export { GeneralAgent, periodPresets }

Check failure on line 1 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`

Check failure on line 2 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
const paths = {

Check failure on line 3 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
SEARCH: '/api/search'

Check failure on line 4 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
}

Check failure on line 5 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`

Check failure on line 6 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
const REQUESTS = {

Check failure on line 7 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
CLIP_SEARCH: {

Check failure on line 8 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
path: paths.SEARCH,

Check failure on line 9 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
params: {

Check failure on line 10 in client/agents/general/general-agent.js

View workflow job for this annotation

GitHub Actions / Build, lint and test

Insert `␍`
TITLE: 'title',
CREATED: 'created'
}
}
}

const periodPresets = ['TODAY', 'LAST_7_DAYS', 'LAST_30_DAYS', 'LAST_365_DAYS']

class GeneralAgent {
/**
* Create an agent.
*
* @param {string} host - Address to the Quantel server to query
*/

constructor(host) {
this.host = host
}

/**
* Search for clips matching the given criteria.
*
* Special note on the created criteria: use one of the exported `periodPresets`
*
* @param {object} criteria - query criteria
* @param {string} criteria.title - clip title criteria. * is allowed as a wildcard
* @param {string} criteria.created - scope the search to clips created in a specific period
*
* @returns {Promise} - a promise containing the search results
*/

searchClip(criteria) {
const { path, params } = REQUESTS.CLIP_SEARCH

const url = new URL(this.host)
url.pathname = url.pathname + path
url.searchParams.append(params.TITLE, criteria?.title)
url.searchParams.append(params.CREATED, criteria?.created)

return fetch(url.href).then((response) => {
if (response.ok) {
return response.json()
} else
throw new Error(
`Unable to fetch results: ${response.status} - ${response.statusText}`
)
})
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
"koa": "^2.11.0",
"koa-router": "^8.0.8",
"koa-static": "^5.0.0",
"node-fetch": "^2.6.0"
"node-fetch": "^3.3.2",
"xml2json": "^0.12.0"
},
"devDependencies": {
"@sinonjs/referee-sinon": "^6.0.1",
"@types/xml2json": "^0.11.4",
"eslint": "^7.5",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
Expand Down
7 changes: 7 additions & 0 deletions server/agents/create-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { QuantelAgent } from './quantel/quantel-agent.js'

function createSearchAgent(env) {
return new QuantelAgent()
}

export { createSearchAgent }
18 changes: 13 additions & 5 deletions server/agents/quantel/quantel-agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from 'node-fetch'
import { xmlStringToObject } from '../../xml/parser.js'
export { QuantelAgent, periodPresets }
export { QuantelAgent }

const paths = {
SEARCH: 'quantel/homezone/clips/search',
Expand All @@ -15,7 +16,7 @@ const REQUESTS = {
}
}

const periodPresets = {
const PERIOD_PRESETS = {
TODAY: '[NOW-1DAY/DAY TO NOW]',
LAST_7_DAYS: '[NOW-7DAY/DAY TO NOW]',
LAST_30_DAYS: '[NOW-30DAY/DAY TO NOW]',
Expand Down Expand Up @@ -56,9 +57,16 @@ class QuantelAgent {
const url = new URL(this.host)
url.pathname = url.pathname + path
const queryParamValue = buildQueryParam(
Object.assign({}, criteria, {
poolId: this.poolId
})
Object.assign(
{},
{
title: criteria.title,
created: PERIOD_PRESETS[criteria.created] ?? PERIOD_PRESETS.TODAY
},
{
poolId: this.poolId
}
)
)
url.searchParams.append(params.QUERY, queryParamValue)

Expand Down
27 changes: 27 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const serve = require('koa-static')
const Router = require('koa-router')
const fetch = require('node-fetch')
const PassThrough = require('stream').PassThrough
const { createSearchAgent } = require('./agents/create-agent.js')

const app = new Koa()
const router = new Router()
Expand All @@ -13,6 +14,32 @@ const QUANTEL_TRANSFORMER_URL = process.env.QUANTEL_TRANSFORMER_URL

const packageInfo = require('../package.json')

const agent = createSearchAgent(process.env)

router.get('/api/search', async (ctx, next) => {
ctx.set({
'Content-Type': 'application/json',
'Cache-Control': 'no-store',
'Access-Control-Allow-Origin': '*'
})

try {
ctx.body = JSON.stringify(
await agent.searchClip({
title: ctx.request.query['title'],
created: ctx.request.query['created']
})
)
} catch (e) {
ctx.status = 500
ctx.body = JSON.stringify({ error: `Search failed: ${e}` })

console.error(e)
}

next()
})

router.all('/api/(.*)', async (ctx, next) => {
// console.log(ctx.request.method, ctx.params['0'], ctx.request.querystring);
if (!QUANTEL_TRANSFORMER_URL) {
Expand Down
53 changes: 3 additions & 50 deletions server/xml/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,12 @@
* typically is a browser thing. For server side usage, xml2json is probably
* what you want :)
*/
export { xmlStringToObject, xmlToObject }
import parser from 'xml2json'
export { xmlStringToObject }

/**
* Convenience function for conversion from XML source strings.
*/
function xmlStringToObject(xmlString) {
const domparser = new window.DOMParser()
const doc = domparser.parseFromString(xmlString, 'text/xml')

return xmlToObject(doc)
}

/**
* Returns an object representing the XML documents.
*/
function xmlToObject(doc) {
if (doc.firstChild) {
return {
[doc.firstChild.nodeName]: nodeToObj(doc.firstChild)
}
}

return {}
}

function nodeToObj(node) {
if (node.childNodes) {
const obj = {}

for (const n of node.childNodes) {
const { nodeName } = n

switch (nodeName) {
case '#text':
if (n.textContent && n.textContent.trim() !== '') {
return n.textContent
}
break
default:
if (obj[nodeName]) {
if (Array.isArray(obj[nodeName])) {
obj[nodeName].push(nodeToObj(n))
} else {
obj[nodeName] = [obj[nodeName], nodeToObj(n)]
}
} else {
obj[nodeName] = nodeToObj(n)
}
}
}

return obj
}

return {}
return parser.toJson(xmlString)
}
Loading

0 comments on commit 55840ff

Please sign in to comment.