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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
android_project_root := ./native/android
android_generated_source_path := $(android_project_root)/lib/build/generated/source
jni_libs_root := $(android_project_root)/lib/src/main/jniLibs
udl_path := wp_api/src/wp_api.udl

# The directory where the git repo is mounted in the docker container
docker_container_repo_dir=/app
Expand Down Expand Up @@ -35,7 +34,7 @@ bindings:

#wp_api
cargo run --release --bin uniffi_bindgen generate --library ./target/release/libwp_api.dylib --out-dir $(android_generated_source_path) --language kotlin
cargo run --release --bin uniffi_bindgen generate wp_api/src/wp_api.udl --out-dir ./target/swift-bindings --language swift
cargo run --release --bin uniffi_bindgen generate --library ./target/release/libwp_api.dylib --out-dir ./target/swift-bindings --language swift
cp target/swift-bindings/wp_api.swift native/swift/Sources/wordpress-api-wrapper/wp_api.swift
sed -i '' 's/wp_apiFFI/libwordpressFFI/g' native/swift/Sources/wordpress-api-wrapper/wp_api.swift

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LibraryTest {
fun testBasicPostListRequest() {
val request = library.postListRequest()
assert(request.method == RequestMethod.GET)
assert(request.url == "$siteUrl/wp-json/wp/v2/posts?context=edit")
assert(request.url == "$siteUrl/wp-json/wp/v2/posts?context=edit&page=1&per_page=10")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Library(siteUrl: String, authentication: WpAuthentication) {
private val wpApiHelper = WpApiHelper(siteUrl, authentication)
private val client = OkHttpClient()

fun postListRequest(): WpNetworkRequest = wpApiHelper.postListRequest(PostListParams())
fun postListRequest(): WpNetworkRequest =
wpApiHelper.postListRequest(PostListParams())

fun makePostListRequest(): PostListResponse {
val wpNetworkRequest = postListRequest()
Expand All @@ -32,7 +33,11 @@ class Library(siteUrl: String, authentication: WpAuthentication) {
}

client.newCall(requestBuilder.build()).execute().use { response ->
return WpNetworkResponse(response.body!!.bytes(), response.code.toUShort(), null)
return WpNetworkResponse(
body = response.body!!.bytes(),
statusCode = response.code.toUShort(),
headerMap = null
)
}
}
}
2 changes: 1 addition & 1 deletion native/swift/Sources/wordpress-api/WordPressAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct WordPressAPI {

public init(urlSession: URLSession, baseUrl: URL, authenticationStategy: WpAuthentication) {
self.urlSession = urlSession
self.helper = WpApiHelper(url: baseUrl.absoluteString, authentication: authenticationStategy)
self.helper = WpApiHelper(siteUrl: baseUrl.absoluteString, authentication: authenticationStategy)
}

package func perform(request: WpNetworkRequest) async throws -> WpNetworkResponse {
Expand Down
3 changes: 0 additions & 3 deletions wp_api/build.rs

This file was deleted.

4 changes: 2 additions & 2 deletions wp_api/src/api_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use http::StatusCode;

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum WPApiError {
#[error(
"Client error with type '{:?}' and status_code '{}'",
Expand All @@ -19,7 +19,7 @@ pub enum WPApiError {
UnknownError,
}

#[derive(Debug)]
#[derive(Debug, uniffi::Enum)]
pub enum ClientErrorType {
BadRequest,
Unauthorized,
Expand Down
26 changes: 14 additions & 12 deletions wp_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ pub mod api_error;
pub mod pages;
pub mod posts;

#[derive(uniffi::Object)]
pub struct WPApiHelper {
site_url: Url,
authentication: WPAuthentication,
}

#[uniffi::export]
impl WPApiHelper {
#[uniffi::constructor]
pub fn new(site_url: String, authentication: WPAuthentication) -> Self {
let url = Url::parse(site_url.as_str()).unwrap();

Expand Down Expand Up @@ -53,15 +56,10 @@ impl WPApiHelper {
format!("Basic {}", self.authentication.auth_token),
);

if let Some(page) = params.page {
url.query_pairs_mut()
.append_pair("page", page.to_string().as_str());
}

if let Some(per_page) = params.per_page {
url.query_pairs_mut()
.append_pair("per_page", per_page.to_string().as_str());
}
url.query_pairs_mut()
.append_pair("page", params.page.to_string().as_str());
url.query_pairs_mut()
.append_pair("per_page", params.per_page.to_string().as_str());

WPNetworkRequest {
method: RequestMethod::GET,
Expand All @@ -71,19 +69,21 @@ impl WPApiHelper {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, uniffi::Record)]
// TODO: This will probably become an `enum` where we support multiple authentication types.
pub struct WPAuthentication {
pub auth_token: String,
}

#[derive(uniffi::Enum)]
pub enum RequestMethod {
GET,
POST,
PUT,
DELETE,
}

#[derive(uniffi::Record)]
pub struct WPNetworkRequest {
pub method: RequestMethod,
pub url: String,
Expand All @@ -95,9 +95,10 @@ pub struct WPNetworkRequest {
pub header_map: Option<HashMap<String, String>>,
}

#[derive(uniffi::Record)]
pub struct WPNetworkResponse {
pub status_code: u16,
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.
//
Expand All @@ -106,6 +107,7 @@ pub struct WPNetworkResponse {
pub header_map: Option<HashMap<String, String>>,
}

#[uniffi::export]
pub fn parse_post_list_response(
response: WPNetworkResponse,
) -> Result<PostListResponse, WPApiError> {
Expand Down Expand Up @@ -156,4 +158,4 @@ pub fn extract_link_header(response: &WPNetworkResponse) -> Option<String> {
None
}

uniffi::include_scaffolding!("wp_api");
uniffi::setup_scaffolding!("wp_api");
7 changes: 3 additions & 4 deletions wp_api/src/pages.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
pub struct PageRequestBuilder {}

#[derive(uniffi::Record)]
pub struct PageListParams {
pub page: Option<u32>,
pub per_page: Option<u32>,
}

#[derive(Debug)]
#[derive(Debug, uniffi::Record)]
pub struct PageListResponse {
pub page_list: Option<Vec<PageObject>>,
}

#[derive(Debug)]
#[derive(Debug, uniffi::Record)]
pub struct PageObject {
pub id: Option<u32>,
pub title: Option<String>,
Expand Down
39 changes: 25 additions & 14 deletions wp_api/src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,81 @@ use serde::{Deserialize, Serialize};

pub trait PostNetworkingInterface: Send + Sync {}

#[derive(Default)] // The default has `None` for all
#[derive(Default, uniffi::Record)]
pub struct PostListParams {
pub page: Option<u32>,
pub per_page: Option<u32>,
#[uniffi(default = 1)]
pub page: u32,
#[uniffi(default = 10)]
pub per_page: u32,
}

#[derive(uniffi::Record)]
pub struct PostCreateParams {
pub title: Option<String>,
pub content: Option<String>,
}

#[derive(uniffi::Record)]
pub struct PostRetrieveParams {
pub password: Option<String>,
}

#[derive(uniffi::Record)]
pub struct PostUpdateParams {
pub title: Option<String>,
pub content: Option<String>,
}

#[derive(uniffi::Record)]
pub struct PostDeleteParams {
pub force: Option<bool>,
}

#[derive(uniffi::Record)]
pub struct PostListRequest {
pub params: Option<String>,
}
#[derive(uniffi::Record)]
pub struct PostCreateRequest {
pub params: Option<String>,
}
#[derive(uniffi::Record)]
pub struct PostRetrieveRequest {
pub params: Option<String>,
}
#[derive(uniffi::Record)]
pub struct PostUpdateRequest {
pub params: Option<String>,
}
#[derive(uniffi::Record)]
pub struct PostDeleteRequest {
pub params: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostListResponse {
pub post_list: Option<Vec<PostObject>>,
pub next_page: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostCreateResponse {
pub post: Option<PostObject>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostRetrieveResponse {
pub post: Option<PostObject>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostUpdateResponse {
pub post: Option<PostObject>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostDeleteResponse {
pub post: Option<PostObject>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostObject {
pub id: Option<u32>,
pub date: Option<String>,
Expand All @@ -92,34 +103,34 @@ pub struct PostObject {
pub tags: Option<Vec<u32>>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostGuid {
pub raw: Option<String>,
pub rendered: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostTitle {
pub raw: Option<String>,
pub rendered: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostContent {
pub raw: Option<String>,
pub rendered: Option<String>,
pub protected: Option<bool>,
pub block_version: Option<u32>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostExcerpt {
pub raw: Option<String>,
pub rendered: Option<String>,
pub protected: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct PostMeta {
pub footnotes: Option<String>,
}
Loading