SearchSnippet implementation.
parent
8b7cfda3d7
commit
136f10ec52
|
@ -2,7 +2,10 @@ use std::collections::HashMap;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::core::{set::SetError, RequestParams};
|
||||
use crate::{
|
||||
core::{set::SetError, RequestParams},
|
||||
Error,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct CopyBlobRequest {
|
||||
|
@ -27,16 +30,16 @@ pub struct CopyBlobResponse {
|
|||
}
|
||||
|
||||
impl CopyBlobRequest {
|
||||
pub fn new(params: RequestParams, from_account_id: String) -> Self {
|
||||
pub fn new(params: RequestParams, from_account_id: impl Into<String>) -> Self {
|
||||
CopyBlobRequest {
|
||||
from_account_id,
|
||||
from_account_id: from_account_id.into(),
|
||||
account_id: params.account_id,
|
||||
blob_ids: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blob_id(&mut self, blob_id: String) -> &mut Self {
|
||||
self.blob_ids.push(blob_id);
|
||||
pub fn blob_id(&mut self, blob_id: impl Into<String>) -> &mut Self {
|
||||
self.blob_ids.push(blob_id.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@ -50,17 +53,21 @@ impl CopyBlobResponse {
|
|||
&self.account_id
|
||||
}
|
||||
|
||||
pub fn copied(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
pub fn copied(&mut self, id: &str) -> crate::Result<String> {
|
||||
if let Some(result) = self.copied.as_mut().and_then(|r| r.remove(id)) {
|
||||
Ok(result)
|
||||
} else if let Some(error) = self.not_copied.as_mut().and_then(|r| r.remove(id)) {
|
||||
Err(error.to_string_error().into())
|
||||
} else {
|
||||
Err(Error::Internal(format!("Id {} not found.", id)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn copied_ids(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
self.copied.as_ref().map(|map| map.keys())
|
||||
}
|
||||
|
||||
pub fn copied_details(&self, id: &str) -> Option<&str> {
|
||||
self.copied
|
||||
.as_ref()
|
||||
.and_then(|map| map.get(id).map(|s| s.as_str()))
|
||||
}
|
||||
|
||||
pub fn not_copied(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
pub fn not_copied_ids(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
self.not_copied.as_ref().map(|map| map.keys())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,27 @@
|
|||
use crate::{
|
||||
client::Client,
|
||||
core::request::{Arguments, Request},
|
||||
Method,
|
||||
};
|
||||
|
||||
use super::copy::{CopyBlobRequest, CopyBlobResponse};
|
||||
|
||||
impl Client {
|
||||
pub async fn blob_copy(
|
||||
&self,
|
||||
from_account_id: impl Into<String>,
|
||||
blob_id: impl Into<String>,
|
||||
) -> crate::Result<String> {
|
||||
let blob_id = blob_id.into();
|
||||
let mut request = self.build();
|
||||
request.copy_blob(from_account_id).blob_id(&blob_id);
|
||||
request
|
||||
.send_single::<CopyBlobResponse>()
|
||||
.await?
|
||||
.copied(&blob_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Request<'_> {
|
||||
pub fn copy_blob(&mut self, from_account_id: impl Into<String>) -> &mut CopyBlobRequest {
|
||||
self.add_method_call(
|
||||
|
|
|
@ -5,7 +5,10 @@ use serde::{de::DeserializeOwned, Serialize};
|
|||
use crate::{
|
||||
blob::copy::CopyBlobRequest,
|
||||
client::Client,
|
||||
email::{import::EmailImportRequest, parse::EmailParseRequest, Email},
|
||||
email::{
|
||||
import::EmailImportRequest, parse::EmailParseRequest,
|
||||
search_snippet::SearchSnippetGetRequest, Email,
|
||||
},
|
||||
email_submission::EmailSubmission,
|
||||
identity::Identity,
|
||||
mailbox::Mailbox,
|
||||
|
@ -71,6 +74,7 @@ pub enum Arguments {
|
|||
EmailCopy(CopyRequest<Email<Set>>),
|
||||
EmailImport(EmailImportRequest),
|
||||
EmailParse(EmailParseRequest),
|
||||
SearchSnippetGet(SearchSnippetGetRequest),
|
||||
IdentityGet(GetRequest<Identity<Set>>),
|
||||
IdentitySet(SetRequest<Identity<Set>>),
|
||||
EmailSubmissionGet(GetRequest<EmailSubmission<Set>>),
|
||||
|
@ -150,6 +154,10 @@ impl Arguments {
|
|||
Arguments::EmailParse(EmailParseRequest::new(params))
|
||||
}
|
||||
|
||||
pub fn search_snippet_get(params: RequestParams) -> Self {
|
||||
Arguments::SearchSnippetGet(SearchSnippetGetRequest::new(params))
|
||||
}
|
||||
|
||||
pub fn identity_get(params: RequestParams) -> Self {
|
||||
Arguments::IdentityGet(GetRequest::new(params))
|
||||
}
|
||||
|
@ -313,6 +321,13 @@ impl Arguments {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn search_snippet_get_mut(&mut self) -> &mut SearchSnippetGetRequest {
|
||||
match self {
|
||||
Arguments::SearchSnippetGet(ref mut r) => r,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn identity_get_mut(&mut self) -> &mut GetRequest<Identity<Set>> {
|
||||
match self {
|
||||
Arguments::IdentityGet(ref mut r) => r,
|
||||
|
|
|
@ -4,7 +4,10 @@ use serde::{de::Visitor, Deserialize};
|
|||
|
||||
use crate::{
|
||||
blob::copy::CopyBlobResponse,
|
||||
email::{import::EmailImportResponse, parse::EmailParseResponse, Email},
|
||||
email::{
|
||||
import::EmailImportResponse, parse::EmailParseResponse,
|
||||
search_snippet::SearchSnippetGetResponse, Email,
|
||||
},
|
||||
email_submission::EmailSubmission,
|
||||
identity::Identity,
|
||||
mailbox::Mailbox,
|
||||
|
@ -110,7 +113,6 @@ pub type EmailGetResponse = GetResponse<Email<Get>>;
|
|||
pub type EmailSetResponse = SetResponse<Email<Get>>;
|
||||
pub type EmailCopyResponse = CopyResponse<Email<Get>>;
|
||||
pub type EmailChangesResponse = ChangesResponse<Email<Get>>;
|
||||
pub type SearchSnippetGetResponse = GetResponse<String>;
|
||||
pub type IdentitySetResponse = SetResponse<Identity<Get>>;
|
||||
pub type IdentityGetResponse = GetResponse<Identity<Get>>;
|
||||
pub type IdentityChangesResponse = ChangesResponse<Identity<Get>>;
|
||||
|
|
|
@ -16,6 +16,7 @@ use crate::{
|
|||
use super::{
|
||||
import::{EmailImportRequest, EmailImportResponse},
|
||||
parse::{EmailParseRequest, EmailParseResponse},
|
||||
search_snippet::{SearchSnippetGetRequest, SearchSnippetGetResponse},
|
||||
BodyProperty, Email, Property,
|
||||
};
|
||||
|
||||
|
@ -219,6 +220,20 @@ impl Client {
|
|||
.await?
|
||||
.created(&id)
|
||||
}
|
||||
|
||||
pub async fn search_snippet_get(
|
||||
&self,
|
||||
filter: Option<impl Into<Filter<super::query::Filter>>>,
|
||||
email_ids: impl IntoIterator<Item = impl Into<String>>,
|
||||
) -> crate::Result<SearchSnippetGetResponse> {
|
||||
let mut request = self.build();
|
||||
let snippet_request = request.get_search_snippet();
|
||||
if let Some(filter) = filter {
|
||||
snippet_request.filter(filter);
|
||||
}
|
||||
snippet_request.email_ids(email_ids);
|
||||
request.send_single::<SearchSnippetGetResponse>().await
|
||||
}
|
||||
}
|
||||
|
||||
impl Request<'_> {
|
||||
|
@ -326,4 +341,16 @@ impl Request<'_> {
|
|||
pub async fn send_parse_email(self) -> crate::Result<EmailParseResponse> {
|
||||
self.send_single().await
|
||||
}
|
||||
|
||||
pub fn get_search_snippet(&mut self) -> &mut SearchSnippetGetRequest {
|
||||
self.add_method_call(
|
||||
Method::GetSearchSnippet,
|
||||
Arguments::search_snippet_get(self.params(Method::GetSearchSnippet)),
|
||||
)
|
||||
.search_snippet_get_mut()
|
||||
}
|
||||
|
||||
pub async fn send_get_search_snippet(self) -> crate::Result<SearchSnippetGetResponse> {
|
||||
self.send_single().await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::core::{query::Filter, request::ResultReference, RequestParams};
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct SearchSnippet {
|
||||
#[serde(rename = "emailId")]
|
||||
email_id: String,
|
||||
subject: Option<String>,
|
||||
preview: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct SearchSnippetGetRequest {
|
||||
#[serde(rename = "accountId")]
|
||||
account_id: String,
|
||||
|
||||
#[serde(rename = "filter")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
filter: Option<Filter<super::query::Filter>>,
|
||||
|
||||
#[serde(rename = "emailIds")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
email_ids: Option<Vec<String>>,
|
||||
|
||||
#[serde(rename = "#emailIds")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
email_ids_ref: Option<ResultReference>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct SearchSnippetGetResponse {
|
||||
#[serde(rename = "accountId")]
|
||||
account_id: String,
|
||||
|
||||
#[serde(rename = "list")]
|
||||
list: Vec<SearchSnippet>,
|
||||
|
||||
#[serde(rename = "notFound")]
|
||||
not_found: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl SearchSnippetGetRequest {
|
||||
pub fn new(params: RequestParams) -> Self {
|
||||
SearchSnippetGetRequest {
|
||||
account_id: params.account_id,
|
||||
filter: None,
|
||||
email_ids: None,
|
||||
email_ids_ref: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter(&mut self, filter: impl Into<Filter<super::query::Filter>>) -> &mut Self {
|
||||
self.filter = Some(filter.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn email_id(&mut self, email_id: impl Into<String>) -> &mut Self {
|
||||
self.email_ids
|
||||
.get_or_insert_with(Vec::new)
|
||||
.push(email_id.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn email_ids(
|
||||
&mut self,
|
||||
email_ids: impl IntoIterator<Item = impl Into<String>>,
|
||||
) -> &mut Self {
|
||||
self.email_ids
|
||||
.get_or_insert_with(Vec::new)
|
||||
.extend(email_ids.into_iter().map(|id| id.into()));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn email_ids_ref(&mut self, reference: ResultReference) -> &mut Self {
|
||||
self.email_ids_ref = reference.into();
|
||||
self.email_ids = None;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchSnippet {
|
||||
pub fn email_id(&self) -> &str {
|
||||
&self.email_id
|
||||
}
|
||||
|
||||
pub fn subject(&self) -> Option<&str> {
|
||||
self.subject.as_deref()
|
||||
}
|
||||
|
||||
pub fn preview(&self) -> Option<&str> {
|
||||
self.preview.as_deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchSnippetGetResponse {
|
||||
pub fn account_id(&self) -> &str {
|
||||
&self.account_id
|
||||
}
|
||||
|
||||
pub fn id(&self, id: &str) -> Option<&SearchSnippet> {
|
||||
self.list.iter().find(|snippet| snippet.email_id == id)
|
||||
}
|
||||
|
||||
pub fn list(&self) -> &[SearchSnippet] {
|
||||
&self.list
|
||||
}
|
||||
|
||||
pub fn not_found(&self) -> Option<&[String]> {
|
||||
self.not_found.as_deref()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue