open Eio open Requests let () = Eio_main.run @@ fun env -> Mirage_crypto_rng_unix.use_default (); Switch.run @@ fun sw -> (* Example 1: Basic session usage with cookies *) Printf.printf "\n=== Example 1: Basic Session with Cookies ===\n"; let session = Session.create ~sw env in (* First request sets a cookie *) let resp1 = Session.get session "https://httpbin.org/cookies/set?session_id=abc123" in Printf.printf "Set cookie response: %d\n" (Response.status resp1); (* Second request automatically includes the cookie *) let resp2 = Session.get session "https://httpbin.org/cookies" in let body2 = Response.body resp2 |> Buf_read.take_all in Printf.printf "Cookies seen by server: %s\n" body2; (* Example 2: Session with default headers and auth *) Printf.printf "\n=== Example 2: Session with Default Configuration ===\n"; let github_session = Session.create ~sw env in (* Set default headers that apply to all requests *) Session.set_default_header github_session "User-Agent" "OCaml-Requests-Example/1.0"; Session.set_default_header github_session "Accept" "application/vnd.github.v3+json"; (* Set authentication (if you have a token) *) (* Session.set_auth github_session (Auth.bearer "your_github_token"); *) (* All requests will use these defaults *) let user = Session.get github_session "https://api.github.com/users/ocaml" in Printf.printf "GitHub user status: %d\n" (Response.status user); (* Example 3: Session with retry logic *) Printf.printf "\n=== Example 3: Session with Retry Logic ===\n"; let retry_config = Retry.create_config ~max_retries:3 ~backoff_factor:0.5 ~status_forcelist:[429; 500; 502; 503; 504] () in let robust_session = Session.create ~sw ~retry:retry_config env in Session.set_timeout robust_session (Timeout.create ~total:30.0 ()); (* This request will automatically retry on failures *) let result = Session.get robust_session "https://httpbin.org/status/503" in Printf.printf "Request status (might retry): %d\n" (Response.status result); (* Example 4: Persistent cookies *) Printf.printf "\n=== Example 4: Persistent Cookies ===\n"; let persistent_session = Session.create ~sw ~persist_cookies:true ~app_name:"ocaml_example" env in (* Login and save cookies *) let _login = Session.post persistent_session ~form:["username", "demo"; "password", "demo"] "https://httpbin.org/post" in (* Cookies will be saved to ~/.config/ocaml_example/cookies.txt *) Session.save_cookies persistent_session; Printf.printf "Cookies saved to disk\n"; (* Example 5: Concurrent requests with the same session *) Printf.printf "\n=== Example 5: Concurrent Requests ===\n"; let urls = [ "https://httpbin.org/delay/1"; "https://httpbin.org/delay/1"; "https://httpbin.org/delay/1"; ] in let start_time = Unix.gettimeofday () in let responses = Session.map_concurrent session ~max_concurrent:3 ~f:(fun sess url -> let resp = Session.get sess url in Response.status resp ) urls in let elapsed = Unix.gettimeofday () -. start_time in Printf.printf "Concurrent requests completed in %.2fs\n" elapsed; List.iter (Printf.printf "Status: %d\n") responses; (* Example 6: Prepared requests *) Printf.printf "\n=== Example 6: Prepared Requests ===\n"; let prepared = Session.Prepared.create ~session ~method_:Method.POST "https://httpbin.org/post" in (* Inspect and modify the prepared request *) let prepared = Session.Prepared.set_header prepared "X-Custom" "Header" in let prepared = Session.Prepared.set_body prepared (Body.text "Hello, World!") in Format.printf "Prepared request:@.%a@." Session.Prepared.pp prepared; (* Send when ready *) let resp = Session.Prepared.send prepared in Printf.printf "Prepared request sent, status: %d\n" (Response.status resp); (* Example 7: Hooks *) Printf.printf "\n=== Example 7: Request/Response Hooks ===\n"; let hook_session = Session.create ~sw env in (* Add a request hook to log all requests *) Session.Hooks.add_request_hook hook_session (fun headers method_ url -> Printf.printf "-> Request: %s %s\n" (Method.to_string method_) url; headers ); (* Add a response hook to log all responses *) Session.Hooks.add_response_hook hook_session (fun response -> Printf.printf "<- Response: %d\n" (Response.status response) ); (* All requests will trigger hooks *) let _ = Session.get hook_session "https://httpbin.org/get" in let _ = Session.post hook_session "https://httpbin.org/post" in (* Example 8: Session statistics *) Printf.printf "\n=== Example 8: Session Statistics ===\n"; let stats = Session.stats session in Printf.printf "Total requests: %d\n" stats#requests_made; Printf.printf "Total time: %.3fs\n" stats#total_time; Printf.printf "Average time per request: %.3fs\n" (stats#total_time /. float_of_int stats#requests_made); (* Pretty print session info *) Format.printf "@.Session info:@.%a@." Session.pp session; (* Example 9: Download file *) Printf.printf "\n=== Example 9: Download File ===\n"; let download_session = Session.create ~sw env in let temp_file = Path.(env#fs / "/tmp/example_download.json") in Session.download_file download_session ~on_progress:(fun ~received ~total -> match total with | Some t -> Printf.printf "Downloaded %Ld/%Ld bytes\r%!" received t | None -> Printf.printf "Downloaded %Ld bytes\r%!" received ) "https://httpbin.org/json" temp_file; Printf.printf "\nFile downloaded to /tmp/example_download.json\n"; Printf.printf "\n=== All examples completed successfully! ===\n"