Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed body-parser #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const createRequest = require('./index').createRequest
const createRequest = require("./index").createRequest;

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.EA_PORT || 8080
const express = require("express");
const app = express();
const port = process.env.EA_PORT || 8080;

app.use(bodyParser.json())
app.use(exress.json());

app.post('/', (req, res) => {
console.log('POST Data: ', req.body)
app.post("/", (req, res) => {
console.log("POST Data: ", req.body);
createRequest(req.body, (status, result) => {
console.log('Result: ', result)
res.status(status).json(result)
})
})
console.log("Result: ", result);
res.status(status).json(result);
});
});

app.listen(port, () => console.log(`Listening on port ${port}!`))
app.listen(port, () => console.log(`Listening on port ${port}!`));
76 changes: 39 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,78 @@
const { Requester, Validator } = require('@chainlink/external-adapter')
const { Requester, Validator } = require("@chainlink/external-adapter");

// Define custom error scenarios for the API.
// Return true for the adapter to retry.
const customError = (data) => {
if (data.Response === 'Error') return true
return false
}
if (data.Response === "Error") return true;
return false;
};

// Define custom parameters to be used by the adapter.
// Extra parameters can be stated in the extra object,
// with a Boolean value indicating whether or not they
// should be required.
const customParams = {
base: ['base', 'from', 'coin'],
quote: ['quote', 'to', 'market'],
endpoint: false
}
base: ["base", "from", "coin"],
quote: ["quote", "to", "market"],
endpoint: false,
};

const createRequest = (input, callback) => {
// The Validator helps you validate the Chainlink request data
const validator = new Validator(callback, input, customParams)
const jobRunID = validator.validated.id
const endpoint = validator.validated.data.endpoint || 'price'
const url = `https://min-api.cryptocompare.com/data/${endpoint}`
const fsym = validator.validated.data.base.toUpperCase()
const tsyms = validator.validated.data.quote.toUpperCase()
const validator = new Validator(callback, input, customParams);
const jobRunID = validator.validated.id;
const endpoint = validator.validated.data.endpoint || "price";
const url = `https://min-api.cryptocompare.com/data/${endpoint}`;
const fsym = validator.validated.data.base.toUpperCase();
const tsyms = validator.validated.data.quote.toUpperCase();

const params = {
fsym,
tsyms
}
tsyms,
};

// This is where you would add method and headers
// you can add method like GET or POST and add it to the config
// The default is GET requests
// method = 'get'
// method = 'get'
// headers = 'headers.....'
const config = {
url,
params
}
params,
};

// The Requester allows API calls be retry in case of timeout
// or connection failure
Requester.request(config, customError)
.then(response => {
.then((response) => {
// It's common practice to store the desired value at the top-level
// result key. This allows different adapters to be compatible with
// one another.
response.data.result = Requester.validateResultNumber(response.data, [tsyms])
callback(response.status, Requester.success(jobRunID, response))
response.data.result = Requester.validateResultNumber(response.data, [
tsyms,
]);
callback(response.status, Requester.success(jobRunID, response));
})
.catch(error => {
callback(500, Requester.errored(jobRunID, error))
})
}
.catch((error) => {
callback(500, Requester.errored(jobRunID, error));
});
};

// This is a wrapper to allow the function to work with
// GCP Functions
exports.gcpservice = (req, res) => {
createRequest(req.body, (statusCode, data) => {
res.status(statusCode).send(data)
})
}
res.status(statusCode).send(data);
});
};

// This is a wrapper to allow the function to work with
// AWS Lambda
exports.handler = (event, context, callback) => {
createRequest(event, (statusCode, data) => {
callback(null, data)
})
}
callback(null, data);
});
};

// This is a wrapper to allow the function to work with
// newer AWS Lambda implementations
Expand All @@ -79,11 +81,11 @@ exports.handlerv2 = (event, context, callback) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
})
})
}
isBase64Encoded: false,
});
});
};

