Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code, unused_variables)]

use serde::Deserialize;
use std::collections::HashMap;

pub use api_error::*;
Expand Down Expand Up @@ -390,6 +391,16 @@ pub fn parse_api_details_response(response: WPNetworkResponse) -> Result<WPAPIDe
Ok(api_details)
}

pub fn parse_wp_response<'de, T: Deserialize<'de>>(
response: &'de WPNetworkResponse,
) -> Result<T, WPApiError> {
parse_response_for_generic_errors(response)?;
serde_json::from_slice(&response.body).map_err(|err| WPApiError::ParsingError {
reason: err.to_string(),
response: String::from_utf8_lossy(&response.body).to_string(),
})
}

pub fn parse_response_for_generic_errors(response: &WPNetworkResponse) -> Result<(), WPApiError> {
let response_str = String::from_utf8_lossy(&response.body).to_string();
// TODO: Further parse the response body to include error message
Expand Down
30 changes: 10 additions & 20 deletions wp_api/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,69 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use wp_derive::WPContextual;

use crate::{parse_response_for_generic_errors, WPApiError, WPApiParamOrder, WPNetworkResponse};
use crate::{parse_wp_response, WPApiError, WPApiParamOrder, WPNetworkResponse};

#[uniffi::export]
pub fn parse_filter_users_response(
response: &WPNetworkResponse,
) -> Result<Vec<SparseUser>, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_filter_retrieve_user_response(
response: &WPNetworkResponse,
) -> Result<SparseUser, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_list_users_response_with_edit_context(
response: &WPNetworkResponse,
) -> Result<Vec<UserWithEditContext>, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_list_users_response_with_embed_context(
response: &WPNetworkResponse,
) -> Result<Vec<UserWithEmbedContext>, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_list_users_response_with_view_context(
response: &WPNetworkResponse,
) -> Result<Vec<UserWithViewContext>, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_retrieve_user_response_with_edit_context(
response: &WPNetworkResponse,
) -> Result<UserWithEditContext, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_retrieve_user_response_with_embed_context(
response: &WPNetworkResponse,
) -> Result<UserWithEmbedContext, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_retrieve_user_response_with_view_context(
response: &WPNetworkResponse,
) -> Result<UserWithViewContext, WPApiError> {
parse_users_response(response)
parse_wp_response(response)
}

#[uniffi::export]
pub fn parse_delete_user_response(
response: &WPNetworkResponse,
) -> Result<UserDeleteResponse, WPApiError> {
parse_users_response(response)
}

pub fn parse_users_response<'de, T: Deserialize<'de>>(
response: &'de WPNetworkResponse,
) -> Result<T, WPApiError> {
parse_response_for_generic_errors(response)?;
serde_json::from_slice(&response.body).map_err(|err| WPApiError::ParsingError {
reason: err.to_string(),
response: String::from_utf8_lossy(&response.body).to_string(),
})
parse_wp_response(response)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
Expand Down