Skip to content
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

Create initial proxy service #2

Merged
merged 6 commits into from
Dec 19, 2023
Merged

Create initial proxy service #2

merged 6 commits into from
Dec 19, 2023

Conversation

emilyatmobtown
Copy link
Contributor

This PR creates the basic structure for the library. The package can be installed to a project and, when enabled on a request by filter, will take over the dispatch of a WP_REST_Server request. In this way, a WP REST API endpoint can be used to proxy a request to a remote API.

Copy link
Member

@dlh01 dlh01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty interesting approach. I'd love to know more about the situation that led to it.

I think my biggest question is about the use of the rest_pre_dispatch filter, since it necessitates re-implementing the logic for matching the route and everything that follows. Would it work to use the rest_dispatch_request filter? That runs after after the core REST server finds the handler and checks the request params.

@emilyatmobtown
Copy link
Contributor Author

This is a pretty interesting approach. I'd love to know more about the situation that led to it.

I think my biggest question is about the use of the rest_pre_dispatch filter, since it necessitates re-implementing the logic for matching the route and everything that follows. Would it work to use the rest_dispatch_request filter? That runs after after the core REST server finds the handler and checks the request params.

@dlh01 I hear you – I weighed that option as well. Going that route would require having a callable callback in the route registration. Otherwise dispatch() returns an error before the rest_dispatch_request filter is called.

By hijacking the request earlier, the callback property still needs to exist in the route registration args for a handler to be matched, but it can be an empty string. That felt like an acceptable requirement in integrating the library. Requiring a throwaway callback felt awkward.

The situation that led to it is an API using WP REST API endpoints that is a proxy for requests to a remote API. The remote API is the source of truth for data, but is not built to handle the kind of traffic that will come from the site. The proxy will leverage edge cache and object cache to reduce the traffic and also integrate data points from WordPress in the response so the remote response can be trimmed down.

@dlh01
Copy link
Member

dlh01 commented Dec 18, 2023

By hijacking the request earlier, the callback property still needs to exist in the route registration args for a handler to be matched, but it can be an empty string. That felt like an acceptable requirement in integrating the library. Requiring a throwaway callback felt awkward.

Huh. I guess I understand that point, but why would the route not have a callback?

Maybe I misunderstood something. I thought that this library was for intercepting requests to routes that would otherwise have been served by WordPress. Is the idea instead that implementers would register a route with the intention of having the route be served by this library? So your route registration would be lighter-weight, and then by integrating the route with the plugin, the plugin would do most of the work behind the scenes?

@emilyatmobtown
Copy link
Contributor Author

Huh. I guess I understand that point, but why would the route not have a callback?

Maybe I misunderstood something. I thought that this library was for intercepting requests to routes that would otherwise have been served by WordPress. Is the idea instead that implementers would register a route with the intention of having the route be served by this library? So your route registration would be lighter-weight, and then by integrating the route with the plugin, the plugin would do most of the work behind the scenes?

@dlh01 Exactly. The latter is the intention. The WordPress route is simply a proxy to a third party request. There may be other use cases, but in our case the third party API is not built for the volume of traffic on the site so we'll mitigate that with a caching layer in the proxy API. There's also a hook to add a transformation layer so while a callback isn't needed, there is the option of transforming the response shape in the implementation.

@emilyatmobtown emilyatmobtown merged commit 644af62 into develop Dec 19, 2023
5 checks passed
@emilyatmobtown emilyatmobtown deleted the initial-setup branch December 19, 2023 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants