My agentic slop goes here. Not intended for anyone else!
at main 3.9 kB view raw
1open Zulip 2 3let () = 4 Printf.printf "OCaml Zulip TOML Support Demo\n"; 5 Printf.printf "=============================\n\n"; 6 7 (* Example 1: Create a sample zuliprc TOML file *) 8 let zuliprc_content = {| 9# Zulip API Configuration 10[api] 11email = "demo@example.com" 12key = "demo-api-key-12345" 13site = "https://demo.zulipchat.com" 14 15# Optional settings 16insecure = false 17cert_bundle = "/etc/ssl/certs/ca-certificates.crt" 18|} in 19 20 let zuliprc_file = "demo_zuliprc.toml" in 21 let oc = open_out zuliprc_file in 22 output_string oc zuliprc_content; 23 close_out oc; 24 25 Printf.printf "Created sample zuliprc.toml file:\n%s\n" zuliprc_content; 26 27 (* Test loading auth from TOML *) 28 (match Auth.from_zuliprc ~path:zuliprc_file () with 29 | Ok auth -> 30 Printf.printf "✅ Successfully loaded authentication from TOML:\n"; 31 Printf.printf " Email: %s\n" (Auth.email auth); 32 Printf.printf " Server: %s\n" (Auth.server_url auth); 33 Printf.printf " Auth Header: %s\n" (Auth.to_basic_auth_header auth); 34 35 (* Test creating client *) 36 Eio_main.run @@ fun env -> 37 Eio.Switch.run @@ fun sw -> 38 let client = Client.create ~sw env auth in 39 Printf.printf "✅ Created client successfully\n\n"; 40 41 (* Test basic functionality *) 42 (match Client.request client ~method_:`GET ~path:"/users/me" () with 43 | Ok _response -> Printf.printf "✅ Mock API request succeeded\n" 44 | Error err -> Printf.printf "❌ API request failed: %s\n" (Zulip.error_message err)) 45 | Error err -> 46 Printf.printf "❌ Failed to load auth from TOML: %s\n" (Zulip.error_message err)); 47 48 (* Example 2: Root-level TOML configuration *) 49 let root_toml_content = {| 50email = "root-user@example.com" 51key = "root-api-key-67890" 52site = "https://root.zulipchat.com" 53|} in 54 55 let root_file = "demo_root.toml" in 56 let oc = open_out root_file in 57 output_string oc root_toml_content; 58 close_out oc; 59 60 Printf.printf "\nTesting root-level TOML configuration:\n"; 61 (match Auth.from_zuliprc ~path:root_file () with 62 | Ok auth -> 63 Printf.printf "✅ Root-level TOML parsed successfully:\n"; 64 Printf.printf " Email: %s\n" (Auth.email auth); 65 Printf.printf " Server: %s\n" (Auth.server_url auth) 66 | Error err -> 67 Printf.printf "❌ Failed to parse root-level TOML: %s\n" (Zulip.error_message err)); 68 69 (* Example 3: Test error handling with invalid TOML *) 70 let invalid_toml = {| 71[api 72email = "invalid@example.com" # Missing closing bracket 73|} in 74 75 let invalid_file = "demo_invalid.toml" in 76 let oc = open_out invalid_file in 77 output_string oc invalid_toml; 78 close_out oc; 79 80 Printf.printf "\nTesting error handling with invalid TOML:\n"; 81 (match Auth.from_zuliprc ~path:invalid_file () with 82 | Ok _ -> Printf.printf "❌ Should have failed with invalid TOML\n" 83 | Error err -> Printf.printf "✅ Correctly handled invalid TOML: %s\n" (Zulip.error_message err)); 84 85 (* Example 4: Test missing file handling *) 86 Printf.printf "\nTesting missing file handling:\n"; 87 (match Auth.from_zuliprc ~path:"nonexistent.toml" () with 88 | Ok _ -> Printf.printf "❌ Should have failed with missing file\n" 89 | Error err -> Printf.printf "✅ Correctly handled missing file: %s\n" (Zulip.error_message err)); 90 91 (* Clean up *) 92 List.iter (fun file -> 93 if Sys.file_exists file then Sys.remove file 94 ) [zuliprc_file; root_file; invalid_file]; 95 96 Printf.printf "\n🎉 TOML support demonstration complete!\n"; 97 Printf.printf "\nFeatures demonstrated:\n"; 98 Printf.printf "• Parse TOML files with [api] section\n"; 99 Printf.printf "• Parse TOML files with root-level configuration\n"; 100 Printf.printf "• Proper error handling for invalid TOML syntax\n"; 101 Printf.printf "• Proper error handling for missing files\n"; 102 Printf.printf "• Integration with existing Zulip client\n"