Skip to content

Commit eb4d615

Browse files
author
Jonathan D.A. Jewell
committed
feat: implement http client, config load, and schema examples
1 parent 826548c commit eb4d615

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

crates/opm-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ anyhow = "1"
1111
serde = { version = "1", features = ["derive"] }
1212
serde_json = "1"
1313
toml = "0.8"
14+
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
1415

1516
[dev-dependencies]
1617
assert_cmd = "2"

crates/opm-cli/src/clients/http.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
// SPDX-License-Identifier: PMPL-1.0
22

3-
use anyhow::Result;
3+
use anyhow::{Context, Result};
4+
use reqwest::blocking::Client;
5+
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
6+
7+
pub fn post_json(base: &str, path: &str, token: Option<&str>, body: &str) -> Result<()> {
8+
let client = Client::new();
9+
let mut headers = HeaderMap::new();
10+
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
11+
12+
if let Some(token) = token {
13+
let value = format!("Bearer {token}");
14+
headers.insert(AUTHORIZATION, HeaderValue::from_str(&value)?);
15+
}
16+
17+
let url = format!("{}{}", base.trim_end_matches('/'), path);
18+
let resp = client
19+
.post(url)
20+
.headers(headers)
21+
.body(body.to_string())
22+
.send()
23+
.context("opm http post")?;
24+
25+
if !resp.status().is_success() {
26+
return Err(anyhow::anyhow!("http error: {}", resp.status()));
27+
}
428

5-
pub fn post_json(_base: &str, _path: &str, _token: Option<&str>, _body: &str) -> Result<()> {
6-
// TODO: implement HTTP POST for service calls
729
Ok(())
830
}

crates/opm-cli/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: PMPL-1.0
22

33
use serde::Deserialize;
4+
use std::fs;
5+
use std::path::Path;
46

57
#[derive(Debug, Deserialize, Clone)]
68
pub struct ServiceConfig {
@@ -42,4 +44,10 @@ impl OpmConfig {
4244
},
4345
}
4446
}
47+
48+
pub fn load_from(path: impl AsRef<Path>) -> anyhow::Result<Self> {
49+
let data = fs::read_to_string(path)?;
50+
let cfg = toml::from_str(&data)?;
51+
Ok(cfg)
52+
}
4553
}

crates/opm-cli/src/wiring.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::config::OpmConfig;
77

88
pub fn publish(path: &str) -> Result<()> {
99
println!("opm publish (stub) for {path}");
10-
let cfg = OpmConfig::example();
10+
let cfg = OpmConfig::load_from("opm.toml").unwrap_or_else(|_| OpmConfig::example());
1111
clients::claim_forge::attest(path, &cfg.claim_forge.base_url, cfg.claim_forge.token.as_deref())?;
1212
clients::checky_monkey::analyze(path, &cfg.checky_monkey.base_url, cfg.checky_monkey.token.as_deref())?;
1313
clients::palimpsest_license::audit(path, &cfg.palimpsest_license.base_url, cfg.palimpsest_license.token.as_deref())?;
@@ -17,7 +17,7 @@ pub fn publish(path: &str) -> Result<()> {
1717

1818
pub fn audit(package: &str) -> Result<()> {
1919
println!("opm audit (stub) for {package}");
20-
let cfg = OpmConfig::example();
20+
let cfg = OpmConfig::load_from("opm.toml").unwrap_or_else(|_| OpmConfig::example());
2121
clients::claim_forge::attest(package, &cfg.claim_forge.base_url, cfg.claim_forge.token.as_deref())?;
2222
clients::checky_monkey::analyze(package, &cfg.checky_monkey.base_url, cfg.checky_monkey.token.as_deref())?;
2323
clients::oikos::score(package, &cfg.oikos.base_url, cfg.oikos.token.as_deref())?;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"package": "example-lib",
3+
"version": "1.2.3"
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"path": "./dist/example-lib",
3+
"manifest": {
4+
"name": "example-lib",
5+
"version": "1.2.3",
6+
"license": "MIT"
7+
}
8+
}

0 commit comments

Comments
 (0)