-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.js
101 lines (90 loc) · 3.11 KB
/
notify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require('dotenv').config()
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
}
// 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 = {
payment_to: true,
payment_from: true,
amount: true,
currency: true
}
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 url = `https://api.github.com/repos/${process.env.OCTOBAY_ORG}/${process.env.OCTOBAY_REPO}/issues/${process.env.NOTIFICATIONS_ISSUE_NUMBER}/comments`
const payment_to = validator.validated.data.payment_to
const payment_from = validator.validated.data.payment_from
const amount = validator.validated.data.amount
const currency = validator.validated.data.currency.toUpperCase()
const body = `@${payment_from} has funded @${payment_to} ${amount} ${currency}. `
const auth = {
username: process.env.OPIN_USERNAME,
password: process.env.OPIN_TOKEN
}
const headers = {
Accept: 'application/vnd.github.v3+json'
}
// 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'
// headers = 'headers.....'
const config = {
method: 'POST',
url,
auth,
headers,
data: { body: body }
}
// The Requester allows API calls be retry in case of timeout
// or connection failure
Requester.request(config, customError)
.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.
statusCode = 200
response.data.result = response.data.id
callback(statusCode, Requester.success(jobRunID, response))
})
.catch(error => {
callback(500, Requester.errored(jobRunID, JSON.stringify(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)
})
}
// 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)
})
}
// This is a wrapper to allow the function to work with
// newer AWS Lambda implementations
exports.handlerv2 = (event, context, callback) => {
createRequest(JSON.parse(event.body), (statusCode, data) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
})
})
}
// This allows the function to be exported for testing
// or for running in express
module.exports.createRequest = createRequest