|
2 | 2 |
|
3 | 3 | var CodeBuilder = require('../../helpers/code-builder') |
4 | 4 |
|
| 5 | +function getDecompressionMethods (source) { |
| 6 | + var acceptEncoding = source.allHeaders['accept-encoding'] |
| 7 | + if (!acceptEncoding) { |
| 8 | + return [] // no decompression |
| 9 | + } |
| 10 | + |
| 11 | + var supportedMethods = { |
| 12 | + gzip: 'DecompressionMethods.GZip', |
| 13 | + deflate: 'DecompressionMethods.Deflate' |
| 14 | + } |
| 15 | + var methods = [] |
| 16 | + acceptEncoding.split(',').forEach(function (encoding) { |
| 17 | + var match = /\s*([^;\s]+)/.exec(encoding) |
| 18 | + if (match) { |
| 19 | + var method = supportedMethods[match[1]] |
| 20 | + if (method) { |
| 21 | + methods.push(method) |
| 22 | + } |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + return methods |
| 27 | +} |
| 28 | + |
5 | 29 | module.exports = function (source, options) { |
6 | 30 | var indentation = ' ' |
7 | 31 | var code = new CodeBuilder(indentation) |
8 | 32 |
|
9 | 33 | var clienthandler = '' |
10 | | - if (source.allHeaders.cookie) { |
11 | | - clienthandler = 'new HttpClientHandler { UseCookies = false }' |
| 34 | + var cookies = !!source.allHeaders.cookie |
| 35 | + var decompressionMethods = getDecompressionMethods(source) |
| 36 | + if (cookies || decompressionMethods.length) { |
| 37 | + clienthandler = 'clientHandler' |
| 38 | + code.push('var clientHandler = new HttpClientHandler') |
| 39 | + code.push('{') |
| 40 | + if (cookies) { |
| 41 | + // enable setting the cookie header |
| 42 | + code.push(1, 'UseCookies = false,') |
| 43 | + } |
| 44 | + if (decompressionMethods.length) { |
| 45 | + // enable decompression for supported methods |
| 46 | + code.push(1, 'AutomaticDecompression = %s,', decompressionMethods.join(' | ')) |
| 47 | + } |
| 48 | + code.push('};') |
12 | 49 | } |
13 | 50 |
|
14 | 51 | code.push('var client = new HttpClient(%s);', clienthandler) |
| 52 | + |
15 | 53 | code.push('var request = new HttpRequestMessage') |
16 | 54 | code.push('{') |
17 | 55 |
|
18 | 56 | var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE' ] |
19 | 57 | var method = source.method.toUpperCase() |
20 | 58 | if (method && (methods.indexOf(method) !== -1)) { |
| 59 | + // buildin method |
21 | 60 | method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}` |
22 | 61 | } else { |
| 62 | + // custom method |
23 | 63 | method = `new HttpMethod("${method}")` |
24 | 64 | } |
25 | 65 | code.push(1, 'Method = %s,', method) |
26 | 66 |
|
27 | 67 | code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl) |
28 | 68 |
|
29 | 69 | var headers = Object.keys(source.allHeaders).filter(function (header) { |
30 | | - return header !== 'content-type' |
| 70 | + switch (header) { |
| 71 | + case 'content-type': |
| 72 | + case 'content-length': |
| 73 | + case 'accept-encoding': |
| 74 | + // skip these headers |
| 75 | + return false |
| 76 | + default: |
| 77 | + return true |
| 78 | + } |
31 | 79 | }) |
32 | 80 | if (headers.length) { |
33 | 81 | code.push(1, 'Headers =') |
@@ -84,14 +132,16 @@ module.exports = function (source, options) { |
84 | 132 | break |
85 | 133 | } |
86 | 134 | } |
87 | | - |
88 | 135 | code.push('};') |
| 136 | + |
| 137 | + // send and read response |
89 | 138 | code.push('using (var response = await client.SendAsync(request))') |
90 | 139 | code.push('{') |
91 | 140 | code.push(1, 'response.EnsureSuccessStatusCode();') |
92 | 141 | code.push(1, 'var body = await response.Content.ReadAsStringAsync();') |
93 | 142 | code.push(1, 'Console.WriteLine(body);') |
94 | 143 | code.push('}') |
| 144 | + |
95 | 145 | return code.join() |
96 | 146 | } |
97 | 147 |
|
|
0 commit comments