Skip to content

Commit 2da5d3f

Browse files
author
Daniel Teoh
committed
Adds ability to override request agent
1 parent d10252c commit 2da5d3f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var client = new Swagger({
3636
});
3737
```
3838

39-
NOTE: we're explicitly setting the responseContentType, because we don't want you getting stuck when
39+
NOTE: we're explicitly setting the responseContentType, because we don't want you getting stuck when
4040
there is more than one content type available.
4141

4242
That's it! You'll get a JSON response with the default callback handler:
@@ -299,8 +299,24 @@ var client = new SwaggerClient({
299299
});
300300
```
301301

302+
You can also pass in your own version superagent (if, for example, you have other superagent plugins etc that you want to use)
303+
304+
var agent = require('some-other-special-superagent');
305+
306+
var client = new SwaggerClient({
307+
spec: petstoreRaw,
308+
requestAgent: agent,
309+
success: function () {
310+
client.pet.getPetById({petId: 3}, function(data){
311+
expect(data).toBe('ok');
312+
done();
313+
});
314+
}
315+
});
316+
```
317+
302318
### Using custom http(s) agent
303-
In case if you need to sign all requests to petstore with custom certificate
319+
In case if you need to sign all requests to petstore with custom certificate
304320
305321
```js
306322
var connectionAgent = {

lib/http.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ SwaggerHttp.prototype.execute = function (obj, opts) {
3838
}
3939
client.opts = opts || {};
4040

41+
if (opts && opts.requestAgent) {
42+
request = opts.requestAgent;
43+
}
44+
4145
// legacy support
4246
var hasJQuery = false;
4347
if(typeof window !== 'undefined') {
@@ -221,7 +225,7 @@ SuperagentHttpClient.prototype.execute = function (obj) {
221225
}
222226

223227
var r = request[method](obj.url);
224-
228+
225229
if (timeout) {
226230
r.timeout(timeout);
227231
}

lib/types/operation.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var Operation = module.exports = function (parent, scheme, operationId, httpMeth
2121
this.client = parent.options.client || null;
2222
this.requestInterceptor = parent.options.requestInterceptor || null;
2323
this.responseInterceptor = parent.options.responseInterceptor || null;
24+
this.requestAgent = parent.options.requestAgent;
2425
}
2526
this.authorizations = args.security;
2627
this.basePath = parent.basePath || '/';
@@ -773,6 +774,10 @@ Operation.prototype.execute = function (arg1, arg2, arg3, arg4, parent) {
773774
opts.client = this.client;
774775
}
775776

777+
if(this.requestAgent) {
778+
opts.requestAgent = this.requestAgent;
779+
}
780+
776781
// add the request interceptor from parent, if none sent from client
777782
if(!opts.requestInterceptor && this.requestInterceptor ) {
778783
opts.requestInterceptor = this.requestInterceptor ;

0 commit comments

Comments
 (0)