Skip to content

Commit 2649549

Browse files
committed
creating pages
1 parent 58aa2c3 commit 2649549

File tree

5 files changed

+50
-46
lines changed

5 files changed

+50
-46
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ pub mod helpers {
22
use rand::Rng;
33

44
pub fn rand_string(size: i32) -> String {
5-
let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut \
6-
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip \
7-
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
8-
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
5+
let text = "lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut \
6+
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip \
7+
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu \
8+
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui \
9+
officia deserunt mollit anim id est laborum.";
910
let words = text.split(" ").collect::<Vec<&str>>();
1011
let mut lorem = "".to_string();
11-
for a in 1..size {
12+
for a in 0..size {
1213
let mut num = rand::thread_rng().gen_range(0..words.len());
1314
lorem = [lorem, words[num].to_string()].join(" ");
1415
}

src/main.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::iter::Map;
88
use reqwest::{Body, Error};
99
use serde_json::{json, Value};
1010
use tokio::time::Instant;
11-
use crate::models::models::{Ancestor, create_page, CreatePage, CreatePageSpace, PageBody, Storage};
12-
use crate::pages::page_service::create_page;
11+
use crate::models::models::{Ancestor, CreatePage, CreatePageSpace, PageBody, Storage};
12+
use crate::pages::page_service::{create_page, get_page};
1313

1414

1515
#[tokio::main]
@@ -21,52 +21,40 @@ async fn main() -> Result<(), Error> {
2121
let token = base64::encode(b"admin:admin");
2222
let conf_url = "http://localhost:8110";
2323

24-
// CREATE PAGE
25-
let req = CreatePage {
26-
title: "Rust page 2".to_string(),
27-
ctype: "page".to_string(),
28-
space: CreatePageSpace {
29-
key: "dev16".to_string(),
30-
},
31-
body: PageBody {
32-
storage: Storage {
33-
representation: "storage".to_string(),
34-
value: "lorem...".to_string(),
35-
},
36-
},
37-
ancestors: vec![Ancestor {
38-
id: 1213317,
39-
}],
40-
};
41-
let resp = create_page(&conf_url, &token, req).await;
42-
println!("{:?}", resp);
24+
// get page
25+
let page = get_page(conf_url, token, "1213317".to_string()).await;
26+
println!("{:?}", page);
4327

4428

45-
// create several pages
46-
// for a in 21..=40 {
47-
// let mut to_create: CreatePage = CreatePage {
48-
// title: format!("RUST page {a}"),
29+
// CREATE PAGE
30+
// let space_key = "dev16";
31+
// let parent = 1213317;
32+
//
33+
// for a in 40..42 {
34+
// let title = format!("Rust page {a}");
35+
//
36+
// let req = CreatePage {
37+
// title: title.to_string(),
4938
// ctype: "page".to_string(),
5039
// space: CreatePageSpace {
51-
// key: "DEV12".to_string(),
40+
// key: space_key.to_string(),
5241
// },
5342
// body: PageBody {
5443
// storage: Storage {
5544
// representation: "storage".to_string(),
56-
// value: rand_string(100),
57-
// }
45+
// value: helpers::helpers::rand_string(30).to_string(),
46+
// },
5847
// },
5948
// ancestors: vec![Ancestor {
60-
// id: 1048683
61-
// }]
49+
// id: parent,
50+
// }],
6251
// };
63-
//
64-
// println!("{:?}", serde_json::to_string(&to_create));
65-
//
66-
// let created = create_page(&conf_url.to_string(), &token,to_create);
67-
// let fin = created.await;
52+
// let resp = create_page(&conf_url, &token, req).await;
53+
// println!("{:?}", resp);
6854
// }
6955

56+
57+
7058
let mut end: u128 = start.elapsed().as_millis();
7159
println!("{:?}", println!(">>> Action took :: {end} millis"));
7260

src/models.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
pub mod models {
2+
use serde_json::{json, Value};
3+
use serde::{Deserialize, Serialize};
4+
25
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
36
#[allow(non_snake_case)]
47
pub struct Extentions {

src/pages.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
pub mod page_service {
2-
use serde_json::{json, Value};
3-
use serde::{Deserialize, Serialize};
4-
use crate::models::models::CreatePage;
2+
use reqwest::Response;
3+
use crate::models::models::{Content, CreatePage};
4+
5+
pub async fn get_page(url: &str, token: String, id: String) -> Content {
6+
let request_url = format!("{url}/rest/api/content/{id}");
7+
let client = reqwest::Client::new();
8+
let resp: Response = client.get(&request_url)
9+
.header("Authorization", format!("Basic {token}"))
10+
.header("Accept", "application/json")
11+
.send().await.unwrap();
12+
let body = resp.text().await.unwrap();
13+
println!("{:?}", body); // todo - debug
14+
return serde_json::from_str(body.as_str()).unwrap();
15+
}
516

617
pub async fn create_page(conf_url: &str, token: &str, page: CreatePage) -> String {
718
let request_url = format!("{conf_url}/rest/api/content/");
819
let client = reqwest::Client::new();
920
let res = client.post(&request_url)
1021
.json(&page)
22+
.header("Content-Type", "application/json")
1123
.header("Authorization", format!("Basic {token}"))
1224
.send()
1325
.await.unwrap();

0 commit comments

Comments
 (0)