|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var CodeBuilder = require('../../helpers/code-builder') |
| 4 | + |
| 5 | +module.exports = function (source, options) { |
| 6 | + var indentation = ' ' |
| 7 | + var code = new CodeBuilder(indentation) |
| 8 | + |
| 9 | + var clienthandler = '' |
| 10 | + if (source.allHeaders.cookie) { |
| 11 | + clienthandler = 'new HttpClientHandler { UseCookies = false }' |
| 12 | + } |
| 13 | + |
| 14 | + code.push('var client = new HttpClient(%s);', clienthandler) |
| 15 | + code.push('var request = new HttpRequestMessage') |
| 16 | + code.push('{') |
| 17 | + |
| 18 | + var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE' ] |
| 19 | + var method = source.method.toUpperCase() |
| 20 | + if (method && (methods.indexOf(method) !== -1)) { |
| 21 | + method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}` |
| 22 | + } else { |
| 23 | + method = `new HttpMethod("${method}")` |
| 24 | + } |
| 25 | + code.push(1, 'Method = %s,', method) |
| 26 | + |
| 27 | + code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl) |
| 28 | + |
| 29 | + var headers = Object.keys(source.allHeaders).filter(function (header) { |
| 30 | + return header !== 'content-type' |
| 31 | + }) |
| 32 | + if (headers.length) { |
| 33 | + code.push(1, 'Headers =') |
| 34 | + code.push(1, '{') |
| 35 | + headers.forEach(function (key) { |
| 36 | + code.push(2, '{ "%s", "%s" },', key, source.allHeaders[key]) |
| 37 | + }) |
| 38 | + code.push(1, '},') |
| 39 | + } |
| 40 | + |
| 41 | + if (source.postData.text) { |
| 42 | + const contentType = source.postData.mimeType |
| 43 | + switch (contentType) { |
| 44 | + case 'application/x-www-form-urlencoded': |
| 45 | + code.push(1, 'Content = new FormUrlEncodedContent(new Dictionary<string, string>') |
| 46 | + code.push(1, '{') |
| 47 | + source.postData.params.forEach(function (param) { |
| 48 | + code.push(2, '{ "%s", "%s" },', param.name, param.value) |
| 49 | + }) |
| 50 | + code.push(1, '}),') |
| 51 | + break |
| 52 | + case 'multipart/form-data': |
| 53 | + code.push(1, 'Content = new MultipartFormDataContent') |
| 54 | + code.push(1, '{') |
| 55 | + source.postData.params.forEach(function (param) { |
| 56 | + code.push(2, 'new StringContent(%s)', JSON.stringify(param.value || '')) |
| 57 | + code.push(2, '{') |
| 58 | + code.push(3, 'Headers =') |
| 59 | + code.push(3, '{') |
| 60 | + if (param.contentType) { |
| 61 | + code.push(4, 'ContentType = new MediaTypeHeaderValue("%s"),', param.contentType) |
| 62 | + } |
| 63 | + code.push(4, 'ContentDisposition = new ContentDispositionHeaderValue("form-data")') |
| 64 | + code.push(4, '{') |
| 65 | + code.push(5, 'Name = "%s",', param.name) |
| 66 | + if (param.fileName) { |
| 67 | + code.push(5, 'FileName = "%s",', param.fileName) |
| 68 | + } |
| 69 | + code.push(4, '}') |
| 70 | + code.push(3, '}') |
| 71 | + code.push(2, '},') |
| 72 | + }) |
| 73 | + |
| 74 | + code.push(1, '},') |
| 75 | + break |
| 76 | + default: |
| 77 | + code.push(1, 'Content = new StringContent(%s)', JSON.stringify(source.postData.text || '')) |
| 78 | + code.push(1, '{') |
| 79 | + code.push(2, 'Headers =') |
| 80 | + code.push(2, '{') |
| 81 | + code.push(3, 'ContentType = new MediaTypeHeaderValue("%s")', contentType) |
| 82 | + code.push(2, '}') |
| 83 | + code.push(1, '}') |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + code.push('};') |
| 89 | + code.push('using (var response = await client.SendAsync(request))') |
| 90 | + code.push('{') |
| 91 | + code.push(1, 'response.EnsureSuccessStatusCode();') |
| 92 | + code.push(1, 'var body = await response.Content.ReadAsStringAsync();') |
| 93 | + code.push(1, 'Console.WriteLine(body);') |
| 94 | + code.push('}') |
| 95 | + return code.join() |
| 96 | +} |
| 97 | + |
| 98 | +module.exports.info = { |
| 99 | + key: 'httpclient', |
| 100 | + title: 'HttpClient', |
| 101 | + link: 'https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient', |
| 102 | + description: '.NET Standard HTTP Client' |
| 103 | +} |
0 commit comments