Menu

1 / 2

My Tiny Rust Utils, Part 4: web.rs

DEV Community·icsboyx·7 months ago
#1pZ4NQCQ
#rust#http#api#tutorial#headers
Reading 0:00
15s threshold

I like reqwest .

I just do not always like seeing raw reqwest patterns repeated all over my application code.

That is the whole reason src/utils/web.rs exists.

I don't need full HTTP framework. I just need one place to keep a few recurring things:

response decoding

basic header preparation

a default User-Agent

a simpler response type for the rest of the app

A smaller response model

The file starts here:

#[derive(Debug, Clone, Serialize, Deserialize)] pub enum ResponseBody { Json ( Value ), Text ( String ), Binary ( Bytes ), }

#[derive(Debug, Clone)] pub struct HttpReply { pub status : StatusCode , pub headers : HeaderMap , pub body : ResponseBody , }

Enter fullscreen mode

Exit fullscreen mode

That already helps me.

Instead of passing raw reqwest::Response deeper into the app, I get a smaller local type that matches how this project actually thinks about HTTP replies.

That is enough abstraction for me.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.