Skip to content

Commit bc30e7a

Browse files
author
Vsevolod
committed
update_row function and documentation for some functions and structures were added
1 parent 450328f commit bc30e7a

File tree

7 files changed

+233
-209
lines changed

7 files changed

+233
-209
lines changed

module/move/gspread/src/actions/gspread.rs

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,35 @@ mod private
1010
use error_tools::typed::Error;
1111
use derive_tools::AsRefStr;
1212
use crate::*;
13-
use ser::{DisplayFromStr, JsonValue};
13+
use ser::
14+
{
15+
DisplayFromStr,
16+
JsonValue
17+
};
1418
use std::collections::HashMap;
15-
use google_sheets4::api::{BatchUpdateValuesResponse, ValueRange};
19+
use google_sheets4::api::
20+
{
21+
BatchUpdateValuesResponse,
22+
BatchUpdateValuesRequest,
23+
ValueRange
24+
};
1625

1726
#[ ser::serde_as ]
1827
#[ derive( Debug, Error, AsRefStr, ser::Serialize ) ]
1928
#[ serde( tag = "type", content = "data" ) ]
29+
30+
/// Represents errors that can occur while interacting with the Google Sheets API
31+
/// or during related operations in the application.
2032
pub enum Error
2133
{
34+
/// Represents an error returned by the Google Sheets API.
35+
///
36+
/// # Details
37+
/// This error occurs when the API returns a specific error message.
38+
/// The error message from the Google Sheets API is stored and displayed.
39+
///
40+
/// # Fields
41+
/// - `google_sheets4::Error`: The raw error returned by the API.
2242
#[ error( "Google Sheets returned error:\n{0}" ) ]
2343
ApiError
2444
(
@@ -27,31 +47,75 @@ mod private
2747
google_sheets4::Error
2848
),
2949

50+
/// Represents an error that occurs while initializing Google Sheets Hub.
51+
///
52+
/// # Details
53+
/// This error indicates that the application failed to properly configure with the Google Sheets Hub.
54+
///
55+
/// # Fields
56+
/// - `String`: A detailed error message describing the issue.
57+
#[ error( "Hub Error:\n{0}" ) ]
58+
HubError
59+
(
60+
String
61+
),
62+
63+
/// Represents an error caused by an invalid URL format.
64+
///
65+
/// # Details
66+
/// This error occurs when the provided URL does not match the expected format
67+
///
68+
/// # Fields
69+
/// - `String`: The invalid URL or a message describing the issue.
3070
#[ error( "Invalid URL format:\n{0}" ) ]
3171
InvalidUrl
3272
(
3373
String
3474
),
3575

76+
/// Represents an error related to a cell in the spreadsheet.
77+
///
78+
/// # Details
79+
/// This error indicates that a cell was not got or updated
80+
///
81+
/// # Fields
82+
/// - `String`: A message describing the issue with the cell.
3683
#[ error( "Cell error:\n{0}" ) ]
3784
CellError
3885
(
3986
String
4087
),
4188

89+
/// Represents an error caused by invalid JSON input or parsing issues.
90+
///
91+
/// # Details
92+
/// This error occurs when the provided JSON data does not conform to the expected
93+
/// structure or format.
94+
///
95+
/// # Fields
96+
/// - `String`: A detailed error message describing the JSON issue.
4297
#[ error( "Invalid JSON format:\n{0}" ) ]
4398
InvalidJSON
4499
(
45100
String
46101
),
47102

103+
/// Represents a generic parsing error.
104+
///
105+
/// # Details
106+
/// This error is raised when a string or other input cannot be parsed
107+
/// into the expected format or structure.
108+
///
109+
/// # Fields
110+
/// - `String`: A message describing the parse error.
48111
#[ error( "Parse error:\n{0}" ) ]
49112
ParseError
50113
(
51114
String
52115
)
53116
}
54117

118+
/// Retrive spreadsheet id from url
55119
pub fn get_spreadsheet_id_from_url
56120
(
57121
url : &str
@@ -75,24 +139,34 @@ mod private
75139

76140
/// Function to update a row on a Google Sheet.
77141
///
78-
/// It converts from HashMap to a row wich is actually sorted array, by column name.
142+
/// It sends HTTP request to Google Sheets API and change row wich provided values.
79143
///
80144
/// **Params**
81-
/// - `id` : Row's id.
82-
/// - `vales` : Pairs of key value, where key is a clomun's name and value is a value of cell.
145+
/// - `spreadsheet_id` : Spreadsheet identifire.
146+
/// - `sheet_name` : Sheet name.
147+
/// - `row_key` : row's key.
148+
/// - `row_key_val` : pairs of key value, where key is a column name and value is a new value.
83149
///
84150
/// **Returns**
85-
/// - `RowWrapper` object.
151+
/// - `Result`
86152
pub async fn update_row
87153
(
88-
row_key : usize,
89-
values : HashMap< String, String >,
90-
sheet_name : &str
91-
) -> Result< Vec< ValueRange > >
154+
spreadsheet_id : &str,
155+
sheet_name : &str,
156+
row_key : &str,
157+
row_key_val : HashMap< String, String >
158+
) -> Result< BatchUpdateValuesResponse >
92159
{
93-
let mut value_ranges = Vec::with_capacity( values.len() );
160+
let secret = Secret::read();
161+
let hub = hub(&secret)
162+
.await
163+
.map_err( | _ | {
164+
Error::HubError( format!( "Failed to create a hub. Ensure that you have a .env file with Secrets" ) )
165+
})?;
94166

95-
for ( col_name, value ) in values {
167+
let mut value_ranges = Vec::with_capacity( row_key_val.len() );
168+
169+
for ( col_name, value ) in row_key_val {
96170
value_ranges.push
97171
(
98172
ValueRange
@@ -104,7 +178,23 @@ mod private
104178
)
105179
}
106180

107-
Ok( value_ranges )
181+
let req = BatchUpdateValuesRequest
182+
{
183+
value_input_option: Some( "USER_ENTERED".to_string() ),
184+
data: Some( value_ranges ),
185+
include_values_in_response: Some( true ),
186+
..Default::default()
187+
};
188+
189+
match hub
190+
.spreadsheets()
191+
.values_batch_update( req, spreadsheet_id )
192+
.doit()
193+
.await
194+
{
195+
Ok( ( _, response ) ) => Ok( response ),
196+
Err( error ) => Err( Error::ApiError( error ) ),
197+
}
108198
}
109199

110200
pub type Result< T > = core::result::Result< T, Error >;

0 commit comments

Comments
 (0)