11import { RequestOptions } from "../request" ;
22
33export function generateCSharpCode ( options : RequestOptions ) : string {
4+ // Remove content-type from headers if it's JSON since it will be set by StringContent
5+ const headers = { ...options . headers } ;
6+ const isJsonContent =
7+ headers &&
8+ Object . entries ( headers ) . some (
9+ ( [ key , value ] ) =>
10+ key . toLowerCase ( ) === "content-type" &&
11+ value . toLowerCase ( ) . includes ( "json" )
12+ ) ;
13+
14+ // Helper function to convert JSON to C# object initialization syntax
15+ function toCSharpObject ( obj : any , indent : number = 3 ) : string {
16+ if ( obj === null ) return "null" ;
17+ if ( typeof obj === "string" ) return `"${ obj } "` ;
18+ if ( typeof obj === "number" || typeof obj === "boolean" )
19+ return obj . toString ( ) ;
20+ if ( Array . isArray ( obj ) ) {
21+ if ( obj . length === 0 ) return "new object[] {}" ;
22+ const items = obj
23+ . map ( ( item ) => toCSharpObject ( item , indent + 1 ) )
24+ . join ( ",\n" + " " . repeat ( indent * 4 ) ) ;
25+ return `new object[] {\n${ " " . repeat (
26+ indent * 4
27+ ) } ${ items } \n${ " " . repeat ( ( indent - 1 ) * 4 ) } }`;
28+ }
29+ if ( typeof obj === "object" ) {
30+ const entries = Object . entries ( obj ) ;
31+ if ( entries . length === 0 ) return "new {}" ;
32+ const props = entries
33+ . map (
34+ ( [ key , value ] ) =>
35+ `${ key } = ${ toCSharpObject ( value , indent + 1 ) } `
36+ )
37+ . join ( ",\n" + " " . repeat ( indent * 4 ) ) ;
38+ return `new {\n${ " " . repeat ( indent * 4 ) } ${ props } \n${ " " . repeat (
39+ ( indent - 1 ) * 4
40+ ) } }`;
41+ }
42+ return "null" ;
43+ }
44+
445 let code = `using System;
546using System.Net.Http;
647using System.Threading.Tasks;
48+ using System.Text.Json;
749
8- public class Program
9- {
10- public static async Task Main(string[] args)
11- {
12- using (var client = new HttpClient())
13- {
14- var request = new HttpRequestMessage
15- {
16- Method = new HttpMethod("${ options . method || 'GET' } "),
17- RequestUri = new Uri("${ options . url } "),
18- };
19-
20- ${ options . headers ? Object . entries ( options . headers ) . map ( ( [ key , value ] ) => `request.Headers.Add("${ key } ", "${ value } ");` ) . join ( '\n' ) : '' }
21-
22- ${ options . body ? `request.Content = new StringContent("${ options . body } ");` : '' }
23-
24- ${ options . query ? `var query = System.Web.HttpUtility.ParseQueryString(string.Empty);
25- ${ Object . entries ( options . query ) . map ( ( [ key , value ] ) => {
50+ public class Program {
51+ public static async Task Main(string[] args) {
52+ using var client = new HttpClient();
53+ ${
54+ isJsonContent && options . body
55+ ? `var requestData = ${ toCSharpObject (
56+ JSON . parse ( options . body )
57+ ) } ;\n\n `
58+ : ""
59+ } var request = new HttpRequestMessage {
60+ Method = new HttpMethod("${ options . method || "GET" } "),
61+ RequestUri = new Uri("${ options . url } ")
62+ };
63+ ${ Object . entries ( headers || { } )
64+ . map (
65+ ( [ key , value ] ) =>
66+ `request.Headers.Add("${ key } ", "${ value } ");\n `
67+ )
68+ . join ( "" ) }
69+ ${
70+ options . body
71+ ? isJsonContent
72+ ? `var jsonOptions = new JsonSerializerOptions {
73+ };
74+ request.Content = new StringContent(
75+ JsonSerializer.Serialize(requestData, jsonOptions),
76+ System.Text.Encoding.UTF8,
77+ "application/json"
78+ );`
79+ : `request.Content = new StringContent("${ options . body } ");`
80+ : ""
81+ }
82+ ${
83+ options . query
84+ ? `var query = System.Web.HttpUtility.ParseQueryString(string.Empty);
85+ ${ Object . entries ( options . query )
86+ . map ( ( [ key , value ] ) => {
2687 if ( Array . isArray ( value ) ) {
27- return value . map ( v => `query["${ key } "] = "${ v } ";` ) . join ( '\n' ) ;
88+ return value
89+ . map ( ( v ) => `query["${ key } "] = "${ v } ";\n ` )
90+ . join ( "" ) ;
2891 } else {
29- return `query["${ key } "] = "${ value } ";` ;
92+ return `query["${ key } "] = "${ value } ";\n ` ;
3093 }
31- } ) . join ( '\n' ) }
32- request.RequestUri = new Uri(request.RequestUri + "?" + query);` : '' }
33-
34- try
35- {
36- using (var response = await client.SendAsync(request))
37- {
38- response.EnsureSuccessStatusCode();
39- }
40- }
41- catch (Exception ex)
42- {
43- Console.WriteLine(ex.ToString());
44- }
94+ } )
95+ . join (
96+ ""
97+ ) } request.RequestUri = new Uri(request.RequestUri + "?" + query);`
98+ : ""
99+ }
100+ try {
101+ using var response = await client.SendAsync(request);
102+ response.EnsureSuccessStatusCode();
103+ } catch (Exception ex) {
104+ Console.WriteLine(ex.ToString());
45105 }
46106 }
47107}` ;
48108 return code ;
49- }
109+ }
0 commit comments