// This allows the function to be exported for testing
// or for running in express
module.exports.createRequest = createRequest
module.exports.createRequest = createRequest;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@chainlink/external-adapter": "^0.2.3"
},
"devDependencies": {
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"eslint": "^7.0.0",
"eslint-config-standard": "^14.1.1",
Expand Down
104 changes: 64 additions & 40 deletions test/index_test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,75 @@
const assert = require('chai').assert
const createRequest = require('../index.js').createRequest
const assert = require("chai").assert;
const createRequest = require("../index.js").createRequest;

describe('createRequest', () => {
const jobID = '1'
describe("createRequest", () => {
const jobID = "1";

context('successful calls', () => {
context("successful calls", () => {
const requests = [
{ name: 'id not supplied', testData: { data: { base: 'ETH', quote: 'USD' } } },
{ name: 'base/quote', testData: { id: jobID, data: { base: 'ETH', quote: 'USD' } } },
{ name: 'from/to', testData: { id: jobID, data: { from: 'ETH', to: 'USD' } } },
{ name: 'coin/market', testData: { id: jobID, data: { coin: 'ETH', market: 'USD' } } }
]
{
name: "id not supplied",
testData: { data: { base: "ETH", quote: "USD" } },
},
{
name: "base/quote",
testData: { id: jobID, data: { base: "ETH", quote: "USD" } },
},
{
name: "from/to",
testData: { id: jobID, data: { from: "ETH", to: "USD" } },
},
{
name: "coin/market",
testData: { id: jobID, data: { coin: "ETH", market: "USD" } },
},
];

requests.forEach(req => {
requests.forEach((req) => {
it(`${req.name}`, (done) => {
createRequest(req.testData, (statusCode, data) => {
assert.equal(statusCode, 200)
assert.equal(data.jobRunID, jobID)
assert.isNotEmpty(data.data)
assert.isAbove(Number(data.result), 0)
assert.isAbove(Number(data.data.result), 0)
done()
})
})
})
})
assert.equal(statusCode, 200);
assert.equal(data.jobRunID, jobID);
assert.isNotEmpty(data.data);
assert.isAbove(Number(data.result), 0);
assert.isAbove(Number(data.data.result), 0);
done();
});
});
});
});

context('error calls', () => {
context("error calls", () => {
const requests = [
{ name: 'empty body', testData: {} },
{ name: 'empty data', testData: { data: {} } },
{ name: 'base not supplied', testData: { id: jobID, data: { quote: 'USD' } } },
{ name: 'quote not supplied', testData: { id: jobID, data: { base: 'ETH' } } },
{ name: 'unknown base', testData: { id: jobID, data: { base: 'not_real', quote: 'USD' } } },
{ name: 'unknown quote', testData: { id: jobID, data: { base: 'ETH', quote: 'not_real' } } }
]
{ name: "empty body", testData: {} },
{ name: "empty data", testData: { data: {} } },
{
name: "base not supplied",
testData: { id: jobID, data: { quote: "USD" } },
},
{
name: "quote not supplied",
testData: { id: jobID, data: { base: "ETH" } },
},
{
name: "unknown base",
testData: { id: jobID, data: { base: "not_real", quote: "USD" } },
},
{
name: "unknown quote",
testData: { id: jobID, data: { base: "ETH", quote: "not_real" } },
},
];

requests.forEach(req => {
requests.forEach((req) => {
it(`${req.name}`, (done) => {
createRequest(req.testData, (statusCode, data) => {
assert.equal(statusCode, 500)
assert.equal(data.jobRunID, jobID)
assert.equal(data.status, 'errored')
assert.isNotEmpty(data.error)
done()
})
})
})
})
})
assert.equal(statusCode, 500);
assert.equal(data.jobRunID, jobID);
assert.equal(data.status, "errored");
assert.isNotEmpty(data.error);
done();
});
});
});
});
});