Add FarmerAPI class to Worker
This commit is contained in:
parent
10f75b5d0d
commit
877287574b
@ -21,6 +21,7 @@
|
||||
"body-parser": "^1.15.2",
|
||||
"express": "^4.14.0",
|
||||
"express-liquid": "^0.2.6",
|
||||
"request": "^2.79.0",
|
||||
"sequelize": "^3.28.0",
|
||||
"sqlite3": "^3.1.8"
|
||||
}
|
||||
|
31
service/farmerapi.js
Normal file
31
service/farmerapi.js
Normal file
@ -0,0 +1,31 @@
|
||||
"use strict"
|
||||
|
||||
var request = require("request")
|
||||
var API_URL = "http://localhost:8080"
|
||||
|
||||
class FarmerAPI {
|
||||
getWork() {
|
||||
return new Promise(function(resolve, reject) {
|
||||
request.get({
|
||||
url: API_URL + '/workers/get-work',
|
||||
qs: {
|
||||
token: "foobar"
|
||||
},
|
||||
json: true,
|
||||
// auth: {
|
||||
// username: me.username,
|
||||
// password: me.password,
|
||||
// sendImmediately: true
|
||||
// }
|
||||
}, function (error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
resolve(body.work)
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FarmerAPI
|
@ -1 +1,13 @@
|
||||
"use strict"
|
||||
|
||||
require("process").chdir(__dirname)
|
||||
|
||||
const FarmerAPI = require("./farmerapi")
|
||||
const api = new FarmerAPI()
|
||||
|
||||
api.getWork().then(function(r) {
|
||||
console.log("r: ")
|
||||
console.log(r);
|
||||
}).catch(function(e) {
|
||||
console.log("e" + e)
|
||||
})
|
||||
|
@ -11,25 +11,31 @@ router.get("/get-work", function(req, res) {
|
||||
token: worker_token
|
||||
}
|
||||
}).then(function(worker) {
|
||||
db.Work.findAll({
|
||||
limit: 1,
|
||||
where: {
|
||||
workerId: null
|
||||
}
|
||||
}).then(function(jobs) {
|
||||
if (jobs && jobs.length == 1) {
|
||||
var job = jobs[0]
|
||||
job.workerId = worker.id
|
||||
job.save()
|
||||
res.send({
|
||||
author: job.author,
|
||||
basename: job.basename,
|
||||
type: job.work_type
|
||||
})
|
||||
} else {
|
||||
res.send({})
|
||||
}
|
||||
})
|
||||
if (worker) {
|
||||
db.Work.findAll({
|
||||
limit: 1,
|
||||
where: {
|
||||
workerId: null
|
||||
}
|
||||
}).then(function(jobs) {
|
||||
if (jobs && jobs.length == 1) {
|
||||
var job = jobs[0]
|
||||
job.workerId = worker.id
|
||||
job.save()
|
||||
res.send({
|
||||
work: {
|
||||
author: job.author,
|
||||
basename: job.basename,
|
||||
type: job.work_type
|
||||
}
|
||||
})
|
||||
} else {
|
||||
res.send({})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
res.status(401).status("Unauthorized")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user