Skip to content

Commit ab2d74c

Browse files
committed
CLI should use Agent in requests - get #986
1 parent f7d93b1 commit ab2d74c

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c
55
**Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md).
66
See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable.
77

8+
## UNRELEASED
9+
10+
- CLI should use Agent in requests - get #986
11+
812
## [v0.40.2]
913

1014
- fix property sort order when importing + add tests #980

cli/src/main.rs

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ enum Commands {
104104
/// Validates the store
105105
#[command(hide = true)]
106106
Validate,
107+
/// Print the current agent
108+
Agent,
107109
}
108110

109111
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
@@ -263,6 +265,10 @@ fn exec_command(context: &mut Context) -> AtomicResult<()> {
263265
Commands::Validate => {
264266
validate(context);
265267
}
268+
Commands::Agent => {
269+
let agent = context.read_config();
270+
println!("{}", agent.agent);
271+
}
266272
};
267273
Ok(())
268274
}

cli/src/path.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ pub fn get_path(
1010
// let subcommand_matches = context.matches.subcommand_matches("get").unwrap();
1111
let path_string: String = path_vec.join(" ");
1212

13+
context.read_config();
14+
1315
// Returns a URL or Value
1416
let store = &mut context.store;
1517
let path = store.get_path(
1618
&path_string,
1719
Some(&context.mapping.lock().unwrap()),
18-
&ForAgent::Sudo,
20+
&store.get_default_agent()?.into(),
1921
)?;
2022
let out = match path {
2123
storelike::PathReturn::Subject(subject) => {

lib/src/agents.rs

+13
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ pub fn verify_public_key(public_key: &str) -> AtomicResult<()> {
211211
Ok(())
212212
}
213213

214+
impl From<Agent> for ForAgent {
215+
fn from(agent: Agent) -> Self {
216+
agent.subject.into()
217+
}
218+
}
219+
220+
impl<'a> From<&'a Agent> for ForAgent {
221+
fn from(agent: &'a Agent) -> Self {
222+
let subject: String = agent.subject.clone();
223+
subject.into()
224+
}
225+
}
226+
214227
#[cfg(test)]
215228
mod test {
216229
#[cfg(test)]

lib/src/client/helpers.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,33 @@ pub fn fetch_body(
5454
if !url.starts_with("http") {
5555
return Err(format!("Could not fetch url '{}', must start with http.", url).into());
5656
}
57-
if let Some(agent) = client_agent {
58-
get_authentication_headers(url, agent)?;
59-
}
6057

61-
let agent = ureq::builder()
58+
let client = ureq::builder()
6259
.timeout(std::time::Duration::from_secs(2))
6360
.build();
64-
let resp = agent
65-
.get(url)
66-
.set("Accept", content_type)
67-
.call()
68-
.map_err(|e| format!("Error when server tried fetching {} : {}", url, e))?;
61+
62+
let mut req = client.get(url);
63+
if let Some(agent) = client_agent {
64+
let headers = get_authentication_headers(url, agent)?;
65+
for (key, value) in headers {
66+
req = req.set(key.as_str(), value.as_str());
67+
}
68+
}
69+
70+
let resp = match req.set("Accept", content_type).call() {
71+
Ok(response) => response,
72+
Err(ureq::Error::Status(status, response)) => {
73+
let body = response
74+
.into_string()
75+
.unwrap_or_else(|_| "<failed to read response body>".to_string());
76+
return Err(format!(
77+
"Error when server tried fetching {}: Status: {}. Body: {}",
78+
url, status, body
79+
)
80+
.into());
81+
}
82+
Err(e) => return Err(format!("Error when server tried fetching {}: {}", url, e).into()),
83+
};
6984
let status = resp.status();
7085
let body = resp
7186
.into_string()

0 commit comments

Comments
 (0)