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
48 changes: 2 additions & 46 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![allow(dead_code, unused_variables)]

use request::{endpoint::ApiEndpoint, WPNetworkRequest, WPNetworkResponse};
use serde::Deserialize;
use std::collections::HashMap;
use url::Url;

pub use api_error::{WPApiError, WPRestError, WPRestErrorCode, WPRestErrorWrapper};
use endpoint::*;
use login::*;
use plugins::*;
use users::*;

mod api_error; // re-exported relevant types
pub mod endpoint;
pub mod login;
pub mod plugins;
pub mod request;
pub mod users;

#[cfg(test)]
Expand Down Expand Up @@ -391,50 +391,6 @@ impl WPApiParamOrder {
}
}

#[derive(uniffi::Record)]
pub struct WPNetworkRequest {
pub method: RequestMethod,
pub url: String,
// TODO: We probably want to implement a specific type for these headers instead of using a
// regular HashMap.
//
// It could be something similar to `reqwest`'s [`header`](https://docs.rs/reqwest/latest/reqwest/header/index.html)
// module.
pub header_map: HashMap<String, String>,
pub body: Option<Vec<u8>>,
}

#[derive(uniffi::Record)]
pub struct WPNetworkResponse {
pub body: Vec<u8>,
pub status_code: u16,
// TODO: We probably want to implement a specific type for these headers instead of using a
// regular HashMap.
//
// It could be something similar to `reqwest`'s [`header`](https://docs.rs/reqwest/latest/reqwest/header/index.html)
// module.
pub header_map: Option<HashMap<String, String>>,
}

impl WPNetworkResponse {
pub fn get_link_header(&self, name: &str) -> Option<Url> {
if let Some(headers) = self.header_map.clone() {
// TODO: This is inefficient
if headers.contains_key("Link") {
if let Ok(res) = parse_link_header::parse_with_rel(&headers["Link"]) {
if let Some(next) = res.get(name) {
if let Ok(url) = Url::parse(next.raw_uri.as_str()) {
return Some(url);
}
}
}
}
}

None
}
}

#[uniffi::export]
pub fn parse_api_details_response(response: WPNetworkResponse) -> Result<WPAPIDetails, WPApiError> {
let api_details =
Expand Down
51 changes: 51 additions & 0 deletions wp_api/src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::collections::HashMap;

use url::Url;

use crate::RequestMethod;

pub mod endpoint;

#[derive(uniffi::Record)]
pub struct WPNetworkRequest {
pub method: RequestMethod,
pub url: String,
// TODO: We probably want to implement a specific type for these headers instead of using a
// regular HashMap.
//
// It could be something similar to `reqwest`'s [`header`](https://docs.rs/reqwest/latest/reqwest/header/index.html)
// module.
pub header_map: HashMap<String, String>,
pub body: Option<Vec<u8>>,
}

#[derive(uniffi::Record)]
pub struct WPNetworkResponse {
pub body: Vec<u8>,
pub status_code: u16,
// TODO: We probably want to implement a specific type for these headers instead of using a
// regular HashMap.
//
// It could be something similar to `reqwest`'s [`header`](https://docs.rs/reqwest/latest/reqwest/header/index.html)
// module.
pub header_map: Option<HashMap<String, String>>,
}

impl WPNetworkResponse {
pub fn get_link_header(&self, name: &str) -> Option<Url> {
if let Some(headers) = self.header_map.clone() {
// TODO: This is inefficient
if headers.contains_key("Link") {
if let Ok(res) = parse_link_header::parse_with_rel(&headers["Link"]) {
if let Some(next) = res.get(name) {
if let Ok(url) = Url::parse(next.raw_uri.as_str()) {
return Some(url);
}
}
}
}
}

None
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use url::Url;

use crate::{plugins::PluginListParams, ApiBaseUrl, PluginSlug, SparsePluginField, WPContext};
use crate::{plugins::PluginListParams, PluginSlug, SparsePluginField, WPContext};

use super::UrlExtension;
use super::{ApiBaseUrl, UrlExtension};

pub struct PluginsEndpoint {
api_base_url: ApiBaseUrl,
Expand Down Expand Up @@ -75,8 +75,9 @@ impl PluginsEndpoint {
mod tests {
use super::*;
use crate::{
endpoint::tests::{fixture_api_base_url, validate_endpoint},
generate, ApiEndpoint, PluginStatus,
generate,
request::endpoint::tests::{fixture_api_base_url, validate_endpoint},
ApiEndpoint, PluginStatus,
};
use rstest::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use url::Url;

use crate::{ApiBaseUrl, SparseUserField, UserDeleteParams, UserId, UserListParams, WPContext};
use crate::{SparseUserField, UserDeleteParams, UserId, UserListParams, WPContext};

use super::UrlExtension;
use super::{ApiBaseUrl, UrlExtension};

pub struct UsersEndpoint {
api_base_url: ApiBaseUrl,
Expand Down Expand Up @@ -93,7 +93,7 @@ impl UsersEndpoint {
mod tests {
use super::*;
use crate::{
endpoint::tests::{fixture_api_base_url, validate_endpoint},
request::endpoint::tests::{fixture_api_base_url, validate_endpoint},
ApiEndpoint,
};
use rstest::*;
Expand Down
5 changes: 3 additions & 2 deletions wp_api/tests/integration_test_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use futures::Future;
use http::HeaderMap;
use std::{fs::read_to_string, process::Command};
use wp_api::{
users::UserId, WPApiError, WPApiHelper, WPAuthentication, WPNetworkRequest, WPNetworkResponse,
WPRestError, WPRestErrorCode, WPRestErrorWrapper,
request::{WPNetworkRequest, WPNetworkResponse},
users::UserId,
WPApiError, WPApiHelper, WPAuthentication, WPRestError, WPRestErrorCode, WPRestErrorWrapper,
};

// The first user is also the current user
Expand Down