UNPKG

5.61 kBJavaScriptView Raw
1const axiosFactory = require('./axios-factory')
2const auth = require('./handlers/auth.js')
3const nodes = require('./handlers/nodes')
4const workflow = require('./handlers/workflow')
5const rhcore = require('./handlers/rhcore')
6const search = require('./handlers/search')
7const members = require('./handlers/members')
8const versions = require('./handlers/versions')
9const webreports = require('./handlers/webreports')
10const FormDataFactory = require('./handlers/form-data-factory')
11const isnil = require('lodash.isnil')
12const RPCClient = require('./rpc-client')
13const dataTypesEnum = require('./data-types-enum.json')
14
15// let getCache = {}
16
17module.exports = class Session {
18 constructor (options) {
19 this.axios = axiosFactory(options)
20 }
21
22 get nodes () {
23 // this creates a circular reference.. bad?
24 if (this._nodes == null) {
25 this._nodes = nodes(this)
26 }
27
28 return this._nodes
29 }
30
31 get auth () {
32 if (this._auth == null) {
33 this._auth = auth(this)
34 }
35
36 return this._auth
37 }
38
39 get workflow () {
40 // this creates a circular reference.. bad?
41 if (this._workflow == null) {
42 this._workflow = workflow(this)
43 }
44
45 return this._workflow
46 }
47
48 get rhcore () {
49 // this creates a circular reference.. bad?
50 if (this._rhcore == null) {
51 this._rhcore = rhcore(this)
52 }
53
54 return this._rhcore
55 }
56
57 get members () {
58 // this creates a circular reference.. bad?
59 if (this._members == null) {
60 this._members = members(this)
61 }
62
63 return this._members
64 }
65
66 get search () {
67 // this creates a circular reference.. bad?
68 if (this._search == null) {
69 this._search = search(this)
70 }
71
72 return this._search
73 }
74
75 get webreports () {
76 // this creates a circular reference.. bad?
77 if (this._webreports == null) {
78 this._webreports = webreports(this)
79 }
80
81 return this._webreports
82 }
83
84 get versions () {
85 // this creates a circular reference.. bad?
86 if (this._versions == null) {
87 this._versions = versions(this)
88 }
89
90 return this._versions
91 }
92
93 get dataTypesEnum () {
94 return dataTypesEnum
95 }
96
97 rpcClient (baseURL = '/api/v1/rh/rpc/rhnode/') {
98 return new RPCClient(this, baseURL)
99 }
100
101 _isObject (value) {
102 return value && typeof value === 'object' && value.constructor === Object
103 }
104
105 _isString (value) {
106 return (typeof value === 'string' || value instanceof String)
107 }
108
109 _isBoolean (value) {
110 return (typeof value === 'boolean')
111 }
112
113 objectToForm (obj) {
114 const formData = FormDataFactory.createFormData()
115
116 for (const [key, value] of Object.entries(obj)) {
117 if (value && value.name && value.file) {
118 formData.append(key, value.file, value.name)
119 } else if (Array.isArray(value) || this._isObject(value) || this._isBoolean(value)) {
120 formData.append(key, JSON.stringify(value))
121 } else if (!isnil(value)) {
122 // should empty strings be sent?
123 formData.append(key, value)
124 }
125 }
126
127 return formData
128 }
129
130 get (...args) {
131 return this.axios.get(...args)
132 }
133
134 /*
135 // async getCached(...args) {
136 // const key = sha1(JSON.stringify(args))
137
138 // try {
139 // await semaphore.acquire(key)
140
141 // if (!getCache[key]) {
142 // getCache[key] = this.get(...args)
143 // }
144 // } finally {
145 // semaphore.release(key)
146 // }
147
148 // return getCache[key]
149 // }
150*/
151 putForm (url, params) {
152 const formData = this.objectToForm(params)
153 return process.node
154 ? this.put(url, formData.getBuffer(), {
155 headers: formData.getHeaders()
156 })
157 : this.put(url, formData)
158 }
159
160 postForm (url, params) {
161 const formData = this.objectToForm(params)
162 return process.node
163 ? this.post(url, formData.getBuffer(), {
164 headers: formData.getHeaders(),
165 maxBodyLength: Infinity
166 })
167 : this.post(url, formData, {
168 maxBodyLength: Infinity
169 })
170 }
171
172 patchForm (url, params) {
173 const formData = this.objectToForm(params)
174 return process.node
175 ? this.patch(url, formData.getBuffer(), {
176 headers: formData.getHeaders()
177 })
178 : this.patch(url, formData)
179 }
180
181 deleteForm (url, params) {
182 // FormData does not working on Delete!!
183 // See here: https://stackoverflow.com/questions/51069552/axios-delete-request-with-body-and-headers
184 const formData = this.objectToForm(params)
185 return process.node
186 ? this.delete(url, formData.getBuffer(), {
187 headers: formData.getHeaders()
188 })
189 : this.delete(url, formData)
190 }
191
192 putBody (url, body) {
193 return this.putForm(url, {
194 body
195 })
196 }
197
198 postBody (url, body) {
199 return this.postForm(url, {
200 body
201 })
202 }
203
204 patchBody (url, body) {
205 return this.patchForm(url, {
206 body
207 })
208 }
209
210 deleteBody (url, body) {
211 return this.deleteForm(url, {
212 body
213 })
214 }
215
216 post (...args) {
217 return this.axios.post(...args)
218 }
219
220 put (...args) {
221 return this.axios.put(...args)
222 }
223
224 delete (...args) {
225 return this.axios.delete(...args)
226
227 // console.log(args)
228 // console.log(url)
229
230 // return this.axios.delete(URL, {
231 // headers: {
232 // Authorization: authorizationToken
233 // },
234 // data: {
235 // source: source
236 // }
237 // });
238 }
239
240 options (...args) {
241 return this.axios.options(...args)
242 }
243
244 patch (...args) {
245 return this.axios.patch(...args)
246 }
247}