@@ -2,7 +2,10 @@ import type { Client } from '../../targets';
2
2
3
3
import { CodeBuilder } from '../../../helpers/code-builder' ;
4
4
import { escapeForDoubleQuotes } from '../../../helpers/escape' ;
5
- import { getHeader } from '../../../helpers/headers' ;
5
+
6
+ function title ( s : string ) : string {
7
+ return s [ 0 ] . toUpperCase ( ) + s . slice ( 1 ) . toLowerCase ( ) ;
8
+ }
6
9
7
10
export const restsharp : Client = {
8
11
info : {
@@ -11,7 +14,7 @@ export const restsharp: Client = {
11
14
link : 'http://restsharp.org/' ,
12
15
description : 'Simple REST and HTTP API Client for .NET' ,
13
16
} ,
14
- convert : ( { allHeaders , method, fullUrl, headersObj, cookies, postData } ) => {
17
+ convert : ( { method, fullUrl, headersObj, cookies, postData, uriObj } ) => {
15
18
const { push, join } = new CodeBuilder ( ) ;
16
19
const isSupportedMethod = [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'HEAD' , 'OPTIONS' ] . includes (
17
20
method . toUpperCase ( )
@@ -21,26 +24,71 @@ export const restsharp: Client = {
21
24
return 'Method not supported' ;
22
25
}
23
26
24
- push ( `var client = new RestClient("${ fullUrl } ");` ) ;
25
- push ( `var request = new RestRequest(Method.${ method . toUpperCase ( ) } );` ) ;
27
+ push ( 'using RestSharp;\n\n' ) ;
28
+ push ( `var options = new RestClientOptions("${ fullUrl } ");` ) ;
29
+ push ( 'var client = new RestClient(options);' ) ;
26
30
27
- // Add headers, including the cookies
31
+ // The first argument is the sub-path to the base URL, given as the
32
+ // constructor to RestClient; for our purposes we're just giving the entire
33
+ // URL as the base path so it can be an empty string
34
+ push ( 'var request = new RestRequest("");' ) ;
35
+
36
+ // If we have multipart form data, set this value. Setting the content-type header manually and then trying to add a mutlipart file parameter
37
+ const isMultipart = postData . mimeType && postData . mimeType === 'multipart/form-data' ;
38
+ if ( isMultipart ) {
39
+ push ( 'request.AlwaysMultipartFormData = true;' ) ;
40
+ }
28
41
42
+ // Add headers, including the cookies
29
43
Object . keys ( headersObj ) . forEach ( key => {
44
+ // if we have post data, restsharp really wants to set the contentType
45
+ // itself; do not add a content-type header or you end up with failures
46
+ // which manifest as unhandled exceptions.
47
+ if ( postData . mimeType && key . toLowerCase ( ) === 'content-type' ) {
48
+ if ( isMultipart && postData . boundary ) {
49
+ push ( `request.FormBoundary = "${ postData . boundary } ";` ) ;
50
+ }
51
+ return ;
52
+ }
30
53
push ( `request.AddHeader("${ key } ", "${ escapeForDoubleQuotes ( headersObj [ key ] ) } ");` ) ;
31
54
} ) ;
32
55
33
56
cookies . forEach ( ( { name, value } ) => {
34
- push ( `request.AddCookie("${ name } ", "${ value } ");` ) ;
57
+ push ( `request.AddCookie("${ name } ", "${ escapeForDoubleQuotes ( value ) } ", " ${ uriObj . pathname } ", " ${ uriObj . host } ");` ) ;
35
58
} ) ;
36
59
37
- if ( postData . text ) {
38
- const header = getHeader ( allHeaders , 'content-type' ) ;
39
- const text = JSON . stringify ( postData . text ) ;
40
- push ( `request.AddParameter("${ header } ", ${ text } , ParameterType.RequestBody);` ) ;
60
+ switch ( postData . mimeType ) {
61
+ case 'multipart/form-data' :
62
+ if ( ! postData . params ) break ;
63
+ postData . params . forEach ( param => {
64
+ if ( param . fileName ) {
65
+ push ( `request.AddFile("${ param . name } ", "${ param . fileName } ");` ) ;
66
+ } else {
67
+ push ( `request.AddParameter("${ param . name } ", "${ param . value } ");` ) ;
68
+ }
69
+ } ) ;
70
+ break ;
71
+ case 'application/x-www-form-urlencoded' :
72
+ if ( ! postData . params ) break ;
73
+ postData . params . forEach ( param => {
74
+ push ( `request.AddParameter("${ param . name } ", "${ param . value } ");` ) ;
75
+ } ) ;
76
+ break ;
77
+ case 'application/json' : {
78
+ if ( ! postData . text ) break ;
79
+ const text = JSON . stringify ( postData . text ) ;
80
+ push ( `request.AddJsonBody(${ text } , false);` ) ;
81
+ break ;
82
+ }
83
+ default :
84
+ if ( ! postData . text ) break ;
85
+ push ( `request.AddStringBody("${ postData . text } ", "${ postData . mimeType } ");` ) ;
41
86
}
42
87
43
- push ( 'IRestResponse response = client.Execute(request);' ) ;
88
+ push ( `var response = await client.${ title ( method ) } Async(request);\n` ) ;
89
+
90
+ push ( 'Console.WriteLine("{0}", response.Content);' ) ;
91
+
44
92
return join ( ) ;
45
93
} ,
46
94
} ;
0 commit comments