Skip to content

Commit 410eb73

Browse files
author
Shashi Ranjan
committed
C# module
1 parent 8f1ec73 commit 410eb73

19 files changed

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

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("accept", "application/json");
4+
request.AddHeader("content-type", "application/x-www-form-urlencoded");
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("accept", "application/json");
4+
request.AddHeader("x-foo", "Bar");
5+
IRestResponse response = client.Execute(request);

0 commit comments

Comments
 (0)