The CLI frontend to a gist server written in Rust
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

client.rs 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. uuid: String,
  23. created_at: NaiveDateTime,
  24. },
  25. IncomingError {
  26. message: String,
  27. },
  28. }
  29. pub fn build_url_from_config(config: &ServerConfig) -> String {
  30. match config.port {
  31. 80 => format!("{}://{}", config.protocol, config.hostname),
  32. _ => format!("{}://{}:{}", config.protocol, config.hostname, config.port),
  33. }
  34. }
  35. pub fn create_snippet(
  36. snippet: OutgoingSnippet,
  37. config: &ServerConfig,
  38. ) -> Result<ApiResult, Box<std::error::Error>> {
  39. let client = reqwest::Client::new();
  40. let url = build_url_from_config(config);
  41. let resp: ApiResult = client
  42. .post(&format!("{}/api/snippets", url))
  43. .json(&snippet)
  44. .send()?
  45. .json()?;
  46. Ok(resp)
  47. }