|
@@ -9,14 +9,6 @@ pub struct OutgoingSnippet {
|
9
|
9
|
pub filetype: String,
|
10
|
10
|
}
|
11
|
11
|
|
12
|
|
-#[derive(Serialize, Deserialize, Debug)]
|
13
|
|
-pub struct IncomingSnippet {
|
14
|
|
- pub id: i32,
|
15
|
|
- pub title: String,
|
16
|
|
- pub body: String,
|
17
|
|
- pub created_at: NaiveDateTime,
|
18
|
|
-}
|
19
|
|
-
|
20
|
12
|
#[derive(Debug)]
|
21
|
13
|
pub struct ServerConfig {
|
22
|
14
|
pub hostname: String,
|
|
@@ -24,6 +16,20 @@ pub struct ServerConfig {
|
24
|
16
|
pub port: i32,
|
25
|
17
|
}
|
26
|
18
|
|
|
19
|
+#[derive(Serialize, Deserialize, Debug)]
|
|
20
|
+#[serde(untagged)]
|
|
21
|
+pub enum ApiResult {
|
|
22
|
+ IncomingSnippet {
|
|
23
|
+ id: i32,
|
|
24
|
+ title: String,
|
|
25
|
+ body: String,
|
|
26
|
+ created_at: NaiveDateTime,
|
|
27
|
+ },
|
|
28
|
+ IncomingError {
|
|
29
|
+ message: String,
|
|
30
|
+ },
|
|
31
|
+}
|
|
32
|
+
|
27
|
33
|
pub fn build_url_from_config(config: &ServerConfig) -> String {
|
28
|
34
|
match config.port {
|
29
|
35
|
80 => format!("{}://{}", config.protocol, config.hostname),
|
|
@@ -34,13 +40,14 @@ pub fn build_url_from_config(config: &ServerConfig) -> String {
|
34
|
40
|
pub fn create_snippet(
|
35
|
41
|
snippet: OutgoingSnippet,
|
36
|
42
|
config: &ServerConfig,
|
37
|
|
-) -> Result<IncomingSnippet, Box<std::error::Error>> {
|
|
43
|
+) -> Result<ApiResult, Box<std::error::Error>> {
|
38
|
44
|
let client = reqwest::Client::new();
|
39
|
45
|
let url = build_url_from_config(config);
|
40
|
|
- let resp: IncomingSnippet = client
|
|
46
|
+ let resp: ApiResult = client
|
41
|
47
|
.post(&format!("{}/api/snippets", url))
|
42
|
48
|
.json(&snippet)
|
43
|
49
|
.send()?
|
44
|
50
|
.json()?;
|
|
51
|
+
|
45
|
52
|
Ok(resp)
|
46
|
53
|
}
|