Skip to content

Commit

Permalink
add netlify function to get discourse posts
Browse files Browse the repository at this point in the history
  • Loading branch information
JKarlavige committed Sep 13, 2022
1 parent 975ebc8 commit 3fde615
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ website/build/
website/yarn.lock
website/node_modules
website/i18n/*

# Local Netlify folder
.netlify
2 changes: 2 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
functions = "functions"
45 changes: 45 additions & 0 deletions website/functions/get-discourse-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const axios = require('axios')

async function getDiscoursePosts() {
const { DISCOURSE_API_KEY , DISCOURSE_USER } = process.env

try {
// Set API endpoint and headers
let discourse_endpoint = `https://discourse.getdbt.com`
let headers = {
'Accept': 'application/json',
'Api-Key': DISCOURSE_API_KEY,
'Api-Username': DISCOURSE_USER,
}

// Get events from Discourse
let { data } = await axios.get(`${discourse_endpoint}/posts`, { headers })
console.log('data', data)
if(!data)
throw new Error('Unable to get results from api request.')

// Return posts
return await returnResponse(200, data)
} catch(err) {
// Log and return the error
console.log('err', err)
return await returnResponse(500, { error: 'Unable to get events from Discourse.'})
}
}

async function returnResponse(status, res) {
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS'
}
const resObj = {
statusCode: status,
headers,
body: JSON.stringify(res)
}
return resObj
}

exports.handler = getDiscoursePosts
56 changes: 47 additions & 9 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@docusaurus/theme-search-algolia": "2.0.0-beta.17",
"@mdx-js/react": "^1.6.21",
"@svgr/webpack": "^5.5.0",
"axios": "^0.27.2",
"classnames": "^2.3.1",
"clsx": "^1.1.1",
"color": "^3.1.2",
Expand Down

0 comments on commit 3fde615

Please sign in to comment.