Skip to content

Instantly share code, notes, and snippets.

@uasi
Last active August 17, 2023 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uasi/9eff1b5c3a2ba61ce9ccfddb439745ce to your computer and use it in GitHub Desktop.
Save uasi/9eff1b5c3a2ba61ce9ccfddb439745ce to your computer and use it in GitHub Desktop.
curl_easy.inko
import std.stdio.STDOUT
import extern 'c'
fn extern fopen(path: Pointer[UInt8], mode: Pointer[UInt8]) -> Pointer[UInt8]
import extern 'curl'
fn extern curl_easy_init -> Pointer[UInt8]
fn extern curl_easy_setopt(handle: Pointer[UInt8], option: Int32, ...) -> Int32
fn extern curl_easy_perform(handle: Pointer[UInt8]) -> Int32
fn extern curl_easy_cleanup(handle: Pointer[UInt8])
let CURLOPT_WRITEDATA = 10001
let CURLOPT_URL = 10002
class pub Handle {
let @handle: Pointer[UInt8]
fn pub static new -> Handle {
Handle { @handle = curl_easy_init }
}
fn pub mut set_writedata(file: Pointer[UInt8]) -> Int {
curl_easy_setopt(@handle, CURLOPT_WRITEDATA as Int32, file) as Int
}
fn pub mut set_url(url: String) -> Int {
curl_easy_setopt(@handle, CURLOPT_URL as Int32, url.to_pointer) as Int
}
fn pub mut perform -> Int {
curl_easy_perform(@handle) as Int
}
}
class async Main {
fn async main {
let handle = Handle.new
handle.set_writedata(fopen('./out.html'.to_pointer, 'wb'.to_pointer))
handle.set_url('https://example.com')
let result = handle.perform
STDOUT.new.print("result = {result}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment