1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-11-30 15:58:03 +01:00

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 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
{
"gitcoin": [
{...}
],
"bountiesNetwork": [
{...}
]
"gitcoin": 1,
"bountiesNetwork": 2
}
```

View File

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

View File

@ -1,50 +1,52 @@
'use strict' // eslint-disable-line
'use latest'
const express = require('express')
const request = require('request')
const axios = require('axios')
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 => {
// 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 gitcoinBody = response.data // returns only open bounties by default
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')
)
let holder = {}
const data = { gitcoin: gitcoin.length }
holder.gitcoin = gitcoin
// Bounties.network bounties
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)
})
})
return data
} catch (error) {
console.error(`Error: ${error.reason}`) // eslint-disable-line no-console
}
}
app.get('/', (req, res) => {
if (cached) {
res.send(cached)
} else {
getData(res)
async function getBountiesNetwork() {
try {
const response = await axios.get('https://api.bounties.network/bounty/?search=ocean%20protocol&bountyStage=1&platform=bounties-network')
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)