Browse Source

Create gists

master
Dylan Baker 5 years ago
parent
commit
6b2414e2db
5 changed files with 50 additions and 2 deletions
  1. 5
    0
      Cargo.lock
  2. 2
    0
      Cargo.toml
  3. 9
    2
      src/cli.rs
  4. 30
    0
      src/client.rs
  5. 4
    0
      src/main.rs

+ 5
- 0
Cargo.lock View File

@@ -335,6 +335,8 @@ version = "0.1.0"
335 335
 dependencies = [
336 336
  "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
337 337
  "reqwest 0.9.16 (registry+https://github.com/rust-lang/crates.io-index)",
338
+ "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
339
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
338 340
 ]
339 341
 
340 342
 [[package]]
@@ -982,6 +984,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
982 984
 name = "serde"
983 985
 version = "1.0.90"
984 986
 source = "registry+https://github.com/rust-lang/crates.io-index"
987
+dependencies = [
988
+ "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
989
+]
985 990
 
986 991
 [[package]]
987 992
 name = "serde_derive"

+ 2
- 0
Cargo.toml View File

@@ -7,3 +7,5 @@ edition = "2018"
7 7
 [dependencies]
8 8
 clap = "2.33.0"
9 9
 reqwest = "0.9.16"
10
+serde = { version = "1.0.90", features = ["derive"] }
11
+serde_json = "1.0"

+ 9
- 2
src/cli.rs View File

@@ -2,6 +2,8 @@ use clap::ArgMatches;
2 2
 use std::fs;
3 3
 use std::io;
4 4
 
5
+use crate::client::create_gist;
6
+
5 7
 pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
6 8
     let title = matches.value_of("title").unwrap();
7 9
     let input = matches.value_of("input");
@@ -14,8 +16,13 @@ pub fn new_gist(matches: &ArgMatches) -> io::Result<()> {
14 16
     let mut body = String::new();
15 17
     rdr.read_to_string(&mut body)?;
16 18
 
17
-    println!("title: {}", title);
18
-    println!("body: {}", body);
19
+    match create_gist(title, &body) {
20
+        Ok(resp) => println!(
21
+            "Success! Your new gist is available at http://localhost:8000/gists/{}",
22
+            resp.id
23
+        ),
24
+        Err(err) => println!("{}", err),
25
+    }
19 26
 
20 27
     Ok(())
21 28
 }

+ 30
- 0
src/client.rs View File

@@ -0,0 +1,30 @@
1
+use serde::{Deserialize, Serialize};
2
+
3
+#[derive(Serialize, Deserialize, Debug)]
4
+pub struct OutgoingGist {
5
+    pub title: String,
6
+    pub body: String,
7
+}
8
+
9
+#[derive(Serialize, Deserialize, Debug)]
10
+pub struct IncomingGist {
11
+    pub id: i32,
12
+    pub title: String,
13
+    pub body: String,
14
+}
15
+
16
+pub fn create_gist(title: &str, body: &str) -> Result<IncomingGist, Box<std::error::Error>> {
17
+    let client = reqwest::Client::new();
18
+    let gist = OutgoingGist {
19
+        title: title.to_string(),
20
+        body: body.to_string(),
21
+    };
22
+
23
+    let resp: IncomingGist = client
24
+        .post("http://localhost:8000/api/gists")
25
+        .json(&gist)
26
+        .send()?
27
+        .json()?;
28
+
29
+    Ok(resp)
30
+}

+ 4
- 0
src/main.rs View File

@@ -1,9 +1,13 @@
1 1
 extern crate clap;
2
+extern crate reqwest;
3
+extern crate serde;
4
+extern crate serde_json;
2 5
 
3 6
 use clap::{App, Arg, SubCommand};
4 7
 use std::io;
5 8
 
6 9
 mod cli;
10
+mod client;
7 11
 
8 12
 fn main() -> io::Result<()> {
9 13
     let matches = App::new("gist")

Loading…
Cancel
Save