-
Notifications
You must be signed in to change notification settings - Fork 3
Experiment: Add support for fetch-post to wp_rs_cli #834
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
Open
AliSoftware
wants to merge
14
commits into
trunk
Choose a base branch
from
wp_rs_cli/fetch-post
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vibe-coded with cursor-agent
To make things more generic for potential future subcommands that might need similar auth instead of making this too specific to FetchPost
So it could be made more generic and reusable for any future subcommands that might need such conversion too
48d9527
to
d21276a
Compare
As this is only used within the implementation details of that method so makes more sense rather than polluting the module namespace
- Renamed TargetSiteResolver to SiteApiType - Extract the logic to detect the type based on AuthArgs and url into a dedicated `detect_from_args` method - Extract the logic to build the right `ApiUrlResolver` and `WpAuthenticationProvider` for each `SiteApiType` into dedicated methods This helps simplfy a lot the code for `build_api_client` and makes everything more readable
Move the `FetchPostArgs` struct to the top near the other subcommand definitions for consistency with having everything related to subcommand arguments grouped in the same place.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
This adds a
fetch-post
subcommand towp_rs_cli
Why
First, this is obviously a useful feature to have for the CLI
But in particular, as discussed in recent conversations with Oguz in Slack (internal ref: p1754602450419219/1754596820.179099-slack-C08CUT5NT88), this would be especially useful to integrate the CLI into some of our current AI workflows—where we might want to ask our LLM like Claude or Cursor to use the CLI to fetch a post's content and comments and do something with the data (summarize, extract particular info from comments and reformat, etc…)
How
Note
This is mostly vibe-coded with cursor-agent and GPT-5 🤖
(though I reviewed the code and made some tweaks of my own too)
AuthArgs
struct for common parameters related to authentication needed for this subcommand but that might also be useful to any future subcommand that might require authenticationbuild_api_client
method to build aWpApiClient
from theAuthArgs
and an optional pageurl
.SiteApiType::detect_from_args
will use thewpcom_site
(WpCom) orapi_root
(WpOrg) args fromAuthArgs
if explicitly providedurl
parameter ifwpcom_site
/api_root
are not provided explicitly (extractwpcom_site
from the host of theurl
if it's a*.wordpress.com
URL, useWpLoginClient.api_discovery(url)
to setapi_root
otherwise)Commands::FetchPost
subcommand, with its dedicatedFetchPostArgs
, a subset of them coming from#[command(flatten)] auth: AuthArgs
resolve_post_id
to find thepost_id
from a post URL / slug.fetch_post_and_comments
(the core implementation of theCommands::FetchPosts
command) to call theWpApiClient
's.posts().retrieve_with_view_context()
+.comments().list_with_view_context(…)
+ paginationwhile
loop in thefetch_post_and_comments
method. I didn't want to overcomplicate and try to optimize too early though, and figured the current approach would be ok as a first iteration of the implementation before it could be made more generic laterWpApiClient
's level that I missed?Testing
Future directions
Since this is the first of probably many future CLI subcommands that might require authentication, it'd be nice next to have a subcommand to perform the OAuth flow.
Then once we have automatic handling of OAuth flow (call
/authorize
, obtain the code, exchange it for a token, store the bearer token + refresh token + expiration locally for next CLI calls to reuse, auto-refresh the token on expiration…) by the CLI itself, theAuthArgs
and friends will likely be adjusted accordingly so that you don't have to pass the bearer token manually.But all that was out of scope for this spike/experiment, so for now you're just expected to obtain the bearer token on your end and provide it to the CLI, and we can refactor that part around
AuthArgs
later when we get to it or add more subcommands requiring it.