-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add optional raw response processing to query context #24
Add optional raw response processing to query context #24
Conversation
See #25 for the whole experience. These is how this change is used--context class overrides to shape a non-json response into json: |
We set some early assumptions about supported APIs, one of which was that it transacted in JSON. I think it's a great idea to provide escape hatches for non-JSON responses and other exceptional attributes. I had intended |
And yet. 🤪 I think it's fine if that's what we say on the tin, but, besides the need I have for otherwise right here... I could see situations with others that might want to do the same. For example, maybe I'd like to read a static CSV file directly from somewhere.
I am not saying it's the only way or best way. I tried to thread the needle of a change that stayed true to your vision here. FWIW, it gets the job done and, I think, semantically makes decent sense.
Welp, the upside, now |
Yeah, we could do that. I'm glad that customization exists. I considered that but, as a "customer" implementing, I didn't like that I had to re-implement the whole thing just to manipulate the response. I wanted the world-class WP developer request handling 💪 done for me. And I didn't want to copy/paste. |
+1
I like when complexity only presents itself when you need to confront it. Ideally, most extenders of That's one complication with moving I think you've correctly pointed out that the abstraction isn't quite right. For non-JSON response formats like HTML and CSV, I'm wondering if there's a way to delegate just the basic response parsing and leave the "field mapping" as-is. |
I considered that. There is. It requires the context to have fairly intimate knowledge about how The former didn't feel good because of the mental overhead and re-encoding to a JSON string and then extra decodes in |
I went ahead and made the smaller incision since I think you feel more strongly about this than I do. I don't love the need to
|
return base64_decode( $field_value_single ); | ||
|
||
case 'html': | ||
return $field_value_single; |
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.
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.
escapin late :)
I pushed up some type annotations, comments, and tests to show that this isn't necessary. |
* | ||
* @return bool | ||
*/ | ||
public function is_collection(): bool { |
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.
What is the use case for this is_collection()
method override? Maybe the response needs to be inspected? In that case, it seems like it should get passed the $response_data
? Or removed
// This method always returns an array, even if it's a single item. This | ||
// ensures a consistent response shape. The requestor is expected to inspect | ||
// is_collection and unwrap if necessary. | ||
$results = $this->map_fields( $response_data, $is_collection ); | ||
$results = $this->map_fields( $response_data ); |
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.
Removing arg $is_collection
means that is_collection()
gets called twice. Can we get it back to one call?
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.
Approved, but would like to see the is_collection()
Qs resolved somehow
Ha, and so it does. TIL. Yeah, that makes me happier. |
This PR:
Adds
QueryContext#process_response
so implementors can preprocess before the response hitsmap_fields
.Why?
Some remote fetches may not be JSON or, even if so, may need manipulation in certain circumstances that only the "client" (implementor) can know.
How To Test
Unit tests passing. Smoke test your favorite remote data blocks in wp-admin and confirm still working.