Struct apisdk::CodeDataMessage
source · pub struct CodeDataMessage<T = Option<Value>> {
pub code: i64,
pub data: T,
pub message: Option<String>,
/* private fields */
}
Expand description
This struct is used to parse {code, data, message}
payload.
When it’s used as Extractor
, it will extract data
from payload.
§Examples
§As Extractor
To be used as Extractor
, CodeDataMessage
will check code
field of response payload, and ensure it must be 0
.
If not, it will generate an ApiError instance with code
and message
.
async fn get_user(&self) -> ApiResult<User> {
let req = client.get("/api/path").await?;
send!(req, CodeDataMessage).await
}
§As Result
If we want to access the response headers or extra fields, we could use CodeDataMessage
as result type.
async fn get_user(&self) -> ApiResult<User> {
let req = client.get("/api/path").await?;
let res: CodeDataMessage<User> = send!(req).await?;
// to access HTTP headers: res.get_header("name")
// to access extra fields: res.get_extra("other_field")
if res.is_success() {
Ok(res.data)
} else {
Err(ApiError::BusinessError(res.code, res.get_header().map(|v| v.to_string())))
}
}
Fields§
§code: i64
code
field
data: T
data
field
message: Option<String>
message
or msg
field
Implementations§
source§impl<T> CodeDataMessage<T>
impl<T> CodeDataMessage<T>
sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Check whether code
is 0
sourcepub fn get_header(&self, name: &str) -> Option<&str>
pub fn get_header(&self, name: &str) -> Option<&str>
Get any header
- name: header name
sourcepub fn get_extra<D>(&self, name: &str) -> Option<D>where
D: DeserializeOwned,
pub fn get_extra<D>(&self, name: &str) -> Option<D>where
D: DeserializeOwned,
Get any unknown field
- name: field name
sourcepub fn get_request_id(&self) -> Option<&str>
pub fn get_request_id(&self) -> Option<&str>
Get X-Request-ID
header
sourcepub fn get_trace_id(&self) -> Option<&str>
pub fn get_trace_id(&self) -> Option<&str>
Get X-Trace-ID
header
sourcepub fn get_span_id(&self) -> Option<&str>
pub fn get_span_id(&self) -> Option<&str>
Get X-Span-ID
header
Trait Implementations§
source§impl<T: Debug> Debug for CodeDataMessage<T>
impl<T: Debug> Debug for CodeDataMessage<T>
source§impl<'de, T> Deserialize<'de> for CodeDataMessage<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for CodeDataMessage<T>where
T: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl JsonExtractor for CodeDataMessage
impl JsonExtractor for CodeDataMessage
source§fn try_extract<T>(self) -> ApiResult<T>where
T: DeserializeOwned,
fn try_extract<T>(self) -> ApiResult<T>where
T: DeserializeOwned,
Try to extract result from response. Read more
source§fn require_headers() -> bool
fn require_headers() -> bool
The extractor needs response HTTP headers or not.
source§impl<T> Serialize for CodeDataMessage<T>where
T: Serialize,
impl<T> Serialize for CodeDataMessage<T>where
T: Serialize,
source§impl TryFrom<ResponseBody> for CodeDataMessage
impl TryFrom<ResponseBody> for CodeDataMessage
Auto Trait Implementations§
impl<T> Freeze for CodeDataMessage<T>where
T: Freeze,
impl<T> RefUnwindSafe for CodeDataMessage<T>where
T: RefUnwindSafe,
impl<T> Send for CodeDataMessage<T>where
T: Send,
impl<T> Sync for CodeDataMessage<T>where
T: Sync,
impl<T> Unpin for CodeDataMessage<T>where
T: Unpin,
impl<T> UnwindSafe for CodeDataMessage<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more