aboutsummaryrefslogtreecommitdiff
path: root/examples/ureq-examples/src/bin/arti-ureq-tor-client.rs
blob: 6e38229e356f0f0b3b992ee2a5eef55b3ddf7cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Use your own instance of an `arti_client::TorClient` with arti-ureq to make a GET request.

use anyhow::Context;

const TEST_URL: &str = "https://check.torproject.org/api/ip";

fn main() -> anyhow::Result<()> {
    // Create your own instance of a Tor client.
    let tor_client = arti_ureq::arti_client::TorClient::with_runtime(
        arti_ureq::tor_rtcompat::PreferredRuntime::create().context("Failed to create runtime.")?,
    )
    .create_unbootstrapped()
    .context("Error creating Tor Client.")?;

    // Make a Connector and get ureq agent.
    let ureq_agent = arti_ureq::Connector::with_tor_client(tor_client).agent();

    // Make request.
    let mut request = ureq_agent
        .get(TEST_URL)
        .call()
        .context("Failed to make request.")?;

    // Get response body.
    let response = request
        .body_mut()
        .read_to_string()
        .context("Failed to read body.")?;

    // Will output if request was made using Tor.
    println!("Response: {response}");

    Ok(())
}