// HTTP client utilities for Lux // // Provides convenient wrappers around the Http effect. // HTTP response type type Response = { status: Int, body: String, headers: List<(String, String)> } // Make a GET request and return the body pub fn get(url: String): String with Http = { Http.get(url) } // Make a POST request with JSON body pub fn postJson(url: String, data: JsonValue): String with Http = { Http.post(url, Json.stringify(data)) } // Make a PUT request with JSON body pub fn putJson(url: String, data: JsonValue): String with Http = { Http.put(url, Json.stringify(data)) } // Make a DELETE request pub fn delete(url: String): String with Http = { Http.delete(url) } // Fetch JSON and parse it pub fn getJson(url: String): JsonValue with Http = { let body = Http.get(url) Json.parse(body) } // Post JSON and parse response as JSON pub fn postJsonGetJson(url: String, data: JsonValue): JsonValue with Http = { let body = Http.post(url, Json.stringify(data)) Json.parse(body) } // URL encode a string pub fn urlEncode(s: String): String = { // Simple URL encoding String.replace( String.replace( String.replace(s, " ", "%20"), "&", "%26" ), "=", "%3D" ) } // Build query string from parameters pub fn buildQueryString(params: List<(String, String)>): String = { let encoded = List.map(params, fn(pair) => { urlEncode(pair.0) ++ "=" ++ urlEncode(pair.1) }) String.join(encoded, "&") } // Make a GET request with query parameters pub fn getWithParams(baseUrl: String, params: List<(String, String)>): String with Http = { let qs = buildQueryString(params) let url = if String.contains(baseUrl, "?") then baseUrl ++ "&" ++ qs else baseUrl ++ "?" ++ qs Http.get(url) }