feat: methods to get/put an HTTP resource (#63)
This commit is contained in:
parent
26f630d9d6
commit
643ea9e523
@ -37,7 +37,7 @@
|
||||
"debug": "^2.6.6",
|
||||
"event-to-promise": "^0.8.0",
|
||||
"exec-promise": "^0.6.1",
|
||||
"http-request-plus": "^0.1.1",
|
||||
"http-request-plus": "^0.1.4",
|
||||
"json-rpc-protocol": "^0.11.2",
|
||||
"kindof": "^2.0.0",
|
||||
"lodash": "^4.17.4",
|
||||
@ -53,6 +53,7 @@
|
||||
"babel-cli": "^6.24.1",
|
||||
"babel-eslint": "^7.2.3",
|
||||
"babel-plugin-lodash": "^3.2.11",
|
||||
"babel-plugin-transform-decorators-legacy": "^1.3.4",
|
||||
"babel-preset-env": "^1.4.0",
|
||||
"babel-preset-stage-0": "^6.24.1",
|
||||
"cross-env": "^4.0.0",
|
||||
@ -76,7 +77,8 @@
|
||||
},
|
||||
"babel": {
|
||||
"plugins": [
|
||||
"lodash"
|
||||
"lodash",
|
||||
"transform-decorators-legacy"
|
||||
],
|
||||
"presets": [
|
||||
[
|
||||
|
@ -2,10 +2,13 @@ import Collection from 'xo-collection'
|
||||
import createDebug from 'debug'
|
||||
import kindOf from 'kindof'
|
||||
import ms from 'ms'
|
||||
import httpRequest from 'http-request-plus'
|
||||
import { BaseError } from 'make-error'
|
||||
import { EventEmitter } from 'events'
|
||||
import { filter, forEach, isArray, isObject, map, startsWith } from 'lodash'
|
||||
import {
|
||||
cancelable,
|
||||
CancelToken,
|
||||
catchPlus as pCatch,
|
||||
delay as pDelay
|
||||
} from 'promise-toolbox'
|
||||
@ -309,6 +312,66 @@ export class Xapi extends EventEmitter {
|
||||
throw new Error('there is no object with the UUID ' + uuid)
|
||||
}
|
||||
|
||||
@cancelable
|
||||
getResource ($cancelToken, pathname, { host, query }) {
|
||||
return httpRequest(
|
||||
$cancelToken,
|
||||
this._url,
|
||||
host && {
|
||||
hostname: this.getObject(host).address
|
||||
},
|
||||
{
|
||||
path: pathname,
|
||||
query: {
|
||||
...query,
|
||||
session_id: this.sessionId
|
||||
},
|
||||
rejectUnauthorized: !this._allowUnauthorized
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@cancelable
|
||||
putResource ($cancelToken, stream, pathname, {
|
||||
host,
|
||||
query
|
||||
} = {}) {
|
||||
const headers = {}
|
||||
|
||||
// Xen API does not support chunk encoding.
|
||||
const { length } = stream
|
||||
if (length === undefined) {
|
||||
// add a fake huge content length (1 PiB)
|
||||
headers['content-length'] = '1125899906842624'
|
||||
|
||||
const { cancel, token } = CancelToken.source()
|
||||
$cancelToken = CancelToken.race([ $cancelToken, token ])
|
||||
|
||||
// when the data has been emitted, close the connection
|
||||
stream.on('end', () => {
|
||||
setTimeout(cancel, 1e3)
|
||||
})
|
||||
}
|
||||
|
||||
return httpRequest.put(
|
||||
$cancelToken,
|
||||
this._url,
|
||||
host && {
|
||||
hostname: this.getObject(host).address
|
||||
},
|
||||
{
|
||||
body: stream,
|
||||
headers,
|
||||
path: pathname,
|
||||
query: {
|
||||
...query,
|
||||
session_id: this.sessionId
|
||||
},
|
||||
rejectUnauthorized: !this._allowUnauthorized
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
get pool () {
|
||||
return this._pool
|
||||
}
|
||||
|
@ -430,7 +430,7 @@ babel-plugin-syntax-class-properties@^6.8.0:
|
||||
version "6.13.0"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de"
|
||||
|
||||
babel-plugin-syntax-decorators@^6.13.0:
|
||||
babel-plugin-syntax-decorators@^6.1.18, babel-plugin-syntax-decorators@^6.13.0:
|
||||
version "6.13.0"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-decorators/-/babel-plugin-syntax-decorators-6.13.0.tgz#312563b4dbde3cc806cee3e416cceeaddd11ac0b"
|
||||
|
||||
@ -495,6 +495,14 @@ babel-plugin-transform-class-properties@^6.24.1:
|
||||
babel-runtime "^6.22.0"
|
||||
babel-template "^6.24.1"
|
||||
|
||||
babel-plugin-transform-decorators-legacy@^1.3.4:
|
||||
version "1.3.4"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-transform-decorators-legacy/-/babel-plugin-transform-decorators-legacy-1.3.4.tgz#741b58f6c5bce9e6027e0882d9c994f04f366925"
|
||||
dependencies:
|
||||
babel-plugin-syntax-decorators "^6.1.18"
|
||||
babel-runtime "^6.2.0"
|
||||
babel-template "^6.3.0"
|
||||
|
||||
babel-plugin-transform-decorators@^6.24.1:
|
||||
version "6.24.1"
|
||||
resolved "https://registry.yarnpkg.com/babel-plugin-transform-decorators/-/babel-plugin-transform-decorators-6.24.1.tgz#788013d8f8c6b5222bdf7b344390dfd77569e24d"
|
||||
@ -817,14 +825,14 @@ babel-register@^6.24.1:
|
||||
mkdirp "^0.5.1"
|
||||
source-map-support "^0.4.2"
|
||||
|
||||
babel-runtime@^6.18.0, babel-runtime@^6.22.0:
|
||||
babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0:
|
||||
version "6.23.0"
|
||||
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.23.0.tgz#0a9489f144de70efb3ce4300accdb329e2fc543b"
|
||||
dependencies:
|
||||
core-js "^2.4.0"
|
||||
regenerator-runtime "^0.10.0"
|
||||
|
||||
babel-template@^6.16.0, babel-template@^6.24.1:
|
||||
babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.3.0:
|
||||
version "6.24.1"
|
||||
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.24.1.tgz#04ae514f1f93b3a2537f2a0f60a5a45fb8308333"
|
||||
dependencies:
|
||||
@ -1888,9 +1896,9 @@ html-encoding-sniffer@^1.0.1:
|
||||
dependencies:
|
||||
whatwg-encoding "^1.0.1"
|
||||
|
||||
http-request-plus@^0.1.1:
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/http-request-plus/-/http-request-plus-0.1.1.tgz#f12e5f00d4808d5863145e9dde7b3e3274bb5e0c"
|
||||
http-request-plus@^0.1.4:
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/http-request-plus/-/http-request-plus-0.1.4.tgz#c99cd36366e96c13f92da5954b3a2fd2ce2c531e"
|
||||
dependencies:
|
||||
is-redirect "^1.0.0"
|
||||
lodash "^4.17.4"
|
||||
|
Loading…
Reference in New Issue
Block a user