-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintermediate_rep.rs
More file actions
59 lines (48 loc) · 1.58 KB
/
intermediate_rep.rs
File metadata and controls
59 lines (48 loc) · 1.58 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use reqwest::{header::{self, HeaderMap, HeaderValue}, StatusCode};
use serde::Deserialize;
use serde::Serialize;
use tracing::{debug, warn};
use crate::ast;
use crate::config::CONFIG;
use crate::errors::FocusError;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IntermediateRepQuery {
pub lang: String,
pub query: String,
}
pub async fn post_ast(ast: ast::Ast) -> Result<String, FocusError> {
debug!("Posting AST...");
let ast_string = serde_json::to_string_pretty(&ast)
.map_err(|e| FocusError::SerializationError(e.to_string()))?;
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
if let Some(auth_header) = CONFIG.auth_header.clone() {
headers.insert(auth_header.0, auth_header.1);
}
let resp = CONFIG
.client
.post(format!("{}", CONFIG.endpoint_url))
.headers(headers)
.body(ast_string.clone())
.send()
.await
.map_err(FocusError::UnableToPostAst)?;
debug!("Posted AST...");
let text = match resp.status() {
StatusCode::OK => resp.text().await.map_err(FocusError::UnableToPostAst)?,
code => {
warn!(
"Got unexpected code {code} while posting AST; reply was `{}`, debug info: {:?}",
ast_string, resp
);
return Err(FocusError::AstPostingErrorReqwest(format!(
"Error while posting AST `{}`: {:?}",
ast_string, resp
)));
}
};
Ok(text)
}