My agentic slop goes here. Not intended for anyone else!
1open Eio
2open Requests
3
4let () =
5 Eio_main.run @@ fun env ->
6 Mirage_crypto_rng_unix.use_default ();
7 Switch.run @@ fun sw ->
8
9 (* Example 1: Basic session usage with cookies *)
10 Printf.printf "\n=== Example 1: Basic Session with Cookies ===\n";
11 let session = Session.create ~sw env in
12
13 (* First request sets a cookie *)
14 let resp1 = Session.get session "https://httpbin.org/cookies/set?session_id=abc123" in
15 Printf.printf "Set cookie response: %d\n" (Response.status resp1);
16
17 (* Second request automatically includes the cookie *)
18 let resp2 = Session.get session "https://httpbin.org/cookies" in
19 let body2 = Response.body resp2 |> Buf_read.take_all in
20 Printf.printf "Cookies seen by server: %s\n" body2;
21
22 (* Example 2: Session with default headers and auth *)
23 Printf.printf "\n=== Example 2: Session with Default Configuration ===\n";
24 let github_session = Session.create ~sw env in
25
26 (* Set default headers that apply to all requests *)
27 Session.set_default_header github_session "User-Agent" "OCaml-Requests-Example/1.0";
28 Session.set_default_header github_session "Accept" "application/vnd.github.v3+json";
29
30 (* Set authentication (if you have a token) *)
31 (* Session.set_auth github_session (Auth.bearer "your_github_token"); *)
32
33 (* All requests will use these defaults *)
34 let user = Session.get github_session "https://api.github.com/users/ocaml" in
35 Printf.printf "GitHub user status: %d\n" (Response.status user);
36
37 (* Example 3: Session with retry logic *)
38 Printf.printf "\n=== Example 3: Session with Retry Logic ===\n";
39 let retry_config = Retry.create_config
40 ~max_retries:3
41 ~backoff_factor:0.5
42 ~status_forcelist:[429; 500; 502; 503; 504]
43 () in
44
45 let robust_session = Session.create ~sw ~retry:retry_config env in
46 Session.set_timeout robust_session (Timeout.create ~total:30.0 ());
47
48 (* This request will automatically retry on failures *)
49 let result = Session.get robust_session "https://httpbin.org/status/503" in
50 Printf.printf "Request status (might retry): %d\n" (Response.status result);
51
52 (* Example 4: Persistent cookies *)
53 Printf.printf "\n=== Example 4: Persistent Cookies ===\n";
54 let persistent_session = Session.create ~sw
55 ~persist_cookies:true
56 ~app_name:"ocaml_example"
57 env in
58
59 (* Login and save cookies *)
60 let _login = Session.post persistent_session
61 ~form:["username", "demo"; "password", "demo"]
62 "https://httpbin.org/post" in
63
64 (* Cookies will be saved to ~/.config/ocaml_example/cookies.txt *)
65 Session.save_cookies persistent_session;
66 Printf.printf "Cookies saved to disk\n";
67
68 (* Example 5: Concurrent requests with the same session *)
69 Printf.printf "\n=== Example 5: Concurrent Requests ===\n";
70 let urls = [
71 "https://httpbin.org/delay/1";
72 "https://httpbin.org/delay/1";
73 "https://httpbin.org/delay/1";
74 ] in
75
76 let start_time = Unix.gettimeofday () in
77 let responses = Session.map_concurrent session ~max_concurrent:3
78 ~f:(fun sess url ->
79 let resp = Session.get sess url in
80 Response.status resp
81 ) urls in
82
83 let elapsed = Unix.gettimeofday () -. start_time in
84 Printf.printf "Concurrent requests completed in %.2fs\n" elapsed;
85 List.iter (Printf.printf "Status: %d\n") responses;
86
87 (* Example 6: Prepared requests *)
88 Printf.printf "\n=== Example 6: Prepared Requests ===\n";
89 let prepared = Session.Prepared.create
90 ~session
91 ~method_:Method.POST
92 "https://httpbin.org/post" in
93
94 (* Inspect and modify the prepared request *)
95 let prepared = Session.Prepared.set_header prepared "X-Custom" "Header" in
96 let prepared = Session.Prepared.set_body prepared (Body.text "Hello, World!") in
97
98 Format.printf "Prepared request:@.%a@." Session.Prepared.pp prepared;
99
100 (* Send when ready *)
101 let resp = Session.Prepared.send prepared in
102 Printf.printf "Prepared request sent, status: %d\n" (Response.status resp);
103
104 (* Example 7: Hooks *)
105 Printf.printf "\n=== Example 7: Request/Response Hooks ===\n";
106 let hook_session = Session.create ~sw env in
107
108 (* Add a request hook to log all requests *)
109 Session.Hooks.add_request_hook hook_session (fun headers method_ url ->
110 Printf.printf "-> Request: %s %s\n" (Method.to_string method_) url;
111 headers
112 );
113
114 (* Add a response hook to log all responses *)
115 Session.Hooks.add_response_hook hook_session (fun response ->
116 Printf.printf "<- Response: %d\n" (Response.status response)
117 );
118
119 (* All requests will trigger hooks *)
120 let _ = Session.get hook_session "https://httpbin.org/get" in
121 let _ = Session.post hook_session "https://httpbin.org/post" in
122
123 (* Example 8: Session statistics *)
124 Printf.printf "\n=== Example 8: Session Statistics ===\n";
125 let stats = Session.stats session in
126 Printf.printf "Total requests: %d\n" stats#requests_made;
127 Printf.printf "Total time: %.3fs\n" stats#total_time;
128 Printf.printf "Average time per request: %.3fs\n"
129 (stats#total_time /. float_of_int stats#requests_made);
130
131 (* Pretty print session info *)
132 Format.printf "@.Session info:@.%a@." Session.pp session;
133
134 (* Example 9: Download file *)
135 Printf.printf "\n=== Example 9: Download File ===\n";
136 let download_session = Session.create ~sw env in
137 let temp_file = Path.(env#fs / "/tmp/example_download.json") in
138
139 Session.download_file download_session
140 ~on_progress:(fun ~received ~total ->
141 match total with
142 | Some t -> Printf.printf "Downloaded %Ld/%Ld bytes\r%!" received t
143 | None -> Printf.printf "Downloaded %Ld bytes\r%!" received
144 )
145 "https://httpbin.org/json"
146 temp_file;
147
148 Printf.printf "\nFile downloaded to /tmp/example_download.json\n";
149
150 Printf.printf "\n=== All examples completed successfully! ===\n"