MCPcopy Create free account
hub / github.com/TheRedDeveloper/ply-engine / fire_http

Function fire_http

src/net/mod.rs:82–215  ·  view source on GitHub ↗
(
    method: &str,
    id: &str,
    url: &str,
    f: impl FnOnce(&mut HttpConfig) -> &mut HttpConfig,
)

Source from the content-addressed store, hash-verified

80}
81
82fn fire_http(
83 method: &str,
84 id: &str,
85 url: &str,
86 f: impl FnOnce(&mut HttpConfig) -> &mut HttpConfig,
87) {
88 let key = hash_id(id);
89 let mut mgr = NET_MANAGER.lock().unwrap();
90
91 // Idempotent: don't re-fire if a request with this ID already exists
92 if mgr.http_requests.contains_key(&key) {
93 return;
94 }
95
96 let mut config = HttpConfig::new();
97 f(&mut config);
98
99 #[cfg(not(target_arch = "wasm32"))]
100 {
101 use http::PendingHttp;
102
103 let method = method.to_owned();
104 let url = url.to_owned();
105 let (tx, rx) = std::sync::mpsc::channel();
106
107 std::thread::spawn(move || {
108 let result: Result<Response, String> = (|| {
109 let agent = ureq::Agent::new_with_defaults();
110
111 macro_rules! apply_headers {
112 ($req:expr, $headers:expr) => {{
113 let mut r = $req;
114 for (key, value) in $headers {
115 r = r.header(key.as_str(), value.as_str());
116 }
117 r
118 }};
119 }
120
121 let send_result = match method.as_str() {
122 "GET" => {
123 let req = apply_headers!(agent.get(&url), &config.headers);
124 req.call()
125 }
126 "DELETE" => {
127 let req = apply_headers!(agent.delete(&url), &config.headers);
128 req.call()
129 }
130 "POST" => {
131 let req = apply_headers!(agent.post(&url), &config.headers);
132 if let Some(body) = &config.body {
133 req.content_type("application/octet-stream").send(body)
134 } else {
135 req.send_empty()
136 }
137 }
138 "PUT" => {
139 let req = apply_headers!(agent.put(&url), &config.headers);

Callers 4

getFunction · 0.85
postFunction · 0.85
putFunction · 0.85
deleteFunction · 0.85

Calls 7

hash_idFunction · 0.85
fFunction · 0.85
spawnFunction · 0.85
as_strMethod · 0.80
sendMethod · 0.80
statusMethod · 0.80

Tested by

no test coverage detected