AzureApiCaller.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import request from 'ajax-request'
  16. let apiInstance = null
  17. let defaultApiVersion = '2017-11-11-preview'
  18. let azureUrl = 'https://management.azure.com/'
  19. type RequestOptions = {
  20. headers?: {[string]: mixed},
  21. url: string,
  22. }
  23. class AzureApiCaller {
  24. headers: {[string]: mixed}
  25. constructor() {
  26. if (!apiInstance) {
  27. apiInstance = this
  28. }
  29. this.headers = {}
  30. return apiInstance
  31. }
  32. rejectError(error: string, reject: (error: any) => void) {
  33. console.error('%c', 'color: #D0021B', error) // eslint-disable-line no-console
  34. reject(error)
  35. }
  36. send(options: RequestOptions, apiVersion?: string): Promise<any> {
  37. return new Promise((resolve, reject) => {
  38. options.headers = {
  39. ...options.headers,
  40. ...this.headers,
  41. }
  42. let logUrl = options.url
  43. console.log(`%cSending request to Azure proxy: ${logUrl}`, 'color: #F5A623') // eslint-disable-line no-console
  44. if (options.url.indexOf('/azure-login') === -1) {
  45. options.url = `/proxy/${`${azureUrl + options.url}?api-version=${apiVersion || defaultApiVersion}`}`
  46. }
  47. request(options, (err, resp, body) => {
  48. if (!err && resp.statusCode === 200) {
  49. let bodyJs
  50. try {
  51. bodyJs = JSON.parse(body)
  52. } catch (ex) {
  53. reject(ex)
  54. }
  55. if (!bodyJs) {
  56. this.rejectError('Incorrect response body', reject)
  57. } else if (bodyJs.error) {
  58. this.rejectError(`${bodyJs.error.code}: ${bodyJs.error.message}`, reject)
  59. } else {
  60. console.log(`%cReceiving request from Azure proxy '${logUrl}':`, 'color: #0044CA', bodyJs) // eslint-disable-line no-console
  61. resolve(bodyJs)
  62. }
  63. } else if (err) {
  64. this.rejectError(`${err.code}: ${err.message}`, reject)
  65. } else {
  66. this.rejectError('Request failed, there might be a problem with the connection to the server.', reject)
  67. }
  68. })
  69. })
  70. }
  71. setHeader(name: string, value: ?string) {
  72. this.headers[name] = value
  73. }
  74. }
  75. export default new AzureApiCaller()