Skip to content

Commit f4b2bc3

Browse files
committed
spaces
1 parent d922a2d commit f4b2bc3

File tree

5 files changed

+126
-41
lines changed

5 files changed

+126
-41
lines changed

src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use reqwest::{Body, Error};
1010
use serde_json::{json, Value};
1111
use tokio::time::Instant;
1212
use crate::model::models::{Ancestor, CreatePage, CreatePageSpace, PageBody, Storage};
13-
use crate::pages::page_service::{create_page, get_children, get_page};
13+
use crate::pages::page_service::{create_page, get_children, get_descendants, get_page};
14+
use crate::spaces::spaces::get_space;
1415

1516

1617
#[tokio::main]
@@ -23,9 +24,11 @@ async fn main() -> Result<(), Error> {
2324
let conf_url = "http://localhost:8110";
2425

2526
// get page
26-
let page = get_children(conf_url, token, "1213317".to_string()).await;
27-
println!("{:?}", page);
27+
// let pages = get_descendants(conf_url, token, "1213317".to_string()).await;
28+
// pages.results.iter().for_each(|p| println!("{:?}", p.title));
2829

30+
// get space
31+
let space = get_space(conf_url, token, "dev16".to_string()).await;
2932

3033
// CREATE PAGE
3134
// let space_key = "dev16";

src/model.rs

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod space;
2+
13
pub mod models {
24
use serde_json::{json, Value};
35
use serde::{Deserialize, Serialize};
@@ -6,7 +8,7 @@ pub mod models {
68
#[allow(non_snake_case)]
79
pub struct Extentions<T> {
810
#[serde(skip_serializing_if = "Option::is_none")]
9-
position: Option<T>,
11+
pub position: Option<T>,
1012
}
1113

1214
// impl Extentions {
@@ -37,72 +39,80 @@ pub mod models {
3739
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
3840
#[allow(non_snake_case)]
3941
pub struct Expandable {
40-
container: String,
41-
metadata: String,
42-
operations: String,
43-
children: String,
44-
restrictions: String,
45-
ancestors: String,
46-
body: String,
47-
descendants: String,
42+
pub container: String,
43+
pub metadata: String,
44+
pub operations: String,
45+
pub children: String,
46+
pub restrictions: String,
47+
pub ancestors: String,
48+
pub body: String,
49+
pub descendants: String,
4850
}
4951

5052
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
5153
#[allow(non_snake_case)]
5254
pub struct ContentLinks {
5355
#[serde(rename(serialize = "self"))]
5456
#[serde(rename(deserialize = "self"))]
55-
sself: String,
56-
webui: String,
57-
edit: String,
58-
tinyui: String,
57+
pub sself: String,
58+
pub webui: String,
59+
pub edit: String,
60+
pub tinyui: String,
5961
}
6062

6163
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
6264
#[allow(non_snake_case)]
6365
pub struct Links {
6466
#[serde(rename(serialize = "self"))]
6567
#[serde(rename(deserialize = "self"))]
66-
sself: String,
68+
#[serde(skip_serializing_if = "String::is_empty")]
69+
pub sself: String,
6770
#[serde(skip_serializing_if = "String::is_empty")]
6871
// #[serde(skip_deserializing_if = "String::is_empty")]
69-
next: String,
70-
base: String,
71-
context: String,
72+
pub next: String,
73+
pub base: String,
74+
pub context: String,
75+
}
76+
77+
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
78+
#[allow(non_snake_case)]
79+
pub struct SearchResultsLinks {
80+
pub base: String,
81+
pub context: String,
7282
}
7383

7484
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
7585
#[allow(non_snake_case)]
7686
pub struct ContentArrPage {
77-
id: String,
87+
pub id: String,
7888
#[serde(rename(serialize = "type"))]
7989
#[serde(rename(deserialize = "type"))]
80-
Type: String,
81-
status: String,
82-
title: String,
90+
pub Type: String,
91+
pub status: String,
92+
pub title: String,
8393
#[serde(skip_serializing_if = "Option::is_none")]
84-
extensions: Option<Extentions<String>>,
94+
pub extensions: Option<Extentions<String>>,
8595
#[serde(rename(deserialize = "_expandable"))]
86-
_expandable: Expandable,
87-
_links: ContentLinks,
96+
pub _expandable: Expandable,
97+
pub _links: ContentLinks,
8898
}
8999

90100
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
91101
#[allow(non_snake_case)]
92102
pub struct ContentResponse {
93-
results: Vec<ContentArrPage>,
94-
start: i8,
95-
limit: i8,
96-
size: i8,
103+
pub results: Vec<ContentArrPage>,
104+
pub start: i8,
105+
pub limit: i8,
106+
pub size: i8,
97107
// #[serde(skip_serializing_if = "Option::is_none")]
98108
#[serde(skip)]
99-
_links: Links,
109+
pub _links: Links,
100110
}
101111

102112
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
103113
pub struct User {
104-
login: String,
105-
id: u32,
114+
pub login: String,
115+
pub id: u32,
106116
}
107117

108118
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -227,4 +237,19 @@ pub mod models {
227237
pub body: String,
228238
pub descendants: String,
229239
}
240+
241+
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
242+
#[serde(rename_all = "camelCase")]
243+
pub struct SearchResults {
244+
pub results: Vec<ContentArrPage>,
245+
pub start: i8,
246+
pub limit: i8,
247+
pub size: i8,
248+
#[serde(rename = "totalSize")]
249+
pub total_size: i8,
250+
#[serde(rename = "cqlQuery")]
251+
pub cql_query: String,
252+
#[serde(skip)]
253+
pub _links: Links,
254+
}
230255
}

src/model/space.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
use serde::{Deserialize, Serialize};
2+
use crate::model::models::{Links};
23

34
#[derive(Deserialize, Serialize, Debug)]
45
pub struct Space {
56
id: i16,
67
key: String,
78
name: String,
9+
#[serde(rename(serialize = "type"))]
10+
#[serde(rename(deserialize = "type"))]
11+
ttype: String,
12+
_links: Links,
13+
_expandable: SpaceExpandable
14+
}
15+
16+
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
17+
#[allow(non_snake_case)]
18+
pub struct SpaceExpandable {
19+
pub metadata: String,
20+
pub icon: String,
21+
pub description: String,
22+
#[serde(rename(serialize = "retentionPolicy"))]
23+
#[serde(rename(deserialize = "retentionPolicy"))]
24+
pub retention_policy: String,
25+
pub homepage: String,
826
}
927

1028
#[derive(Deserialize, Serialize, Debug)]

src/pages.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod page_service {
22
use reqwest::Response;
3-
use crate::model::models::{Content, CreatePage, ContentResponse};
3+
use crate::model::models::{Content, CreatePage, ContentResponse, SearchResults};
4+
45

56
pub async fn get_page(url: &str, token: String, id: String) -> Content {
67
let request_url = format!("{url}/rest/api/content/{id}");
@@ -24,6 +25,17 @@ pub mod page_service {
2425
return serde_json::from_str(body.as_str()).unwrap();
2526
}
2627

28+
pub async fn get_descendants(url: &str, token: String, id: String) -> SearchResults {
29+
let request_url = format!("{url}/rest/api/content/search?cql=parent={id}");
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+
2739
pub async fn create_page(conf_url: &str, token: &str, page: CreatePage) -> String {
2840
let request_url = format!("{conf_url}/rest/api/content/");
2941
let client = reqwest::Client::new();
@@ -37,6 +49,17 @@ pub mod page_service {
3749
return created;
3850
}
3951

52+
pub async fn get_comments(conf_url: &str, token: &str) -> SearchResults {
53+
let request_url = format!("{conf_url}/rest/api/search?cql=type=comment");
54+
let client = reqwest::Client::new();
55+
let res = client.get(&request_url)
56+
.header("Content-Type", "application/json")
57+
.header("Authorization", format!("Basic {token}"))
58+
.send()
59+
.await.unwrap();
60+
return serde_json::from_str(res.text().await.unwrap().as_str()).unwrap();
61+
}
62+
4063
// todo
4164
pub async fn copy_page(conf_url: &str, token: &str, page: CreatePage) -> String {
4265
return String::from("");
@@ -47,7 +70,4 @@ pub mod page_service {
4770
}
4871

4972

50-
51-
52-
5373
}

src/spaces.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
pub mod spaces {
22

3-
pub async fn get_space() {
3+
use reqwest::Response;
4+
use crate::model::models::{Content, CreatePage, ContentResponse, SearchResults};
45

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();
515
}
616

7-
pub async fn create_space() {
8-
17+
pub async fn create_space(conf_url: &str, token: &str, page: CreateSpace) -> String {
18+
let request_url = format!("{conf_url}/rest/api/content/");
19+
let client = reqwest::Client::new();
20+
let res = client.post(&request_url)
21+
.json(&page)
22+
.header("Content-Type", "application/json")
23+
.header("Authorization", format!("Basic {token}"))
24+
.send()
25+
.await.unwrap();
26+
let created: String = res.text().await.unwrap();
27+
return created;
928
}
1029
}

0 commit comments

Comments
 (0)