Skip to content

Commit

Permalink
basic endpoint trait
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Sep 25, 2023
1 parent b459c5d commit c464f9d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Project specific ignores
.env
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_builder = "~0.12"
reqwest = { version = "~0.11.19", features = [
"blocking",
"json",
], default-features = false, optional = true }
thiserror = { version = "^1.0.2", optional = true }
http = "~0.2"
serde = { version = "~1.0.103", features = ["derive"] }
serde_json = "^1.0.25"
serde_urlencoded = "~0.7"
url = "^2.1"
dotenvy = "0.15.7"
4 changes: 4 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod endpoint;
mod error;

pub use self::error::BodyError;
27 changes: 27 additions & 0 deletions src/api/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::Cow;

use http::{self, header, Method, Request};

use crate::api::BodyError;

pub trait Endpoint {
/// The HTTP method to use for the endpoint.
fn method(&self) -> Method;
/// The path to the endpoint.
fn endpoint(&self) -> Cow<'static, str>;

/// The body for the endpoint.
///
/// Returns the `Content-Encoding` header for the data as well as the data itself.
///
/// # Errors
/// This method returns an error if the body could not be serialized to JSON.
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
Ok(None)
}
}
19 changes: 19 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed
// except according to those terms.

use thiserror::Error;

/// Errors which may occur when creating form data.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BodyError {
/// Body data could not be serialized from form parameters.
#[error("failed to URL encode form parameters: {}", source)]
UrlEncoded {
/// The source of the error.
#[from]
source: serde_urlencoded::ser::Error,
},
}
14 changes: 1 addition & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
pub mod api;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

0 comments on commit c464f9d

Please sign in to comment.