Permalink
Browse files

feat(rest): add PUT support

  • Loading branch information...
RangerRick committed Jul 11, 2017
1 parent 6a4df09 commit b50d99e10fcbe09f7daa9b13be5d55ac359ee418
Showing with 64 additions and 8 deletions.
  1. +9 −1 src/api/IOnmsHTTP.ts
  2. +4 −1 src/rest/AbstractHTTP.ts
  3. +30 −4 src/rest/AxiosHTTP.ts
  4. +21 −2 src/rest/GrafanaHTTP.ts
View
@@ -32,10 +32,18 @@ export interface IOnmsHTTP {
options: OnmsHTTPOptions;
/**
* Perform an HTTP get to the provided URL.
* Perform an HTTP GET to the provided URL.
* @param url the URL to connect to
* @param options the {@link OnmsHTTPOptions} options to use when connecting
* @returns an {@link OnmsResult} result object
*/
get(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/**
* Perform an HTTP PUT to the provided URL.
* @param url the URL to connect to
* @param options the {@link OnmsHTTPOptions} options to use when connecting
* @returns an {@link OnmsResult} result object
*/
put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
}
View
@@ -51,9 +51,12 @@ export abstract class AbstractHTTP implements IOnmsHTTP {
this.timeout = timeout;
}
/** make an HTTP get call -- this should be overridden by the implementation */
/** make an HTTP GET call -- this should be overridden by the implementation */
public abstract get(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/** make an HTTP PUT call -- this should be overridden by the implementation */
public abstract put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/** a convenience method for implementers to use to turn JSON into a javascript object */
protected transformJSON(data: any) {
if (typeof data === 'string') {
View
@@ -1,5 +1,6 @@
import axios from 'axios';
import {AxiosStatic, AxiosInstance, AxiosRequestConfig} from 'axios';
import * as qs from 'qs';
/** @hidden */
// tslint:disable-next-line
@@ -34,16 +35,41 @@ export class AxiosHTTP extends AbstractHTTP {
this.axiosImpl = axiosImpl || axios;
}
/** make an HTTP get call -- this should be overridden by the implementation */
/** make an HTTP GET call -- this should be overridden by the implementation */
public get(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
const opts = this.getConfig(options);
const urlObj = new URI(realUrl);
urlObj.search(options.parameters);
log.debug('getting ' + urlObj.toString(), catAxios);
urlObj.search(opts.params);
log.debug('GET ' + urlObj.toString(), catAxios);
opts.method = 'get';
opts.url = realUrl;
return this.getImpl(options).request(opts).then((response) => {
let type;
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}
/** make an HTTP get call -- this should be overridden by the implementation */
public put(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
const opts = this.getConfig(options);
const urlObj = new URI(realUrl);
urlObj.search(opts.params);
log.debug('PUT ' + urlObj.toString(), catAxios);
opts.data = Object.apply({}, opts.params);
opts.method = 'put';
opts.url = realUrl;
return this.getImpl(options).get(realUrl, opts).then((response) => {
return this.getImpl(options).request(opts).then((response) => {
let type;
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
View
@@ -31,10 +31,10 @@ export class GrafanaHTTP extends AbstractHTTP {
this.backendSrv = backendSrv;
}
/** make an HTTP get call -- this should be overridden by the implementation */
/** make an HTTP GET call using the Grafana backendSrv */
public get(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('getting ' + realUrl);
log.debug('GET ' + realUrl);
const query = this.getConfig(options);
query.method = 'GET';
query.url = realUrl;
@@ -50,6 +50,25 @@ export class GrafanaHTTP extends AbstractHTTP {
});
}
/** make an HTTP PUT call using the Grafana backendSrv */
public put(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('PUT ' + realUrl);
const query = this.getConfig(options);
query.method = 'PUT';
query.url = realUrl;
return this.backendSrv.datasourceRequest(query).then((response) => {
let type = 'application/xml';
if (query && query.headers && query.headers.accept) {
type = query.headers.accept;
}
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
});
}
/** internal method to turn {@link OnmsHTTPOptions} into a Grafana BackendSrv request object. */
private getConfig(options?: OnmsHTTPOptions): any {
const allOptions = this.getOptions(options);

0 comments on commit b50d99e

Please sign in to comment.