Skip to content

Commit 48e733a

Browse files
committed
C# HttpClient target with decompression
1 parent 33446e7 commit 48e733a

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

src/targets/csharp/httpclient.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,80 @@
22

33
var CodeBuilder = require('../../helpers/code-builder')
44

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+
529
module.exports = function (source, options) {
630
var indentation = ' '
731
var code = new CodeBuilder(indentation)
832

933
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('};')
1249
}
1350

1451
code.push('var client = new HttpClient(%s);', clienthandler)
52+
1553
code.push('var request = new HttpRequestMessage')
1654
code.push('{')
1755

1856
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE' ]
1957
var method = source.method.toUpperCase()
2058
if (method && (methods.indexOf(method) !== -1)) {
59+
// buildin method
2160
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}`
2261
} else {
62+
// custom method
2363
method = `new HttpMethod("${method}")`
2464
}
2565
code.push(1, 'Method = %s,', method)
2666

2767
code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl)
2868

2969
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+
}
3179
})
3280
if (headers.length) {
3381
code.push(1, 'Headers =')
@@ -84,14 +132,16 @@ module.exports = function (source, options) {
84132
break
85133
}
86134
}
87-
88135
code.push('};')
136+
137+
// send and read response
89138
code.push('using (var response = await client.SendAsync(request))')
90139
code.push('{')
91140
code.push(1, 'response.EnsureSuccessStatusCode();')
92141
code.push(1, 'var body = await response.Content.ReadAsStringAsync();')
93142
code.push(1, 'Console.WriteLine(body);')
94143
code.push('}')
144+
95145
return code.join()
96146
}
97147

test/fixtures/output/csharp/httpclient/cookies.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
1+
var clientHandler = new HttpClientHandler
2+
{
3+
UseCookies = false,
4+
};
5+
var client = new HttpClient(clientHandler);
26
var request = new HttpRequestMessage
37
{
48
Method = HttpMethod.Post,

test/fixtures/output/csharp/httpclient/full.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
1+
var clientHandler = new HttpClientHandler
2+
{
3+
UseCookies = false,
4+
};
5+
var client = new HttpClient(clientHandler);
26
var request = new HttpRequestMessage
37
{
48
Method = HttpMethod.Post,

test/fixtures/output/csharp/httpclient/multipart-file.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{
1515
Name = "foo",
1616
FileName = "test/fixtures/files/hello.txt",
17-
},
17+
}
1818
}
1919
},
2020
},

test/fixtures/output/csharp/httpclient/multipart-form-data.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
ContentDisposition = new ContentDispositionHeaderValue("form-data")
1313
{
1414
Name = "foo",
15-
},
15+
}
1616
}
1717
},
1818
},

0 commit comments

Comments
 (0)