-
Notifications
You must be signed in to change notification settings - Fork 3
Initial design for Post Request scaffolding #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
// | ||
// The reason we can use a `PostsRequest` type and not need a more specific type such as | ||
// `PostsListRequest` is because the API documentation states that the return value for | ||
// all `/posts` requests have the same schema: https://developer.wordpress.org/rest-api/reference/posts/#schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this isn't quite true – the schema depends on the context, so view
will return different fields than embed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's the case regardless of the action type (list
, create
etc), right? The documentation specifically says that:
The schema defines all the fields that exist within a post record. Any response from these endpoints can be expected to contain the fields below unless the
_filter
query parameter is used or the schema field only appears in a specific context.
That's where I was going from. However, the context
bit is going to be a difficult problem for us to solve. I've actually left a TODO about that here.
I've been thinking about this after opening the PR, and regardless of the result of this discussion, we are probably going to end up with specific request types.
u32? per_page; | ||
}; | ||
|
||
dictionary PostsRetrieveParams { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, but retrieve
, update
, and delete
operate on a single object. Should we use non-plural nouns for these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had a discussion about this on Zoom today, but just for completeness sake, the reason I used Posts
was to have a logical naming scheme which uses the endpoint as the first word of the type. However, I think we can always use the singular version of that and still have a logical naming scheme.
pub force: Option<bool>, | ||
} | ||
|
||
pub struct PostsRequest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need an HTTP method member here too.
I assume we'll give this to a higher-level RestApi
object that will hold the hostname, http protocol, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap, lots of fields are missing from this.
This PR proposes an initial design for the
/posts
endpoint of WordPress.org API: https://developer.wordpress.org/rest-api/reference/posts/. It's the result of investigating several approaches in a time-boxed manner and it is not a final design.P.S: There is no PR review process for this repository yet, because it's all part of a prototype. So, I'll merge this PR without waiting for feedback. However, if you have any feedback, I'd love to hear about it!
The first design approach we have explored was to use a
Builder
pattern for the request parameters. It's a common design pattern for Rust and it's something developers from most programming languages are familiar with - which is important for a cross-platform project. It's also an ergonomic design when there are a lot of optional types.The main issue we found with
Builder
pattern, in the context of using it in FFI layer, is that we can't have a mutableself
type. For example, the following code doesn't work through FFI layer - because it's not safe:Similarly, we can't use the following approach because we can't force the accessor to give up ownership of the memory through FFI layer - not without custom code anyway:
We have the option to use interior mutability, so for example, we could do:
However, notice the method signature and specifically the return type. We are no longer returning
Self
because we only have a reference to the memory. That means we can't chain commands such asself.bar().baz()
which arguably is one of the primary reasons to utilize aBuilder
pattern.From what we have learned through the
Builder
pattern exploration, the design in this PR made sense to us as an initial approach.The primary components that were supposed to utilize the
Builder
pattern are*Params
objects such asPostsListParams
. The reason we thought that this design made sense as an alternative is that we'd need these components even if we used aBuilder
pattern because when we call.build()
on aBuilder
pattern object, it's supposed to return an object such asPostsListParams
anyway. That's all to say that, if we really want to use theBuilder
pattern in each native layer, this design will accommodate that. And in general, it's a flexible design that's meant to be built on through higher layers.One last note is that how we are going to make requests is not investigated at all in this PR. So, the
PostsRequestBuilder
methods are returning aPostsRequest
is mostly a placeholder at this stage.