Skip to content

Commit

Permalink
feat(rest): allow setting the "Accept" type
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jun 2, 2017
1 parent 0d6d39f commit 5bd300c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/OnmsHTTPOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class OnmsHTTPOptions {
/** how long to wait for ReST calls to time out */
public timeout = 10000;

/** the type of response to accept */
public accept = 'application/json';

/**
* Construct a new OnmsHTTPOptions object.
* @constructor
Expand Down
8 changes: 8 additions & 0 deletions src/rest/AxiosHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export class AxiosHTTP extends AbstractHTTP implements IOnmsHTTP {
if (options.timeout) {
ret.timeout = options.timeout;
}

if (options.accept === 'application/json') {
ret.responseType = 'json';
} else if (options.accept === 'text/plain') {
ret.responseType = 'text/plain';
} else {
throw new OnmsError('Unhandled response type: ' + options.accept);
}
}

return {};
Expand Down
1 change: 1 addition & 0 deletions src/rest/SuperAgentHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class SuperAgentHTTP extends AbstractHTTP implements IOnmsHTTP {
return request[method](url)
.withCredentials()
.timeout(options.timeout || super.timeout || super.options.timeout)
.set('Accept', options.accept)
.auth(options.auth.username || super.server.auth.username || super.options.auth.username,
options.auth.password || super.server.auth.password || super.options.auth.password);
}
Expand Down

0 comments on commit 5bd300c

Please sign in to comment.