Skip to content

Commit

Permalink
Merge pull request #1 from magebitcom/1.3.0
Browse files Browse the repository at this point in the history
1.3.0 adding caching to request
  • Loading branch information
EmilsM authored Nov 19, 2020
2 parents ebcf952 + 2306e10 commit 20ccdaa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 38 deletions.
118 changes: 80 additions & 38 deletions API/vue-storefront-instagram-api/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,97 @@
import { Router } from 'express'
import request from 'request'
import cache from '../../../lib/cache-instance'
import { sha3_224 } from 'js-sha3'

// This might change in the future, but most libraries use it
const QUERY_ID = '17888483320059182'

function _cacheStorageHandler (config, result, hash, tags) {
if (config.server.useOutputCache && cache) {
cache.set(
'api:' + hash,
result,
tags,
1800 // Caching instagram request for 30 minutes
).catch((err) => {
console.error(err)
})
}
}

module.exports = ({ config }) => {
const api = Router();

api.get('/feed', (req, res) => {
const { limit, width, height } = req.query;
const { profile, id } = config.extensions.instagram;
const url = `https://www.instagram.com/graphql/query/?query_id=${QUERY_ID}&variables={"id":${id},"first":${limit},"after":null}`

request(
{
url,
json: true
},
(error, response, body) => {
let apiResult;
const errorResponse = error || body.error;
const errorInQuery = !width || !height

if (errorResponse || errorInQuery) {
apiResult = { code: 500, result: errorResponse || 'Please provide height and width for resize < &height=&width= >' }
} else {
let resultObj = {}

Object.assign(resultObj, {
'username': profile,
'items': []
})

body.data.user.edge_owner_to_timeline_media.edges.forEach((item) => {
const captionEdges = item.node.edge_media_to_caption.edges
resultObj.items.push({
'image': `${config.server.url || ''}/img/?url=${encodeURIComponent(item.node.thumbnail_src + '&mime=.jpg')}&width=${width}&height=${height}&action=fit`,
'image_hq': item.node.display_url,
'caption': captionEdges.length ? captionEdges[0].node.text : null,
'permalink': `https://instagram.com/p/${item.node.shortcode}`,
'like_count': item.node.edge_media_preview_like.count,
'id': item.node.id
const reqHash = sha3_224(`${JSON.stringify(req.body)}${req.url}`)
const s = Date.now()

const dynamicRequestHandler = () => {
const {limit, width, height} = req.query;
const {profile, id} = config.extensions.instagram;
const url = `https://www.instagram.com/graphql/query/?query_id=${QUERY_ID}&variables={"id":${id},"first":${limit},"after":null}`

request(
{
url,
json: true
},
(error, response, body) => {
let apiResult;
const errorResponse = error || body.error;
const errorInQuery = !width || !height

if (errorResponse || errorInQuery) {
apiResult = {
code: 500,
result: errorResponse || 'Please provide height and width for resize < &height=&width= >'
}
} else {
let resultObj = {}

Object.assign(resultObj, {
'username': profile,
'items': []
})

body.data.user.edge_owner_to_timeline_media.edges.forEach((item) => {
const captionEdges = item.node.edge_media_to_caption.edges
resultObj.items.push({
'image': `${config.server.url || ''}/img/?url=${encodeURIComponent(item.node.thumbnail_src + '&mime=.jpg')}&width=${width}&height=${height}&action=fit`,
'image_hq': item.node.display_url,
'caption': captionEdges.length ? captionEdges[0].node.text : null,
'permalink': `https://instagram.com/p/${item.node.shortcode}`,
'like_count': item.node.edge_media_preview_like.count,
'id': item.node.id
})
})
})

apiResult = { code: 200, result: resultObj }
apiResult = {code: 200, result: resultObj}
_cacheStorageHandler(config, apiResult, reqHash, ['instagram'])
}

res.status(apiResult.code).json(apiResult)
}
)
}

res.status(apiResult.code).json(apiResult)
}
)
if (config.server.useOutputCache && cache) {
cache.get(
'api:' + reqHash
).then(output => {
if (output !== null) {
res.setHeader('X-VS-Cache', 'Hit')
res.status(output.code).json(output)
console.log(`cache hit [${req.url}], cached request: ${Date.now() - s}ms`)
} else {
res.setHeader('X-VS-Cache', 'Miss')
console.log(`cache miss [${req.url}], request: ${Date.now() - s}ms`)
dynamicRequestHandler()
}
}).catch(err => console.error(err))
} else {
dynamicRequestHandler()
}
})

return api
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

#### [1.3.0] - 2020-19-11

##### Updated
- Added caching to vsf-api request


#### [1.2.0] - 2020-15-07

##### Updated
Expand Down

0 comments on commit 20ccdaa

Please sign in to comment.