Skip to content

Commit 84c3227

Browse files
committed
spaces
1 parent b2e30dc commit 84c3227

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn main() -> Result<(), Error> {
3333

3434
// get spaces
3535
let spaces = get_spaces(conf_url, token).await;
36-
println!("{:?}", spaces);
36+
println!("{:?}", spaces.len());
3737

3838
// CREATE PAGE
3939
// let space_key = "dev16";

src/spaces.rs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
pub mod spaces {
2+
use std::ops::Deref;
23
use reqwest::Response;
34
use crate::model::models::{Content, CreatePage, ContentResponse, SearchResults};
4-
use crate::model::space::space::{Space, CreateSpace, Spaces};
5-
6-
pub async fn get_space(url: &str, token: String, key: String) -> Space {
7-
let request_url = format!("{url}/rest/api/space/{key}");
8-
let client = reqwest::Client::new();
9-
let resp: Response = client.get(&request_url)
10-
.header("Authorization", format!("Basic {token}"))
11-
.header("Accept", "application/json")
12-
.send().await.unwrap();
13-
let body = resp.text().await.unwrap();
14-
return serde_json::from_str(body.as_str()).unwrap();
15-
}
5+
use crate::model::space::space::{Space, CreateSpace, Spaces, SpaceResult};
6+
7+
pub struct SpaceService {
8+
pub spaces: Vec<SpaceResult>
169

17-
pub async fn get_spaces(url: &str, token: String) -> Spaces {
18-
let request_url = format!("{url}/rest/api/space/");
19-
let client = reqwest::Client::new();
20-
let resp: Response = client.get(&request_url)
21-
.header("Authorization", format!("Basic {token}"))
22-
.header("Accept", "application/json")
23-
.send().await.unwrap();
24-
let body = resp.text().await.unwrap();
25-
return serde_json::from_str(body.as_str()).unwrap();
2610
}
2711

28-
pub async fn create_space(conf_url: &str, token: &str, page: CreateSpace) -> Space {
29-
let request_url = format!("{conf_url}/rest/api/content/");
30-
let client = reqwest::Client::new();
31-
let res = client.post(&request_url)
32-
.json(&page)
33-
.header("Content-Type", "application/json")
34-
.header("Authorization", format!("Basic {token}"))
35-
.send()
36-
.await.unwrap();
37-
let created: String = res.text().await.unwrap();
38-
return serde_json::from_str(created.as_str()).unwrap();
12+
impl SpaceService {
13+
pub async fn get_spaces(url: &str, token: String) -> Vec<SpaceResult> {
14+
let request_url = format!("{url}/rest/api/space/");
15+
let client = reqwest::Client::new();
16+
let resp: Response = client.get(&request_url)
17+
.header("Authorization", format!("Basic {token}"))
18+
.header("Accept", "application/json")
19+
.send().await.unwrap();
20+
let body = resp.text().await.unwrap();
21+
22+
let spaces_init: Spaces = serde_json::from_str(body.as_str()).unwrap();
23+
spaces_init.results.iter().for_each(|s| spaces.push(s));
24+
25+
return spaces;
26+
}
27+
28+
pub async fn get_space(url: &str, token: String, key: String) -> Space {
29+
let request_url = format!("{url}/rest/api/space/{key}");
30+
let client = reqwest::Client::new();
31+
let resp: Response = client.get(&request_url)
32+
.header("Authorization", format!("Basic {token}"))
33+
.header("Accept", "application/json")
34+
.send().await.unwrap();
35+
let body = resp.text().await.unwrap();
36+
return serde_json::from_str(body.as_str()).unwrap();
37+
}
38+
39+
pub async fn create_space(conf_url: &str, token: &str, page: CreateSpace) -> Space {
40+
let request_url = format!("{conf_url}/rest/api/content/");
41+
let client = reqwest::Client::new();
42+
let res = client.post(&request_url)
43+
.json(&page)
44+
.header("Content-Type", "application/json")
45+
.header("Authorization", format!("Basic {token}"))
46+
.send()
47+
.await.unwrap();
48+
let created: String = res.text().await.unwrap();
49+
return serde_json::from_str(created.as_str()).unwrap();
50+
}
3951
}
52+
4053
}

0 commit comments

Comments
 (0)