Skip to content

Commit

Permalink
fix: Support Restsharp 107+ (#179)
Browse files Browse the repository at this point in the history
After version 107, restsharp completely changed their API. This PR fixes the code generator to support the new API.
- also add the escaping function from Kong#289
- I think there are probably still escaping bugs here, but it seems to be working Well Enough™
  • Loading branch information
llimllib authored Jun 16, 2023
1 parent d04de71 commit 3fd5bbc
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 89 deletions.
70 changes: 59 additions & 11 deletions src/targets/csharp/restsharp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { Client } from '../../targets';

import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../helpers/escape';
import { getHeader } from '../../../helpers/headers';

function title(s: string): string {
return s[0].toUpperCase() + s.slice(1).toLowerCase();
}

export const restsharp: Client = {
info: {
Expand All @@ -11,7 +14,7 @@ export const restsharp: Client = {
link: 'http://restsharp.org/',
description: 'Simple REST and HTTP API Client for .NET',
},
convert: ({ allHeaders, method, fullUrl, headersObj, cookies, postData }) => {
convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => {
const { push, join } = new CodeBuilder();
const isSupportedMethod = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes(
method.toUpperCase()
Expand All @@ -21,26 +24,71 @@ export const restsharp: Client = {
return 'Method not supported';
}

push(`var client = new RestClient("${fullUrl}");`);
push(`var request = new RestRequest(Method.${method.toUpperCase()});`);
push('using RestSharp;\n\n');
push(`var options = new RestClientOptions("${fullUrl}");`);
push('var client = new RestClient(options);');

// Add headers, including the cookies
// The first argument is the sub-path to the base URL, given as the
// constructor to RestClient; for our purposes we're just giving the entire
// URL as the base path so it can be an empty string
push('var request = new RestRequest("");');

// If we have multipart form data, set this value. Setting the content-type header manually and then trying to add a mutlipart file parameter
const isMultipart = postData.mimeType && postData.mimeType === 'multipart/form-data';
if (isMultipart) {
push('request.AlwaysMultipartFormData = true;');
}

// Add headers, including the cookies
Object.keys(headersObj).forEach(key => {
// if we have post data, restsharp really wants to set the contentType
// itself; do not add a content-type header or you end up with failures
// which manifest as unhandled exceptions.
if (postData.mimeType && key.toLowerCase() === 'content-type') {
if (isMultipart && postData.boundary) {
push(`request.FormBoundary = "${postData.boundary}";`);
}
return;
}
push(`request.AddHeader("${key}", "${escapeForDoubleQuotes(headersObj[key])}");`);
});

cookies.forEach(({ name, value }) => {
push(`request.AddCookie("${name}", "${value}");`);
push(`request.AddCookie("${name}", "${escapeForDoubleQuotes(value)}", "${uriObj.pathname}", "${uriObj.host}");`);
});

if (postData.text) {
const header = getHeader(allHeaders, 'content-type');
const text = JSON.stringify(postData.text);
push(`request.AddParameter("${header}", ${text}, ParameterType.RequestBody);`);
switch (postData.mimeType) {
case 'multipart/form-data':
if (!postData.params) break;
postData.params.forEach(param => {
if (param.fileName) {
push(`request.AddFile("${param.name}", "${param.fileName}");`);
} else {
push(`request.AddParameter("${param.name}", "${param.value}");`);
}
});
break;
case 'application/x-www-form-urlencoded':
if (!postData.params) break;
postData.params.forEach(param => {
push(`request.AddParameter("${param.name}", "${param.value}");`);
});
break;
case 'application/json': {
if (!postData.text) break;
const text = JSON.stringify(postData.text);
push(`request.AddJsonBody(${text}, false);`);
break;
}
default:
if (!postData.text) break;
push(`request.AddStringBody("${postData.text}", "${postData.mimeType}");`);
}

push('IRestResponse response = client.Execute(request);');
push(`var response = await client.${title(method)}Async(request);\n`);

push('Console.WriteLine("{0}", response.Content);');

return join();
},
};
16 changes: 11 additions & 5 deletions src/targets/csharp/restsharp/fixtures/application-form-encoded.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "foo=bar&hello=world", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddParameter("foo", "bar");
request.AddParameter("hello", "world");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
15 changes: 10 additions & 5 deletions src/targets/csharp/restsharp/fixtures/application-json.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":[]}],\"boolean\":false}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":[]}],\"boolean\":false}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
16 changes: 11 additions & 5 deletions src/targets/csharp/restsharp/fixtures/cookies.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var client = new RestClient("https://httpbin.org/cookies");
var request = new RestRequest(Method.GET);
request.AddCookie("foo", "bar");
request.AddCookie("bar", "baz");
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/cookies");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddCookie("foo", "bar", "/cookies", "httpbin.org");
request.AddCookie("bar", "baz", "/cookies", "httpbin.org");
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
19 changes: 12 additions & 7 deletions src/targets/csharp/restsharp/fixtures/full.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
var client = new RestClient("https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value");
var request = new RestRequest(Method.POST);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddCookie("foo", "bar");
request.AddCookie("bar", "baz");
request.AddParameter("application/x-www-form-urlencoded", "foo=bar", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
request.AddCookie("foo", "bar", "/anything", "httpbin.org");
request.AddCookie("bar", "baz", "/anything", "httpbin.org");
request.AddParameter("foo", "bar");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
12 changes: 9 additions & 3 deletions src/targets/csharp/restsharp/fixtures/headers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
var client = new RestClient("https://httpbin.org/headers");
var request = new RestRequest(Method.GET);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/headers");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddHeader("accept", "application/json");
request.AddHeader("x-foo", "Bar");
request.AddHeader("x-bar", "Foo");
request.AddHeader("quoted-value", "\"quoted\" 'string'");
IRestResponse response = client.Execute(request);
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
12 changes: 9 additions & 3 deletions src/targets/csharp/restsharp/fixtures/http-insecure.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
var client = new RestClient("http://httpbin.org/anything");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("http://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
15 changes: 10 additions & 5 deletions src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\n \"foo\": \"bar\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\n \"foo\": \"bar\"\n}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
15 changes: 10 additions & 5 deletions src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"foo\":null}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\"foo\":null}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
18 changes: 13 additions & 5 deletions src/targets/csharp/restsharp/fixtures/multipart-data.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AlwaysMultipartFormData = true;
request.FormBoundary = "---011000010111000001101001";
request.AddFile("foo", "src/fixtures/files/hello.txt");
request.AddParameter("bar", "Bonjour le monde");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
17 changes: 12 additions & 5 deletions src/targets/csharp/restsharp/fixtures/multipart-file.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AlwaysMultipartFormData = true;
request.FormBoundary = "---011000010111000001101001";
request.AddFile("foo", "src/fixtures/files/hello.txt");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AlwaysMultipartFormData = true;
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
17 changes: 12 additions & 5 deletions src/targets/csharp/restsharp/fixtures/multipart-form-data.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
request.AlwaysMultipartFormData = true;
request.FormBoundary = "---011000010111000001101001";
request.AddParameter("foo", "bar");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
12 changes: 9 additions & 3 deletions src/targets/csharp/restsharp/fixtures/nested.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
var client = new RestClient("https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value");
var client = new RestClient(options);
var request = new RestRequest("");
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
13 changes: 9 additions & 4 deletions src/targets/csharp/restsharp/fixtures/postdata-malformed.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var client = new RestClient("https://httpbin.org/anything");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything");
var client = new RestClient(options);
var request = new RestRequest("");
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);
12 changes: 9 additions & 3 deletions src/targets/csharp/restsharp/fixtures/query-encoded.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
var client = new RestClient("https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00");
var client = new RestClient(options);
var request = new RestRequest("");
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
12 changes: 9 additions & 3 deletions src/targets/csharp/restsharp/fixtures/query.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
var client = new RestClient("https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
using RestSharp;


var options = new RestClientOptions("https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value");
var client = new RestClient(options);
var request = new RestRequest("");
var response = await client.GetAsync(request);

Console.WriteLine("{0}", response.Content);
Loading

0 comments on commit 3fd5bbc

Please sign in to comment.