Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit e5c4ed6

Browse files
committed
Use a mustache template to generate the HTTP actions.
Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
1 parent b875c83 commit e5c4ed6

2 files changed

Lines changed: 67 additions & 71 deletions

File tree

csharp/gen_csharp_binding.ml

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let rec main() =
9494
classes |> List.filter (fun x-> x.name <> "session") |> List.iter gen_class_file;
9595
TypeSet.iter gen_enum !enums;
9696
gen_maps();
97-
gen_http_actions();
97+
render_file ("HTTP_actions.mustache", "HTTP_actions.cs") (gen_http_actions()) templdir destdir;
9898
gen_relations();
9999
let sorted_members = List.sort String.compare !api_members in
100100
let json = `O ["api_members", `A (List.map (fun x -> `O ["api_member", `String x];) sorted_members); ] in
@@ -174,89 +174,52 @@ and gen_relation out_chan (manyField, oneClass, oneField) =
174174
(* ------------------- category: http_actions *)
175175

176176
and gen_http_actions() =
177-
let out_chan = open_out (Filename.concat destdir "HTTP_actions.cs") in
178-
let print format = fprintf out_chan format in
179-
180-
let print_header() = print
181-
"%s
182-
183-
using System;
184-
using System.Text;
185-
using System.Net;
186-
187-
namespace XenAPI
188-
{
189-
public partial class HTTP_actions
190-
{
191-
private static void Get(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
192-
string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
193-
{
194-
HTTP.Get(dataCopiedDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
195-
}
196-
197-
private static void Put(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
198-
string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
199-
{
200-
HTTP.Put(progressDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
201-
}"
202-
Licence.bsd_two_clause
203-
in
204-
205-
let print_footer() = print "\n }\n}\n" in
206-
177+
(* Each action has:
178+
(unique public name, (HTTP method, URI, whether to expose in SDK, [args to expose in SDK], [allowed_roles], [(sub-action,allowed_roles)]))
179+
*)
207180
let decl_of_sdkarg = function
208-
String_query_arg s -> "string " ^ (escaped s)
181+
| String_query_arg s -> "string " ^ (escaped s)
209182
| Int64_query_arg s -> "long " ^ (escaped s)
210183
| Bool_query_arg s -> "bool " ^ (escaped s)
211-
| Varargs_query_arg -> "params string[] args /* alternate names & values */"
184+
| Varargs_query_arg -> "params string[] args /* alternate names and values */"
212185
in
213-
214186
let use_of_sdkarg = function
215-
String_query_arg s
187+
| String_query_arg s
216188
| Int64_query_arg s
217-
| Bool_query_arg s -> "\"" ^ s ^ "\", " ^ (escaped s) (* "s", s *)
189+
| Bool_query_arg s -> sprintf "\"%s\", %s" s (escaped s)
218190
| Varargs_query_arg -> "args"
219191
in
220-
221-
let string1 = function
222-
Get -> "HTTP.DataCopiedDelegate dataCopiedDelegate"
223-
| Put -> "HTTP.UpdateProgressDelegate progressDelegate"
192+
let delegate_type = function
193+
| Get -> "DataCopiedDelegate"
194+
| Put -> "UpdateProgressDelegate"
224195
| _ -> failwith "Unimplemented HTTP method"
225196
in
226-
227-
let string2 = function
228-
Get -> "Get(dataCopiedDelegate"
229-
| Put -> "Put(progressDelegate"
197+
let delegate_name = function
198+
| Get -> "dataCopiedDelegate"
199+
| Put -> "progressDelegate"
230200
| _ -> failwith "Unimplemented HTTP method"
231201
in
232-
233-
let print_one_action_core name meth uri sdkargs =
234-
let enhanced_args = [String_query_arg "task_id"; String_query_arg "session_id"] @ sdkargs in
235-
print "
236-
237-
public static void %s(%s, HTTP.FuncBool cancellingDelegate, int timeout_ms,
238-
string hostname, IWebProxy proxy, string path, %s)
239-
{
240-
%s, cancellingDelegate, timeout_ms, hostname, \"%s\", proxy, path,
241-
%s);
242-
}"
243-
name
244-
(string1 meth)
245-
(String.concat ", " (List.map decl_of_sdkarg enhanced_args))
246-
(string2 meth)
247-
uri
248-
(String.concat ", " (List.map use_of_sdkarg enhanced_args))
249-
in
250-
251-
let print_one_action(name, (meth, uri, sdk, sdkargs, _, _)) =
252-
match sdk with
253-
| false -> ()
254-
| true -> print_one_action_core name meth uri sdkargs
202+
let http_method = function
203+
| Get -> "Get"
204+
| Put -> "Put"
205+
| _ -> failwith "Unimplemented HTTP method"
255206
in
256-
257-
print_header();
258-
List.iter print_one_action http_actions;
259-
print_footer();
207+
let action_json (name, (meth, uri, _, sdkargs, _, _)) =
208+
let enhanced_args = [String_query_arg "task_id"; String_query_arg "session_id"] @ sdkargs in
209+
`O [
210+
"name", `String name;
211+
"delegate_type", `String (delegate_type meth);
212+
"delegate_name", `String (delegate_name meth);
213+
"http_method", `String (http_method meth);
214+
"uri", `String uri;
215+
"sdkargs_decl", `String (enhanced_args |> List.map decl_of_sdkarg |> String.concat ", ");
216+
"sdkargs", `String (enhanced_args |> List.map use_of_sdkarg |> String.concat ", ");
217+
] in
218+
let filtered_actions = http_actions |> List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk) in
219+
`O [
220+
"licence", `String Licence.bsd_two_clause;
221+
"http_actions", `A (List.map action_json filtered_actions);
222+
]
260223

261224
(* ------------------- category: classes *)
262225

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{{{licence}}}
2+
3+
using System;
4+
using System.Text;
5+
using System.Net;
6+
7+
namespace XenAPI
8+
{
9+
public partial class HTTP_actions
10+
{
11+
private static void Get(HTTP.DataCopiedDelegate dataCopiedDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
12+
string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
13+
{
14+
HTTP.Get(dataCopiedDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
15+
}
16+
17+
private static void Put(HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout_ms,
18+
string hostname, string remotePath, IWebProxy proxy, string localPath, params object[] args)
19+
{
20+
HTTP.Put(progressDelegate, cancellingDelegate, HTTP.BuildUri(hostname, remotePath, args), proxy, localPath, timeout_ms);
21+
}
22+
23+
{{#http_actions}}
24+
public static void {{name}}(HTTP.{{delegate_type}} {{delegate_name}}, HTTP.FuncBool cancellingDelegate, int timeout_ms,
25+
string hostname, IWebProxy proxy, string path, {{{sdkargs_decl}}})
26+
{
27+
{{http_method}}({{delegate_name}}, cancellingDelegate, timeout_ms, hostname, "{{uri}}", proxy, path,
28+
{{{sdkargs}}});
29+
}
30+
31+
{{/http_actions}}
32+
}
33+
}

0 commit comments

Comments
 (0)