Skip to content

Commit

Permalink
Update update.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
floris-xlx committed Jul 5, 2024
1 parent 90b7fc7 commit d8026bf
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@
//! and `Err(String)` contains an error message in case of failure.
//!
use crate::SupabaseClient;
use serde_json::{
json,
Value
};
use reqwest::{
Client,
Response
};

use reqwest::{Client, Response};
use serde_json::{json, Value};

impl SupabaseClient {
/// Updates a row in the table, based on the id
pub async fn update(
pub async fn update(&self, table_name: &str, id: &str, body: Value) -> Result<String, String> {
Self::update_with_column_name(self, table_name, "id", id, body).await
}

/// Updates a row in the table, based on the column name
pub async fn update_with_column_name(
&self,
table_name: &str,
column_name: &str,
id: &str,
body: Value
) -> Result<(), String> {

body: Value,
) -> Result<String, String> {
// endpoint and client construction
let endpoint: String = format!(
"{}/rest/v1/{}?id=eq.{}",
self.url, table_name, id
"{}/rest/v1/{}?{}=eq.{}",
self.url, table_name, column_name, id
);
let client: Client = Client::new();

Expand All @@ -74,63 +72,61 @@ impl SupabaseClient {
.header("Content-Type", "application/json")
.body(body.to_string())
.send()
.await {
Ok(response) => response,
Err(error) => return Err(error.to_string())
};

.await
{
Ok(response) => response,
Err(error) => return Err(error.to_string()),
};

if response.status().is_success() {
Ok(())
Ok(id.to_string())
} else {
Err(response.status().to_string())
}
}


/// Creates a row in the table, or updates if the id already exists
pub async fn upsert(
&self,
table_name: &str,
id: &str,
mut body: Value
mut body: Value,
) -> Result<String, String> {

let endpoint: String = format!("{}/rest/v1/{}", self.url, table_name);

#[cfg(feature = "rustls")]
let client = Client::builder().use_rustls_tls().build().unwrap();

#[cfg(not(feature = "rustls"))]
let client = Client::new();


#[cfg(feature = "nightly")]
use crate::nightly::print_nightly_warning;
#[cfg(feature = "nightly")]
print_nightly_warning();

body["id"] = json!(id);

let response: Response = match client
.post(&endpoint)
.header("apikey", &self.api_key)
.header("Authorization", format!("Bearer {}", &self.api_key))
.header("Content-Type", "application/json")
.header("x_client_info", "supabase-rs/0.3.2")
.header("x_client_info", "supabase-rs/0.3.1")
.header("Prefer", "resolution=merge-duplicates")
.header("Prefer", "return=representation")
.body(body.to_string())
.send()
.await {
Ok(response) => response,
Err(e) => return Err(e.to_string())
};
.await
{
Ok(response) => response,
Err(e) => return Err(e.to_string()),
};

if response.status().is_success() {
Ok(id.to_string())

} else {
Err(response.status().to_string())
}
}
}
}

0 comments on commit d8026bf

Please sign in to comment.