UNPKG

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