forked from elasticio/marathon-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (73 loc) · 2.86 KB
/
index.js
File metadata and controls
86 lines (73 loc) · 2.86 KB
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
'use strict'
const axios = require('axios')
const MARATHON_API_VERSION = 'v2'
// http api endpoints
const MarathonApiAppEndpoints = require('./api/apps')
const MarathonApiDeploymentEndpoints = require('./api/deployments')
const MarathonApiGroupEndpoints = require('./api/groups')
const MarathonApiTaskEndpoints = require('./api/tasks')
const MarathonApiInfoEndpoints = require('./api/info')
const MarathonApiQueueEndpoints = require('./api/queue')
const MarathonApiMiscEndpoints = require('./api/misc')
const MarathonApiLeaderEndpoints = require('./api/leader')
// EventSource
const MarathonEventSource = require('./api/events')
// not yet implemented so no need to require here
// var artifacts = require('./artifacts')
// helper functions
const { omit } = require('./lib/nodash')
const contentType = 'application/json'
// configure axios defaults
axios.defaults.headers.common.Accept = contentType
axios.defaults.headers.post['Content-Type'] = contentType
axios.defaults.headers.put['Content-Type'] = contentType
axios.defaults.headers.delete['Content-Type'] = contentType
/**
* https://mesosphere.github.io/marathon/docs/rest-api.html
*/
class MarathonApi {
get basePath () { return `/${MARATHON_API_VERSION}` }
get http () { // returns an http client
const client = axios.create({
...this.defaults,
baseURL: this.baseURL.toString()
})
if (this.logTime) {
client.interceptors.request.use(async config => this.timeTracker(config))
client.interceptors.response.use(async res => this.timeTracker(res))
}
return client
}
constructor (baseURL, opts = {}) {
this.logTime = opts.logTime
this.defaults = omit(opts, 'logTime') // setup options without the logTime
this.baseURL = new URL(`${baseURL}`) // if it's already a url object create a new copy
this.baseURL.pathname = this.basePath
// initialize the api endpoints
this.app = new MarathonApiAppEndpoints(this)
this.apps = this.app // alias for app
this.deployments = new MarathonApiDeploymentEndpoints(this)
this.events = new MarathonEventSource(this)
this.groups = new MarathonApiGroupEndpoints(this)
this.tasks = new MarathonApiTaskEndpoints(this)
this.info = new MarathonApiInfoEndpoints(this)
this.queue = new MarathonApiQueueEndpoints(this)
this.misc = new MarathonApiMiscEndpoints(this)
this.leader = new MarathonApiLeaderEndpoints(this)
this.timers = {} // key/object pairing of timer tokens
}
async timeTracker (c) {
if (this.logTime && !c.status) {
const { pathname } = new URL(c.baseURL)
if (!(pathname in this.timers)) {
this.timers[pathname] = `Marathon Request Timer: ${pathname}`
}
console.time(this.timers[pathname])
} else if (this.logTime && c?.status) {
const { request: { path } } = c
console.timeEnd(this.timers[path])
}
return c
}
}
module.exports = MarathonApi