Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible access to items #1

Closed
ademasi opened this issue Oct 29, 2020 · 4 comments
Closed

Impossible access to items #1

ademasi opened this issue Oct 29, 2020 · 4 comments

Comments

@ademasi
Copy link

ademasi commented Oct 29, 2020

Hi,

Thank you for your work. I wanted to start playing with Zotero and Rust via your crate.
But I am having some issues, I am using my own key generated on https://www.zotero.org/settings/keys to access my items. Nothing get printed at all, looks like my username/password are not working.

Did I miss something ? (I am totally new to rust)

    let z = ZoteroInit::set_user("XXXX", "YYYY");
    
    let items = z.get_items(None);

    for item in items {
        println!("{:#?}", item)
    }
    

Regards, Alex.

@ademasi ademasi changed the title Issues of using this crate Impossible access to items Oct 29, 2020
@Eonm
Copy link
Owner

Eonm commented Oct 30, 2020

Hi @ademasi ,

Thank you for your issue and welcome to the Rust community 🦀.

You should try something like this :

extern crate zotero;

use zotero::ZoteroInit;
use zotero::Get;

use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let z = ZoteroInit::set_user("ID", "API_KEY");
    let items = z.get_items(None)?;
    
   for item in items.iter() {
     println!("{:?}", item);
   }

    Ok(())
}

The problem with your code is that z.get_items(None) returns a Result. A result holds a value or an Error. If you want to iter over a Result you need to get it's value first. To do so you can unwrap the Result whit : z.get_items(None).unwrap() or you can use ? operator as I did in my first example. Or You can also use pattern matching like this :

extern crate zotero;

use zotero::ZoteroInit;
use zotero::Get;

use std::error::Error;

fn main() {
    let z = ZoteroInit::set_user("ID", "API_KEY");
    let items = z.get_items(None);
    
    let items = z.get_items(None);
    
   match items {
     Ok(zotero_items) => println!("{:?}", zotero_items),
     Err(_) => panic!("API Error")
   }
}

If you want to check your API key privileges you should take a look at the doc.

let z = ZoteroInit::set_user("ID", "API_KEY");
let key_info = z.get_api_key_info("API_KEY");
println!("{:?}", key_info);

While looking to fix your issue I noticed other issues with my crate. Zotero API slightly change since I implemented it and I'm facing Derialization issues 😞. I need more time to fix this see #2.

Best regards

@ademasi
Copy link
Author

ademasi commented Oct 30, 2020

Hi @Eonm ,

Thank you for taking the time to reply to my issue.

Indeed, I was expecting something closer to what I am used to in Python or Java. I need to read the rust book ...
Anyway, I was able to access my API key details with get_api_key_info. But as you saw, if I am trying to access an item, even directly with its ID, I get this this error :

Err(
    Error("invalid type: map, expected a string", line: 0, column: 0),
)

What would be the first step to hep with with #2 ?

Regards, Alex.

@Eonm
Copy link
Owner

Eonm commented Oct 30, 2020

I take a closer look at this deserialization issue this afternoon. I can provide a hot fix, but more work needs to be done. I need to implement proper unit test and add more documentation, maybe this week end.

Can you replace the zotero dependencies entry with the following in Cargo.toml :

zotero = { git = "https://github.com/Eonm/zotero", branch ="deserialization-fix"}

I won't touch anything on this branch for not braking your code.

Regards,

@Eonm
Copy link
Owner

Eonm commented Oct 30, 2020

I didn't notice but the first code you used was in the documentation. It's my fault. The documentation is wrong.

@ju6ge ju6ge closed this as completed Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants