pub async fn put<T>(&self, item: Item<T>) -> Result<String>
    where
        T: Serialize,
    {
        let url = format!(
            "{}/{}/items",
            self.url,
            self.base_name.as_ref().ok_or(Error::BaseNameNotPresent)?,
        );

        let Item { key, value } = item;
        let mut value = serde_json::to_value(value).map_err(|_| Error::JSONSerializingFailed)?;

        if !value.is_object() {
            value = serde_json::json!({ "value": value });
        }

        if let Some(x) = key {
            value["key"] = serde_json::json!(x);
        }

        let req_body = serde_json::json!({ "items": [value] });

        let value: serde_json::Value = self
            .client
            .put(&url)
            .json(&req_body)
            .send()
            .await
            .map_err(|_| Error::RequestSendError)?
            .error_for_status()
            .map_err(|_| Error::BadRequest)?
            .json()
            .await
            .map_err(|_| Error::JSONDeserializingFailed)?;

        let key = value["processed"]["items"]
            .as_array()
            .ok_or(Error::JSONDeserializingFailed)?[0]["key"]
            .as_str()
            .ok_or(Error::JSONDeserializingFailed)?
            .to_string();

        Ok(key)
    }
/// Stores an item.
/// This request overwrites an item if the key already exists.
///
/// Returns the key, if successful.
///
/// # Arguments
///
/// * `item`: An `Item`.
///
/// # Errors
///
/// * [`Error::BaseNameNotPresent`](crate::Error::BaseNameNotPresent)
/// * [`Error::JSONSerializingFailed`](crate::Error::JSONSerializingFailed)
/// * [`Error::RequestSendError`](crate::Error::RequestSendError)
/// * [`Error::BadRequest`](crate::Error::BadRequest)
/// * [`Error::JSONDeserializingFailed`](crate::Error::JSONDeserializingFailed)
///
/// # Examples
///
/// ```
/// use deta::{Deta, Item};
/// # #[tokio::main]
/// # async fn main() -> deta::Result<()> {
/// let deta = Deta::new()?;
///
/// let base = deta.base("main");
/// # base.delete(5);
/// let item = Item::new_with_key("put_id", 5);
/// assert_eq!(base.put(item).await?, "put_id");
/// # Ok(())
/// # }
/// ```
Stores an item.
This request overwrites an item if the key already exists.

Returns the key, if successful.
Stores an item . This request overwrites an item if the key already exists . Returns the key if successful .
pub async fn put<T>(&self, item: Item<T>) -> Result<String>
    where
        T: Serialize,
    {
        let url = format!(
            "{}/{}/items",
            self.url,
            self.base_name.as_ref().ok_or(Error::BaseNameNotPresent)?,
        );
        let Item { key, value } = item;
        let mut value = serde_json::to_value(value).map_err(|_| Error::JSONSerializingFailed)?;
        if !value.is_object() {
            value = serde_json::json!({ "value": value });
        }
        if let Some(x) = key {
            value["key"] = serde_json::json!(x);
        }
        let req_body = serde_json::json!({ "items": [value] });
        let value: serde_json::Value = self
            .client
            .put(&url)
            .json(&req_body)
            .send()
            .await
            .map_err(|_| Error::RequestSendError)?
            .error_for_status()
            .map_err(|_| Error::BadRequest)?
            .json()
            .await
            .map_err(|_| Error::JSONDeserializingFailed)?;
        let key = value["processed"]["items"]
            .as_array()
            .ok_or(Error::JSONDeserializingFailed)?[0]["key"]
            .as_str()
            .ok_or(Error::JSONDeserializingFailed)?
            .to_string();
        Ok(key)
    }
pub async fn put < T > ( & self , item : Item < T > ) -> Result < String > where T : Serialize , { let url = format ! ( " " self url self base_name as_ref ( ) ok_or ( Error BaseNameNotPresent ) ) ; let Item { key , value } = item ; let mut value = serde_json :: to_value ( value ) . map_err ( | _ | Error :: JSONSerializingFailed ) ? ; if ! value . is_object ( ) { value = serde_json :: json ! ( { " " value } ) ; } if let Some ( x ) = key { value [ " " ] = serde_json :: json ! ( x ) ; } let req_body = serde_json :: json ! ( { " " [ value ] } ) ; let value : serde_json :: Value = self . client . put ( & url ) . json ( & req_body ) . send ( ) . await . map_err ( | _ | Error :: RequestSendError ) ? . error_for_status ( ) . map_err ( | _ | Error :: BadRequest ) ? . json ( ) . await . map_err ( | _ | Error :: JSONDeserializingFailed ) ? ; let key = value [ " " ] [ " " ] . as_array ( ) . ok_or ( Error :: JSONDeserializingFailed ) ? [ 0 ] [ " " ] . as_str ( ) . ok_or ( Error :: JSONDeserializingFailed ) ? . to_string ( ) ; Ok ( key ) }
Stores an item.
Stores an item .
&self
item
Item<T>
&self
item
Item<T>
errors
None
examples
None
#[tokio::main]
None
async fn main() -> deta::result<()> {
let deta = Deta::new()?.

let base = deta.base("main").
let deta = Deta :: new () ? . let base = deta . base ( " main " ) .
base.delete(5);
let item = Item::new_with_key("put_id", 5);
assert_eq!(base.put(item).await?, "put_id").
let item = Item :: new_with_key ( " put_id " 5 ) ; assert_eq! ( base . put ( item ) . await? " put_id " ) .
ok(())
None
}
None