1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
///! Smartsheet API v2 implementation in Rust
///!
use crate::auth::auth_token;
use crate::builders::ParamBuilder;
use crate::constants::{API_ENDPOINT, ENV_VAR_NAME};
use crate::models::*;
use crate::status::raise_for_status;
use crate::types::Result;
use crate::utils::{into_struct_from_slice, resp_into_struct};
use std::io::{Error, ErrorKind};
use std::time::Instant;
use hyper::client::HttpConnector;
use hyper::header::AUTHORIZATION;
use hyper::{Body, Client, Request};
use hyper_tls::HttpsConnector;
use log::{debug, warn};
/// Client implementation for making requests to the *Smartsheet
/// API v2*
///
/// # Links
/// - [`smartsheet-rs`](https://docs.rs/smartsheet-rs)
/// - [Official Documentation](https://smartsheet-platform.github.io/api-docs/)
///
pub struct SmartsheetApi<'a> {
bearer_token: String,
client: Client<HttpsConnector<HttpConnector>>,
endpoint: &'a str,
}
impl<'a> SmartsheetApi<'a> {
/// Initialize a new `SmartsheetApi` object from an API access token.
pub fn from_token(token: &str) -> Self {
Self::new(API_ENDPOINT, token)
}
/// Initialize a new `SmartsheetApi` object from an API access token,
/// assuming this is currently set in the environment.
pub fn from_env() -> Result<Self> {
let token: String = match std::env::var(ENV_VAR_NAME) {
Ok(val) => Ok(val),
Err(_) => Err(Error::new(
ErrorKind::NotFound,
format!(
"Environment variable `{name}` must be set.",
name = ENV_VAR_NAME
),
)),
}?;
Ok(Self::new(API_ENDPOINT, &token))
}
/// Initialize a new `SmartsheetApi` object from a (custom) base API
/// endpoint, and an access token.
pub fn from_endpoint_and_token(endpoint: &'a str, token: &str) -> Self {
Self::new(endpoint, token)
}
/// Constructor function, for internal use
fn new(endpoint: &'a str, token: &str) -> Self {
let bearer_token = auth_token(token);
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
Self {
bearer_token,
client,
endpoint,
}
}
/// **List Sheets** - Gets a list of all sheets that the user has access
/// to in alphabetical order by name. The list contains an abbreviated
/// Sheet object for each sheet.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#list-sheets
///
pub async fn list_sheets(&self) -> Result<IndexResult<Sheet>> {
self.list_sheets_with_params(None, None, None).await
}
/// **List Sheets** - Gets a list of all sheets that the user has access
/// to in alphabetical order by name, with included _query parameters_.
/// The list contains an abbreviated Sheet object for each sheet.
///
/// # Arguments
///
/// * `include` - A comma-separated list of elements to include in the response.
/// * `include_all` - If true, include all results (i.e. do not paginate).
/// * `modified_since` - Return sheets modified since a provided datetime.
/// Date should be in ISO-8601 format, for example,
/// `2020-01-30T13:25:32-07:00`.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#list-sheets
///
pub async fn list_sheets_with_params(
&self,
include: Option<Vec<ListSheetIncludeFlags>>,
include_all: Option<bool>,
modified_since: Option<&'a str>, // TODO change this to a date type maybe
) -> Result<IndexResult<Sheet>> {
let mut url = format!("{}/{}", self.endpoint, "sheets");
let mut params = ParamBuilder::new();
params.insert_comma_separated_values("include", include);
params.insert_value("includeAll", include_all);
params.insert_value("modifiedSince", modified_since);
params.add_query_to_url(&mut url);
debug!("URL: {}", url);
let req = Request::get(&url)
.header(AUTHORIZATION, &self.bearer_token)
.body(Body::empty())?;
let mut res = self.client.request(req).await?;
raise_for_status(url, &mut res).await?;
let start = Instant::now();
let sheets = into_struct_from_slice(res).await?;
debug!("Deserialize: {:?}", start.elapsed());
Ok(sheets)
}
/// **Get Sheet** - Retrieves the specified sheet. Returns the sheet,
/// including rows, and optionally populated with discussion and
/// attachment objects.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the rows and data for.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-sheet
/// - https://smartsheet-platform.github.io/api-docs/#row-include-flags
///
pub async fn get_sheet(&self, sheet_id: u64) -> Result<Sheet> {
self.get_sheet_with_params(sheet_id, None, None, None, None, None, None)
.await
}
/// **Get Sheet** - Retrieves the specified sheet, with included
/// _query parameters_. Returns the sheet, including rows, and optionally
/// populated with discussion and attachment objects.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the rows and data for.
/// * `include` - A comma-separated list of elements to include in the response.
/// * `exclude` - A comma-separated list of elements to _not_ include in the response.
/// * `row_ids` - A comma-separated list of Row IDs on which to filter the
/// rows included in the result.
/// * `row_numbers` - A comma-separated list of Row numbers on which to
/// filter the rows included in the result. Non-existent
/// row numbers are ignored.
/// * `column_ids` - A comma-separated comma-separated list of Column IDs.
/// The response will contain only the specified columns
/// in the 'columns' array, and individual rows' 'cells'
/// array will only contain cells in the specified columns.
/// * `rows_modified_since` - Return rows modified since a provided datetime.
/// Date should be in ISO-8601 format, for example,
/// `2020-01-30T13:25:32-07:00`.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-sheet
/// - https://smartsheet-platform.github.io/api-docs/#row-include-flags
///
pub async fn get_sheet_with_params(
&self,
sheet_id: u64,
include: Option<Vec<SheetIncludeFlags>>,
exclude: Option<Vec<SheetExcludeFlags>>,
row_ids: Option<Vec<u64>>,
row_numbers: Option<Vec<u64>>,
column_ids: Option<Vec<u64>>,
rows_modified_since: Option<&'a str>, // TODO change this to a date type maybe
) -> Result<Sheet> {
let mut url = format!("{}/{}/{}", self.endpoint, "sheets", sheet_id);
let mut params = ParamBuilder::new();
params.insert_comma_separated_values("include", include);
params.insert_comma_separated_values("exclude", exclude);
params.insert_comma_separated_values("rowIds", row_ids);
params.insert_comma_separated_values("rowNumbers", row_numbers);
params.insert_comma_separated_values("columnIds", column_ids);
params.insert_value("rowsModifiedSince", rows_modified_since);
params.add_query_to_url(&mut url);
debug!("URL: {}", url);
let req = Request::get(&url)
.header(AUTHORIZATION, &self.bearer_token)
.body(Body::empty())?;
let mut res = self.client.request(req).await?;
raise_for_status(url, &mut res).await?;
let start = Instant::now();
// Note: I've timed the different methods for converting response data
// to a `struct` type, and found the buffered reader approach to work
// out the best for this approach. The response time seems to be quite
// stable where the reader implementation is used.
// 1. Bytes
// let sheet = into_struct_from_slice(res).await?;
// 2. String
// let sheet = into_struct_from_str(res).await?;
// 3. (Buffered) Reader
let sheet = resp_into_struct(res).await?;
debug!("Deserialize: {:?}", start.elapsed());
Ok(sheet)
}
/// **Get Row** - Retrieves the specified row from a sheet.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the rows from.
/// * `row_id` - The specified row to retrieve.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-row
///
pub async fn get_row(&self, sheet_id: u64, row_id: u64) -> Result<Row> {
self.get_row_with_params(sheet_id, row_id, None, None, None)
.await
}
/// **Get Row** - Retrieves the specified row from a sheet, with included _column data_.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the rows from.
/// * `row_id` - The specified row to retrieve.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-row
///
pub async fn get_row_with_column_data(&self, sheet_id: u64, row_id: u64) -> Result<Row> {
let include_flags = Some(vec![RowIncludeFlags::Columns]);
self.get_row_with_params(sheet_id, row_id, include_flags, None, None)
.await
}
/// **Get Row** - Retrieves the specified row from a sheet, with included _query parameters_.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the rows from.
/// * `row_id` - The specified row to retrieve.
/// * `include` - A comma-separated list of elements to include in the response.
/// * `exclude` - A comma-separated list of elements to _not_ include in the response.
/// * `level` - Specifies whether multi-contact data is returned in a
/// backwards-compatible, text format, or as multi-contact data.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-row
/// - https://smartsheet-platform.github.io/api-docs/#row-include-flags
///
pub async fn get_row_with_params(
&self,
sheet_id: u64,
row_id: u64,
include: Option<Vec<RowIncludeFlags>>,
exclude: Option<Vec<RowExcludeFlags>>,
level: Option<Level>,
) -> Result<Row> {
let mut url: String = format!(
"{}/{}/{}/{}/{}",
self.endpoint, "sheets", sheet_id, "rows", row_id
);
let mut params = ParamBuilder::new();
params.insert_comma_separated_values("include", include);
params.insert_comma_separated_values("exclude", exclude);
params.insert_value("level", level);
params.add_query_to_url(&mut url);
debug!("URL: {}", url);
let req = Request::get(&url)
.header(AUTHORIZATION, &self.bearer_token)
.body(Body::empty())?;
let mut res = self.client.request(req).await?;
raise_for_status(url, &mut res).await?;
let start = Instant::now();
// asynchronously aggregate the chunks of the body
let row = into_struct_from_slice(res).await?;
debug!("Deserialize: {:?}", start.elapsed());
Ok(row)
}
/// **List Columns** - Gets a list of all columns belonging to the specified sheet.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#list-columns
///
pub async fn list_columns(&self, sheet_id: u64) -> Result<IndexResult<Column>> {
self.list_columns_with_params(sheet_id, None, None, None)
.await
}
/// **List Columns** - Gets a list of all columns belonging to the
/// specified sheet, with included _query parameters_.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the columns from.
/// * `level` - Specifies whether multi-contact data is returned in a
/// backwards-compatible, text format, or as multi-contact data.
/// * `include` - A comma-separated list of elements to include in the response.
/// * `include_all` - If true, include all results (i.e. do not paginate).
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#list-columns
///
pub async fn list_columns_with_params(
&self,
sheet_id: u64,
level: Option<Level>,
include: Option<Vec<ColumnIncludeFlags>>,
include_all: Option<bool>,
) -> Result<IndexResult<Column>> {
let mut url = format!("{}/{}/{}/{}", self.endpoint, "sheets", sheet_id, "columns");
let mut params = ParamBuilder::new();
params.insert_value("level", level);
params.insert_comma_separated_values("include", include);
params.insert_value("includeAll", include_all);
params.add_query_to_url(&mut url);
debug!("URL: {}", url);
let req = Request::get(&url)
.header(AUTHORIZATION, &self.bearer_token)
.body(Body::empty())?;
let mut res = self.client.request(req).await?;
raise_for_status(url, &mut res).await?;
let start = Instant::now();
let columns = into_struct_from_slice(res).await?;
debug!("Deserialize: {:?}", start.elapsed());
Ok(columns)
}
/// **Get Column** - Retrieves a column by *id* from the specified sheet.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the column for.
/// * `column_id` - The Column Id to retrieve the data for.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-column
///
pub async fn get_column(&self, sheet_id: u64, column_id: u64) -> Result<Column> {
self.get_column_with_params(sheet_id, column_id, None, None)
.await
}
/// **Get Column** - Retrieves a column by *id* from the specified sheet,
/// with included _query parameters_.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the column for.
/// * `column_id` - The Column Id to retrieve the data for.
/// * `level` - Specifies whether multi-contact data is returned in a
/// backwards-compatible, text format, or as multi-contact data.
/// * `include` - A comma-separated list of elements to include in the response.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#get-column
///
pub async fn get_column_with_params(
&self,
sheet_id: u64,
column_id: u64,
level: Option<Level>,
include: Option<Vec<ColumnIncludeFlags>>,
) -> Result<Column> {
let mut url = format!(
"{}/{}/{}/{}/{}",
self.endpoint, "sheets", sheet_id, "columns", column_id
);
let mut params = ParamBuilder::new();
params.insert_value("level", level);
params.insert_comma_separated_values("include", include);
params.add_query_to_url(&mut url);
debug!("URL: {}", url);
let req = Request::get(&url)
.header(AUTHORIZATION, &self.bearer_token)
.body(Body::empty())?;
let mut res = self.client.request(req).await?;
raise_for_status(url, &mut res).await?;
let start = Instant::now();
let column = into_struct_from_slice(res).await?;
debug!("Deserialize: {:?}", start.elapsed());
Ok(column)
}
/// **Get Sheet By Name** - Convenience function to retrieve a specified
/// sheet by name. Used for those times when you don't know the Sheet Id.
///
/// This will internally call `list_sheets` and then filter the response
/// data by the sheet name. It returns the first matching name.
///
/// Returns the sheet, including rows, and optionally populated with
/// discussion and attachment objects.
///
/// # Arguments
///
/// * `sheet_name` - The name of the Smartsheet to filter results by.
///
#[deprecated(
since = "0.2.0",
note = "please cache the sheet id and use `get_sheet` instead"
)]
pub async fn get_sheet_by_name(&self, sheet_name: &'a str) -> Result<Sheet> {
// Display a warning that the usage of this method is not recommended
warn!(
"{}",
"Calling `get_sheet_by_name()` is not recommended; it's \
preferable to cache the sheet ID and call \
`get_sheet()` instead."
);
// Get a fresh list of sheets
let result = self.list_sheets_with_params(None, Some(true), None).await?;
// Find the sheet by the provided name
for sheet in result.data {
if sheet.name == sheet_name {
return Ok(sheet);
}
}
Err(Box::from(Error::new(
ErrorKind::NotFound,
format!("The provided sheet `{}` was not found", sheet_name),
)))
}
/// **Get Column By Title** - Convenience function to retrieve a specified
/// column by title (name). Used for those times when you don't know the
/// Column Id.
///
/// This will internally call `list_columns` and then filter the response
/// data by the column title. It returns the first matching name.
///
/// # Arguments
///
/// * `sheet_id` - The Smartsheet to retrieve the column from.
/// * `column_title` - The name of the column to filter results by.
///
///
#[deprecated(
since = "0.2.0",
note = "please cache the column id and use `get_column` instead"
)]
pub async fn get_column_by_title(
&self,
sheet_id: u64,
column_title: &'a str,
) -> Result<Column> {
// Display a warning that the usage of this method is not recommended
warn!(
"{}",
"Calling `get_column_by_title()` is not recommended; it's \
preferable to cache the column ID and call \
`get_column()` instead."
);
// Get a fresh list of columns
let result = self
.list_columns_with_params(sheet_id, None, None, Some(true))
.await?;
// Find the column by the provided name
for column in result.data {
if column.title == column_title {
return Ok(column);
}
}
Err(Box::from(Error::new(
ErrorKind::NotFound,
format!("The provided column `{}` was not found", column_title),
)))
}
}