GitRoot

Craft your forge, Build your project, Grow your community freely
 1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: MIT
 4
 5use std::collections::HashMap;
 6
 7use crate::{
 8    imports,
 9    model::{HttpRequest, HttpResponse},
10};
11
12pub struct HttpClient {
13    header: HashMap<String, Vec<String>>,
14}
15
16impl HttpClient {
17    pub fn new(header: HashMap<String, Vec<String>>) -> Self {
18        HttpClient { header }
19    }
20
21    pub fn get<S>(&self, url: S) -> Result<HttpResponse, String>
22    where
23        S: Into<String>,
24    {
25        imports::http_client(&HttpRequest {
26            method: "GET".into(),
27            url: url.into(),
28            body: "".into(),
29            header: self.header.clone(),
30        })
31    }
32
33    pub fn post<S>(&self, url: S, body: S) -> Result<HttpResponse, String>
34    where
35        S: Into<String>,
36    {
37        imports::http_client(&HttpRequest {
38            method: "POST".into(),
39            url: url.into(),
40            body: body.into(),
41            header: self.header.clone(),
42        })
43    }
44
45    pub fn put<S>(&self, url: S, body: S) -> Result<HttpResponse, String>
46    where
47        S: Into<String>,
48    {
49        imports::http_client(&HttpRequest {
50            method: "PUT".into(),
51            url: url.into(),
52            body: body.into(),
53            header: self.header.clone(),
54        })
55    }
56
57    pub fn delete<S>(&self, url: S) -> Result<HttpResponse, String>
58    where
59        S: Into<String>,
60    {
61        imports::http_client(&HttpRequest {
62            method: "DELETE".into(),
63            url: url.into(),
64            body: "".into(),
65            header: self.header.clone(),
66        })
67    }
68}