Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish between PUT/POST requests #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "firebase-rs"
edition = "2021"
version = "2.0.5"
version = "3.0.0"
description = "Rust based Firebase library"
readme = "README.md"
repository = "https://github.com/emreyalvac/firebase-rs"
documentation = "https://docs.rs/firebase-rs/2.0.4/firebase_rs/"
documentation = "https://docs.rs/firebase-rs/3.0.0/firebase_rs/"
license = "MIT"
authors = ["Emre YALVAÇ <[email protected]>"]
exclude = ["examples/*", "tests/*"]
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub const EXPORT: &str = "export";
pub enum Method {
GET,
POST,
PUT,
DELETE,
PATCH,
}
Expand Down
65 changes: 50 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl Firebase {
/// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap();
/// ```
pub fn new(uri: &str) -> UrlParseResult<Self>
where
Self: Sized,
where
Self: Sized,
{
match check_uri(&uri) {
Ok(uri) => Ok(Self { uri }),
Expand All @@ -41,8 +41,8 @@ impl Firebase {
/// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap();
/// ```
pub fn auth(uri: &str, auth_key: &str) -> UrlParseResult<Self>
where
Self: Sized,
where
Self: Sized,
{
match check_uri(&uri) {
Ok(mut uri) => {
Expand Down Expand Up @@ -90,9 +90,7 @@ impl Firebase {

let mut uri = self.uri.clone();
uri.set_path(&format!("{}.json", new_path));
Self {
uri,
}
Self { uri }
}

/// ```
Expand Down Expand Up @@ -144,6 +142,20 @@ impl Firebase {
Err(_) => Err(RequestError::NetworkError),
}
}
Method::PUT => {
if !data.is_some() {
return Err(RequestError::SerializeError);
}

let request = client.put(self.uri.to_string()).json(&data).send().await;
match request {
Ok(response) => {
let data = response.text().await.unwrap();
Ok(Response { data })
}
Err(_) => Err(RequestError::NetworkError),
}
}
Method::PATCH => {
if !data.is_some() {
return Err(RequestError::SerializeError);
Expand Down Expand Up @@ -171,8 +183,8 @@ impl Firebase {
}

async fn request_generic<T>(&self, method: Method) -> RequestResult<T>
where
T: Serialize + DeserializeOwned + Debug,
where
T: Serialize + DeserializeOwned + Debug,
{
let request = self.request(method, None).await;

Expand Down Expand Up @@ -202,8 +214,31 @@ impl Firebase {
/// # }
/// ```
pub async fn set<T>(&self, data: &T) -> RequestResult<Response>
where
T: Serialize + DeserializeOwned + Debug,
where
T: Serialize + DeserializeOwned + Debug,
{
let data = serde_json::to_value(&data).unwrap();
self.request(Method::PUT, Some(data)).await
}
Comment on lines +217 to +222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a breaking change in the lib. I suggest leave set as the POST and create another put method that will do the PUT request.


/// ```
/// use firebase_rs::Firebase;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug)]
/// struct User {
/// name: String
/// }
///
/// # async fn run() {
/// let user = User { name: String::default() };
/// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap().at("users");
/// let users = firebase.insert(&user).await;
/// # }
/// ```
pub async fn insert<T>(&self, data: &T) -> RequestResult<Response>
where
T: Serialize + DeserializeOwned + Debug,
{
let data = serde_json::to_value(&data).unwrap();
self.request(Method::POST, Some(data)).await
Expand Down Expand Up @@ -249,8 +284,8 @@ impl Firebase {
/// # }
/// ```
pub async fn get<T>(&self) -> RequestResult<T>
where
T: Serialize + DeserializeOwned + Debug,
where
T: Serialize + DeserializeOwned + Debug,
{
self.request_generic::<T>(Method::GET).await
}
Expand Down Expand Up @@ -283,8 +318,8 @@ impl Firebase {
/// # }
/// ```
pub async fn update<T>(&self, data: &T) -> RequestResult<Response>
where
T: DeserializeOwned + Serialize + Debug,
where
T: DeserializeOwned + Serialize + Debug,
{
let value = serde_json::to_value(&data).unwrap();
self.request(Method::PATCH, Some(value)).await
Expand Down