Client http and request

⚓ Rust    📅 2026-04-25    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Client http and request

This is the code I’m using to initialise an HTTP client, and I’m using it to make requests to the GitHub API. I’ll post it here.

use crate::model::github_events::Events;
use reqwest::{blocking, Result};
use std::sync::LazyLock;

static CLIENTS: LazyLock<blocking::Client> = LazyLock::new(|| {
    match blocking::Client::builder().build() {
        Ok(value) => value,
        Err(err) => panic!("{err:?}"),
    }
});

pub fn get_user(name_user_github: String) -> Result<Vec<Events>> {
    let uri: String = format!("https://api.github.com/users/{name_user_github}/events");
    let body = CLIENTS.get(&uri).send()?;
    let res: Vec<Events> = body.json()?;
    Ok(res)
}



Any suggestions for improvement? I’m only just beginning to understand how std::sync works.

Many thanks for any suggestions you can offer to help me improve my code

3 posts - 2 participants

Read full topic

🏷️ Rust_feed