Permalink
Browse files

feat(rest): add POST support

  • Loading branch information...
RangerRick committed Jul 25, 2017
1 parent 8f7fa5a commit d3af9e209916f40d148cc78a3e637ad5ca43f1b0
Showing with 63 additions and 0 deletions.
  1. +8 −0 src/api/IOnmsHTTP.ts
  2. +3 −0 src/api/OnmsHTTPOptions.ts
  3. +3 −0 src/rest/AbstractHTTP.ts
  4. +27 −0 src/rest/AxiosHTTP.ts
  5. +22 −0 src/rest/GrafanaHTTP.ts
View
@@ -45,4 +45,12 @@ export interface IOnmsHTTP {
* @returns An [[OnmsResult]] result object. * @returns An [[OnmsResult]] result object.
*/ */
put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>; put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/**
* Perform an HTTP POST to the provided URL.
* @param url The URL to connect to.
* @param options The [[OnmsHTTPOptions]] options to use when connecting.
* @returns An [[OnmsResult]] result object.
*/
post(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
} }
@@ -22,6 +22,9 @@ export class OnmsHTTPOptions {
/** HTTP parameters to be passed on the URL. */ /** HTTP parameters to be passed on the URL. */
public parameters = {} as IHash<string>; public parameters = {} as IHash<string>;
/** HTTP data to be passed when POSTing */
public data: any;
/** /**
* Construct a new OnmsHTTPOptions object. * Construct a new OnmsHTTPOptions object.
* @constructor * @constructor
View
@@ -72,6 +72,9 @@ export abstract class AbstractHTTP implements IOnmsHTTP {
/** Make an HTTP PUT call. This must be overridden by the concrete implementation. */ /** Make an HTTP PUT call. This must be overridden by the concrete implementation. */
public abstract put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>; public abstract put(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/** Make an HTTP POST call. This must be overridden by the concrete implementation. */
public abstract post(url: string, options?: OnmsHTTPOptions): Promise<OnmsResult<any>>;
/** /**
* A convenience method for implementers to use to turn JSON into a javascript object. * A convenience method for implementers to use to turn JSON into a javascript object.
* Use this to process a JSON response before returning it in an [[OnmsResult]] object. * Use this to process a JSON response before returning it in an [[OnmsResult]] object.
View
@@ -97,6 +97,29 @@ export class AxiosHTTP extends AbstractHTTP {
}); });
} }
/**
* Make an HTTP POST call using `axios.request({method:'post'})`.
*/
public post(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('POST ' + urlObj.toString(), catAxios);
opts.method = 'post';
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);
});
}
/** /**
* Clear the current [[AxiosInstance]] so it is recreated on next request with the * Clear the current [[AxiosInstance]] so it is recreated on next request with the
* new server configuration. * new server configuration.
@@ -157,6 +180,10 @@ export class AxiosHTTP extends AbstractHTTP {
ret.params = clonedeep(allOptions.parameters); ret.params = clonedeep(allOptions.parameters);
} }
if (allOptions.data) {
ret.data = clonedeep(allOptions.data);
}
return ret; return ret;
} }
View
@@ -74,6 +74,25 @@ export class GrafanaHTTP extends AbstractHTTP {
}); });
} }
/** Make an HTTP POST call using the Grafana `BackendSrv`. */
public post(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('PUT ' + realUrl);
const query = this.getConfig(options);
query.method = 'POST';
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 [[OnmsHTTPOptions]] into a Grafana `BackendSrv` request object. * Internal method to turn [[OnmsHTTPOptions]] into a Grafana `BackendSrv` request object.
* @hidden * @hidden
@@ -113,6 +132,9 @@ export class GrafanaHTTP extends AbstractHTTP {
ret.params = clonedeep(allOptions.parameters); ret.params = clonedeep(allOptions.parameters);
} }
if (allOptions.data) {
ret.data = clonedeep(allOptions.data);
}
return ret; return ret;
} }

0 comments on commit d3af9e2

Please sign in to comment.