The CLI frontend to a gist server written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.rs 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use chrono::NaiveDateTime;
  2. use serde::{Deserialize, Serialize};
  3. #[derive(Serialize, Deserialize, Debug)]
  4. pub struct OutgoingSnippet {
  5. pub title: String,
  6. pub body: String,
  7. pub formatted_body: String,
  8. pub filetype: String,
  9. }
  10. #[derive(Serialize, Deserialize, Debug)]
  11. pub struct IncomingSnippet {
  12. pub id: i32,
  13. pub title: String,
  14. pub body: String,
  15. pub created_at: NaiveDateTime,
  16. }
  17. #[derive(Debug)]
  18. pub struct ServerConfig {
  19. pub hostname: String,
  20. pub protocol: String,
  21. pub port: i32,
  22. }
  23. pub fn build_url_from_config(config: &ServerConfig) -> String {
  24. match config.port {
  25. 80 => format!("{}://{}", config.protocol, config.hostname),
  26. _ => format!("{}://{}:{}", config.protocol, config.hostname, config.port),
  27. }
  28. }
  29. pub fn create_snippet(
  30. snippet: OutgoingSnippet,
  31. config: &ServerConfig,
  32. ) -> Result<IncomingSnippet, Box<std::error::Error>> {
  33. let client = reqwest::Client::new();
  34. let url = build_url_from_config(config);
  35. let resp: IncomingSnippet = client
  36. .post(&format!("{}/api/snippets", url))
  37. .json(&snippet)
  38. .send()?
  39. .json()?;
  40. Ok(resp)
  41. }