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.2KB

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