Merge pull request #19 from oceanprotocol/fix/bounties-refactor

rewrite bounty task
This commit is contained in:
Matthias Kretschmann 2018-10-24 09:32:44 +02:00 committed by GitHub
commit b9cd283ab6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 42 deletions

View File

@ -69,16 +69,12 @@ https://TASK_URL/TASK_NAME/
**Response** **Response**
Response is structured by network and fills it with whatever comes back from respective API: Response is structured by network and returns the count of open bounties:
```json ```json
{ {
"gitcoin": [ "gitcoin": 1,
{...} "bountiesNetwork": 2
],
"bountiesNetwork": [
{...}
]
} }
``` ```

View File

@ -3,13 +3,15 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "wt serve webtask-mailchimp.js", "start": "wt serve webtask-bounties.js",
"test": "eslint ./*.js" "test": "eslint ./*.js"
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"cors": "^2.8.4", "cors": "^2.8.4",
"express": "^4.16.4", "express": "^4.16.4",
"ms": "^2.1.1",
"request": "^2.88.0", "request": "^2.88.0",
"webtask-tools": "^3.4.0" "webtask-tools": "^3.4.0"
}, },

View File

@ -1,50 +1,52 @@
'use strict' // eslint-disable-line 'use latest'
const express = require('express') const express = require('express')
const request = require('request') const axios = require('axios')
const webtask = require('webtask-tools') const webtask = require('webtask-tools')
const regeneratorRuntime = require('regenerator-runtime') // eslint-disable-line
const app = express() const server = express()
let cached async function getGitcoin() {
try {
const response = await axios.get('https://gitcoin.co/api/v0.1/bounties/')
const getData = res => { const gitcoinBody = response.data // returns only open bounties by default
// Just chained callbacks in lack of proper async/await support on webtask.io
// Gitcoin bounties
request('https://gitcoin.co/api/v0.1/bounties/', (error, response, body) => {
if (error) return error
const gitcoinBody = JSON.parse(body) // returns only open bounties by default
const gitcoin = gitcoinBody.filter( const gitcoin = gitcoinBody.filter(
// filter the response manually, no way atm to do that as API query // filter the response manually, no way atm to do that as API query
item => item.funding_organisation.includes('Ocean Protocol') item => item.funding_organisation.includes('Ocean Protocol')
) )
let holder = {} const data = { gitcoin: gitcoin.length }
holder.gitcoin = gitcoin return data
} catch (error) {
// Bounties.network bounties console.error(`Error: ${error.reason}`) // eslint-disable-line no-console
request('https://new.api.bounties.network/bounty/?search=ocean%20protocol&bountyStage=1&platform=bounties-network', (error, response, body) => { }
if (error) return
const bountiesNetwork = JSON.parse(body)
holder.bountiesNetwork = bountiesNetwork.results
cached = holder
// Send final response
res.send(holder)
})
})
} }
app.get('/', (req, res) => { async function getBountiesNetwork() {
if (cached) { try {
res.send(cached) const response = await axios.get('https://api.bounties.network/bounty/?search=ocean%20protocol&bountyStage=1&platform=bounties-network')
} else {
getData(res) const bountiesNetwork = response.data
const data = { bountiesNetwork: bountiesNetwork.results.length }
return data
} catch (error) {
console.error(`Error: ${error.reason}`) // eslint-disable-line no-console
}
}
server.get('/', async (req, res) => {
try {
const dataGitcoin = await getGitcoin()
const dataBountiesNetwork = await getBountiesNetwork()
const data = Object.assign(dataGitcoin, dataBountiesNetwork)
res.send(data)
} catch (error) {
res.send(error)
} }
}) })
module.exports = webtask.fromExpress(app) module.exports = webtask.fromExpress(server)