main
Mauro D 2022-09-09 09:54:09 +00:00
parent 756085ec4d
commit 88eeb32198
1 changed files with 103 additions and 103 deletions

206
README.md
View File

@ -22,120 +22,120 @@ Features:
## Usage Example ## Usage Example
```rust ```rust
// Connect to the JMAP server using Basic authentication. // Connect to the JMAP server using Basic authentication.
// (just for demonstration purposes, Bearer tokens should be used instead) // (just for demonstration purposes, Bearer tokens should be used instead)
let client = Client::new() let client = Client::new()
.credentials(("john@example.org", "secret")) .credentials(("john@example.org", "secret"))
.connect("https://jmap.example.org") .connect("https://jmap.example.org")
.await .await
.unwrap(); .unwrap();
// Create a mailbox. // Create a mailbox.
let mailbox_id = client let mailbox_id = client
.mailbox_create("My Mailbox", None::<String>, Role::None) .mailbox_create("My Mailbox", None::<String>, Role::None)
.await .await
.unwrap() .unwrap()
.take_id(); .take_id();
// Import a message into the mailbox. // Import a message into the mailbox.
client client
.email_import( .email_import(
b"From: john@example.org\nSubject: test\n\n test".to_vec(), b"From: john@example.org\nSubject: test\n\n test".to_vec(),
[&mailbox_id], [&mailbox_id],
["$draft"].into(), ["$draft"].into(),
None, None,
) )
.await .await
.unwrap(); .unwrap();
// Obtain all e-mail ids matching a filter. // Obtain all e-mail ids matching a filter.
let email_id = client let email_id = client
.email_query( .email_query(
Filter::and([ Filter::and([
email::query::Filter::subject("test"), email::query::Filter::subject("test"),
email::query::Filter::in_mailbox(&mailbox_id), email::query::Filter::in_mailbox(&mailbox_id),
email::query::Filter::has_keyword("$draft"), email::query::Filter::has_keyword("$draft"),
]) ])
.into(), .into(),
[email::query::Comparator::from()].into(), [email::query::Comparator::from()].into(),
) )
.await .await
.unwrap() .unwrap()
.take_ids() .take_ids()
.pop() .pop()
.unwrap(); .unwrap();
// Fetch an e-mail message. // Fetch an e-mail message.
let email = client let email = client
.email_get( .email_get(
&email_id, &email_id,
[Property::Subject, Property::Preview, Property::Keywords].into(), [Property::Subject, Property::Preview, Property::Keywords].into(),
) )
.await .await
.unwrap() .unwrap()
.unwrap(); .unwrap();
assert_eq!(email.preview().unwrap(), "test"); assert_eq!(email.preview().unwrap(), "test");
assert_eq!(email.subject().unwrap(), "test"); assert_eq!(email.subject().unwrap(), "test");
assert_eq!(email.keywords(), ["$draft"]); assert_eq!(email.keywords(), ["$draft"]);
// Fetch only the updated properties of all mailboxes that changed // Fetch only the updated properties of all mailboxes that changed
// since a state. // since a state.
let mut request = client.build(); let mut request = client.build();
let changes_request = request.changes_mailbox("n").max_changes(0); let changes_request = request.changes_mailbox("n").max_changes(0);
let properties_ref = changes_request.updated_properties_reference(); let properties_ref = changes_request.updated_properties_reference();
let updated_ref = changes_request.updated_reference(); let updated_ref = changes_request.updated_reference();
request request
.get_mailbox() .get_mailbox()
.ids_ref(updated_ref) .ids_ref(updated_ref)
.properties_ref(properties_ref); .properties_ref(properties_ref);
for mailbox in request for mailbox in request
.send() .send()
.await .await
.unwrap() .unwrap()
.unwrap_method_responses() .unwrap_method_responses()
.pop() .pop()
.unwrap() .unwrap()
.unwrap_get_mailbox() .unwrap_get_mailbox()
.unwrap() .unwrap()
.take_list() .take_list()
{ {
println!("Changed mailbox: {:#?}", mailbox); println!("Changed mailbox: {:#?}", mailbox);
} }
// Delete the mailbox including any messages // Delete the mailbox including any messages
client.mailbox_destroy(&mailbox_id, true).await.unwrap(); client.mailbox_destroy(&mailbox_id, true).await.unwrap();
// Open an EventSource connection with the JMAP server. // Open an EventSource connection with the JMAP server.
let mut stream = client let mut stream = client
.event_source( .event_source(
[ [
TypeState::Email, TypeState::Email,
TypeState::EmailDelivery, TypeState::EmailDelivery,
TypeState::Mailbox, TypeState::Mailbox,
TypeState::EmailSubmission, TypeState::EmailSubmission,
TypeState::Identity, TypeState::Identity,
] ]
.into(), .into(),
false, false,
60.into(), 60.into(),
None, None,
) )
.await .await
.unwrap(); .unwrap();
// Consume events received over EventSource. // Consume events received over EventSource.
while let Some(event) = stream.next().await { while let Some(event) = stream.next().await {
let changes = event.unwrap(); let changes = event.unwrap();
println!("-> Change id: {:?}", changes.id()); println!("-> Change id: {:?}", changes.id());
for account_id in changes.changed_accounts() { for account_id in changes.changed_accounts() {
println!(" Account {} has changes:", account_id); println!(" Account {} has changes:", account_id);
if let Some(account_changes) = changes.changes(account_id) { if let Some(account_changes) = changes.changes(account_id) {
for (type_state, state_id) in account_changes { for (type_state, state_id) in account_changes {
println!(" Type {:?} has a new state {}.", type_state, state_id); println!(" Type {:?} has a new state {}.", type_state, state_id);
}
} }
} }
} }
}
``` ```
More examples available under the [examples](examples) directory. More examples available under the [examples](examples) directory.