UNPKG

3.53 kBJavaScriptView Raw
1const auth = require('./auth')
2const nodes = require('./handlers/nodes')
3const workflow = require('./handlers/workflow')
4const rhcore = require('./handlers/rhcore')
5const members = require('./handlers/members')
6const versions = require('./handlers/versions')
7const webreports = require('./handlers/webreports')
8const FormDataFactory = require('./handlers/form-data-factory')
9const isnil = require('lodash.isnil')
10
11const sha1 = require('sha1')
12
13const Semaphore = require('@chriscdn/promise-semaphore')
14const semaphore = new Semaphore()
15
16let getCache = {}
17
18module.exports = class Session {
19
20 constructor(options) {
21 this.axios = auth(options)
22 }
23
24 get nodes() {
25 // this creates a circular reference.. bad?
26 if (this._nodes == null) {
27 this._nodes = nodes(this)
28 }
29
30 return this._nodes
31 }
32
33 get workflow() {
34 // this creates a circular reference.. bad?
35 if (this._workflow == null) {
36 this._workflow = workflow(this)
37 }
38
39 return this._workflow
40 }
41
42 get rhcore() {
43 // this creates a circular reference.. bad?
44 if (this._rhcore == null) {
45 this._rhcore = rhcore(this)
46 }
47
48 return this._rhcore
49 }
50
51 get members() {
52 // this creates a circular reference.. bad?
53 if (this._members == null) {
54 this._members = members(this)
55 }
56
57 return this._members
58 }
59
60 get webreports() {
61 // this creates a circular reference.. bad?
62 if (this._webreports == null) {
63 this._webreports = webreports(this)
64 }
65
66 return this._webreports
67 }
68
69 get versions() {
70 // this creates a circular reference.. bad?
71 if (this._versions == null) {
72 this._versions = versions(this)
73 }
74
75 return this._versions
76 }
77
78 _isObject(value) {
79 return value && typeof value === 'object' && value.constructor === Object
80 }
81
82 _objectToForm(obj) {
83
84 const formData = FormDataFactory.createFormData()
85
86 for (let [key, value] of Object.entries(obj)) {
87 if (value && value.name && value.file) {
88 formData.append(key, value.file, value.name)
89 } else if (Array.isArray(value) || this._isObject(value)) {
90 formData.append(key, JSON.stringify(value))
91 } else if (!isnil(value)) {
92 // should empty strings be sent?
93 formData.append(key, value)
94 }
95 }
96
97 return formData
98 }
99
100 get(...args) {
101 return this.axios.get(...args)
102 }
103
104 async getCached(...args) {
105 const key = sha1(JSON.stringify(args))
106
107 try {
108 await semaphore.acquire(key)
109
110 if (!getCache[key]) {
111 getCache[key] = this.get(...args)
112 }
113 } finally {
114 semaphore.release(key)
115 }
116
117 return getCache[key]
118 }
119
120 putForm(url, params) {
121 const formData = this._objectToForm(params)
122 return process.node ? this.put(url, formData.getBuffer(), {
123 headers: formData.getHeaders()
124 }) : this.put(url, formData)
125 }
126
127 postForm(url, params) {
128 const formData = this._objectToForm(params)
129 return process.node ? this.post(url, formData.getBuffer(), {
130 headers: formData.getHeaders()
131 }) : this.post(url, formData)
132 }
133
134 patchForm(url, params) {
135 const formData = this._objectToForm(params)
136 return process.node ? this.patch(url, formData.getBuffer(), {
137 headers: formData.getHeaders()
138 }) : this.patch(url, formData)
139 }
140
141 post(...args) {
142 return this.axios.post(...args)
143 }
144
145 put(...args) {
146 return this.axios.put(...args)
147 }
148
149 delete(...args) {
150 return this.axios.delete(...args)
151 }
152
153 options(...args) {
154 return this.axios.options(...args)
155 }
156
157 patch(...args) {
158 return this.axios.patch(...args)
159 }
160
161}
\No newline at end of file