Byron / google-apis-rs

A binding and CLI generator for all Google APIs
http://byron.github.io/google-apis-rs
Other
1.02k stars 136 forks source link

Easy way to create structs? #243

Closed lippirk closed 4 years ago

lippirk commented 4 years ago

I am using this library to make requests to the google sheets api, although not using yup-oauth2. I'm testing it out and it works, but I was wondering if there's an easier way to build requests if I don't want to use the Hub. At the moment I have:

  let sheet = google_sheets4::Spreadsheet {
    sheets: Some(vec![Sheet {
      data: Some(vec![GridData {
        row_data: Some(vec![RowData {
          values: Some(vec![CellData {
            user_entered_value: Some(ExtendedValue {
              number_value: Some(8.0),
              ..ExtendedValue::default()
            }),
            ..CellData::default()
          }]),
          ..RowData::default()
        }]),
        ..GridData::default()
      }]),
      ..Sheet::default()
    }]),
    ..Spreadsheet::default()
  };

  let mut resp = client // my own client implementation
    .post("https://sheets.googleapis.com/v4/spreadsheets")
    .header("Authorization", format!("Bearer {}", access_token.secret()))
    .header("Accept", "application/json")
    .header("Content-Type", "application/json")
    .json(&sheet)
    .send()?;

Is there an easier way to construct the Spreadsheet without all those nested updates?

Byron commented 4 years ago

Actually I don't think there is a better way, unfortunately. And I really thought about it :D!

A major issue seems to be that every field is an option. The next version of these APIs supports specifying custom structures, leaving you with control over the types of fields. Thus you could have a structure without options that can be initialized like this:

let s = MySpreadsheet::default();
s.sheets.push(Sheet {
      data: vec![GridData {
        row_data: vec![RowData {
          values: vec![CellData {
            user_entered_value: ExtendedValue {
              number_value: Some(8.0),
              ..ExtendedValue::default()
            },
            ..CellData::default()
          }],
          ..RowData::default()
        }],
        ..GridData::default()
      }],
      ..Sheet::default()
    }]
})

It's not much better, so there is probably no other way but to live with it and hide it behind constructor-like functions for common cases.