1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- use chrono::NaiveDateTime;
- use serde::{Deserialize, Serialize};
-
- #[derive(Serialize, Deserialize, Debug)]
- pub struct OutgoingSnippet {
- pub title: String,
- pub body: String,
- pub filetype: String,
- }
-
- #[derive(Debug)]
- pub struct ServerConfig {
- pub hostname: String,
- pub protocol: String,
- pub port: i32,
- }
-
- #[derive(Serialize, Deserialize, Debug)]
- #[serde(untagged)]
- pub enum ApiResult {
- IncomingSnippet {
- id: i32,
- title: String,
- body: String,
- uuid: String,
- created_at: NaiveDateTime,
- },
- IncomingError {
- message: String,
- },
- }
-
- pub fn build_url_from_config(config: &ServerConfig) -> String {
- match config.port {
- 80 => format!("{}://{}", config.protocol, config.hostname),
- _ => format!("{}://{}:{}", config.protocol, config.hostname, config.port),
- }
- }
-
- pub fn create_snippet(
- snippet: OutgoingSnippet,
- config: &ServerConfig,
- ) -> Result<ApiResult, Box<std::error::Error>> {
- let client = reqwest::Client::new();
- let url = build_url_from_config(config);
- let resp: ApiResult = client
- .post(&format!("{}/api/snippets", url))
- .json(&snippet)
- .send()?
- .json()?;
-
- Ok(resp)
- }
|