Consolidate Cloudflare API in single module

This commit is contained in:
MICHAEL JACKSON 2017-08-12 09:14:46 -07:00
parent be4171ffff
commit cc737f6a9d
3 changed files with 61 additions and 66 deletions

38
server/CloudflareAPI.js Normal file
View File

@ -0,0 +1,38 @@
require('isomorphic-fetch')
const invariant = require('invariant')
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
const CloudflareKey = process.env.CLOUDFLARE_KEY
invariant(
CloudflareEmail,
'Missing the $CLOUDFLARE_EMAIL environment variable'
)
invariant(
CloudflareKey,
'Missing the $CLOUDFLARE_KEY environment variable'
)
function get(path, headers) {
return fetch(`https://api.cloudflare.com/client/v4${path}`, {
method: 'GET',
headers: Object.assign({}, headers, {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey
})
})
}
function getJSON(path, headers) {
return get(path, headers).then(function (res) {
return res.json()
}).then(function (data) {
return data.result
})
}
module.exports = {
get,
getJSON
}

View File

@ -1,4 +1,3 @@
require('isomorphic-fetch')
const parseURL = require('url').parse
const invariant = require('invariant')
const gunzip = require('gunzip-maybe')
@ -7,30 +6,21 @@ const redis = require('redis')
const startOfDay = require('date-fns/start_of_day')
const addDays = require('date-fns/add_days')
const PackageURL = require('./PackageURL')
const CloudflareAPI = require('./CloudflareAPI')
const {
createDayKey,
createHourKey
} = require('./StatsServer')
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
const CloudflareKey = process.env.CLOUDFLARE_KEY
const RedisURL = process.env.OPENREDIS_URL
invariant(
CloudflareEmail,
'Missing the $CLOUDFLARE_EMAIL environment variable'
)
invariant(
CloudflareKey,
'Missing the $CLOUDFLARE_KEY environment variable'
)
invariant(
RedisURL,
'Missing the $OPENREDIS_URL environment variable'
)
const db = redis.createClient(RedisURL)
/**
* Domains we want to analyze.
*/
@ -44,27 +34,18 @@ const DomainNames = [
*/
const LogWindowSeconds = 30
const db = redis.createClient(RedisURL)
function getZones(domain) {
return CloudflareAPI.getJSON(`/zones?name=${domain}`)
}
const getZones = (domain) =>
fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, {
method: 'GET',
headers: {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey
}
}).then(res => res.json())
.then(data => data.result)
const getLogs = (zoneId, startTime, endTime) =>
fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests?start=${startTime}&end=${endTime}`, {
method: 'GET',
headers: {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey,
'Accept-Encoding': 'gzip'
}
}).then(res => res.body.pipe(gunzip()))
function getLogs(zoneId, startTime, endTime) {
return CloudflareAPI.get(
`/zones/${zoneId}/logs/requests?start=${startTime}&end=${endTime}`,
{ 'Accept-Encoding': 'gzip' }
).then(function (res) {
return res.body.pipe(gunzip())
})
}
const toSeconds = (millis) =>
Math.floor(millis / 1000)

View File

@ -1,32 +1,22 @@
require('isomorphic-fetch')
const redis = require('redis')
const addDays = require('date-fns/add_days')
const invariant = require('invariant')
const CloudflareAPI = require('./CloudflareAPI')
const {
createDayKey,
createHourKey,
createMinuteKey
} = require('./StatsServer')
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
const CloudflareKey = process.env.CLOUDFLARE_KEY
const RedisURL = process.env.OPENREDIS_URL
invariant(
CloudflareEmail,
'Missing the $CLOUDFLARE_EMAIL environment variable'
)
invariant(
CloudflareKey,
'Missing the $CLOUDFLARE_KEY environment variable'
)
invariant(
RedisURL,
'Missing the $OPENREDIS_URL environment variable'
)
const db = redis.createClient(RedisURL)
/**
* Domains we want to analyze.
*/
@ -35,27 +25,13 @@ const DomainNames = [
'npmcdn.com'
]
const db = redis.createClient(RedisURL)
function getZones(domain) {
return CloudflareAPI.getJSON(`/zones?name=${domain}`)
}
const getZones = (domain) =>
fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, {
method: 'GET',
headers: {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey
}
}).then(res => res.json())
.then(data => data.result)
const getZoneAnalyticsDashboard = (zoneId, since) =>
fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/analytics/dashboard?since=${since}&continuous=true`, {
method: 'GET',
headers: {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey
}
}).then(res => res.json())
.then(data => data.result)
function getZoneAnalyticsDashboard(zoneId, since) {
return CloudflareAPI.getJSON(`/zones/${zoneId}/analytics/dashboard?since=${since}&continuous=true`)
}
const oneSecond = 1000
const oneMinute = oneSecond * 60