Skip to content

Commit 7203811

Browse files
committed
Merge pull request Kong#57 from shashiranjan84/master
C# module
2 parents 8f1ec73 + e14e7d2 commit 7203811

19 files changed

Lines changed: 137 additions & 0 deletions

‎src/targets/c#/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

‎src/targets/c#/info.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'c#',
5+
title: 'C#',
6+
extname: '.cs',
7+
default: 'restsharp'
8+
}

‎src/targets/c#/restsharp.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
var CodeBuilder = require('../../helpers/code-builder')
4+
5+
module.exports = function (source, options) {
6+
var code = new CodeBuilder()
7+
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
8+
9+
if (methods.indexOf(source.method.toUpperCase()) === -1) {
10+
return 'Method not supported'
11+
} else {
12+
code.push('var client = new RestClient("%s");', source.fullUrl)
13+
code.push('var request = new RestRequest(Method.%s);', source.method.toUpperCase())
14+
}
15+
16+
// Add headers, including the cookies
17+
var headers = Object.keys(source.headersObj)
18+
19+
// construct headers
20+
if (headers.length) {
21+
headers.map(function (key) {
22+
code.push('request.AddHeader("%s", "%s");', key, source.headersObj[key])
23+
})
24+
}
25+
26+
// construct cookies
27+
if (source.cookies.length) {
28+
source.cookies.forEach(function (cookie) {
29+
code.push('request.AddCookie("%s", "%s");', cookie.name, cookie.value)
30+
})
31+
}
32+
33+
if (source.postData.text) {
34+
code.push('request.AddParameter("%s", %s, ParameterType.RequestBody);', source.allHeaders['content-type'], JSON.stringify(source.postData.text))
35+
}
36+
37+
code.push('IRestResponse response = client.Execute(request);')
38+
return code.join()
39+
}
40+
41+
module.exports.info = {
42+
key: 'restsharp',
43+
title: 'RestSharp',
44+
link: 'http://restsharp.org/',
45+
description: 'Simple REST and HTTP API Client for .NET'
46+
}

‎test/fixtures/available-targets.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,19 @@
200200
"description": "Ruby HTTP client"
201201
}
202202
]
203+
},
204+
{
205+
"key": "c#",
206+
"title": "C#",
207+
"extname": ".cs",
208+
"default": "restsharp",
209+
"clients": [
210+
{
211+
"key": "restsharp",
212+
"title": "RestSharp",
213+
"link": "http://restsharp.org/",
214+
"description": "Simple REST and HTTP API Client for .NET"
215+
}
216+
]
203217
}
204218
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.POST);
3+
request.AddHeader("content-type", "application/x-www-form-urlencoded");
4+
request.AddParameter("application/x-www-form-urlencoded", "foo=bar&hello=world", ParameterType.RequestBody);
5+
IRestResponse response = client.Execute(request);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.POST);
3+
request.AddHeader("content-type", "application/json");
4+
request.AddParameter("application/json", "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}]}", ParameterType.RequestBody);
5+
IRestResponse response = client.Execute(request);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.POST);
3+
request.AddCookie("foo", "bar");
4+
request.AddCookie("bar", "baz");
5+
IRestResponse response = client.Execute(request);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Method not supported
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var client = new RestClient("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value");
2+
var request = new RestRequest(Method.POST);
3+
request.AddHeader("content-type", "application/x-www-form-urlencoded");
4+
request.AddHeader("accept", "application/json");
5+
request.AddCookie("foo", "bar");
6+
request.AddCookie("bar", "baz");
7+
request.AddParameter("application/x-www-form-urlencoded", "foo=bar", ParameterType.RequestBody);
8+
IRestResponse response = client.Execute(request);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.GET);
3+
request.AddHeader("x-foo", "Bar");
4+
request.AddHeader("accept", "application/json");
5+
IRestResponse response = client.Execute(request);

0 commit comments

Comments
 (0)