Conversation
|
Maybe we should call this |
| # @param [String|Symbol, nil] output response format using the SerpApi output parameter [optional] | ||
| # @return [String|Hash] raw HTML, Markdown, or JSON / Hash | ||
| def search_archive(search_id, format = :json, output: nil) | ||
| format = output.to_s.to_sym unless output.nil? |
There was a problem hiding this comment.
Why separate format and output arguments?
Personally I prefer |
|
Thanks @trusche for having a look. You are right on search_archive, we should cleanly just pass the extension. I fixed it.
Well, that's how I started as well :) However, consistency among our clients is more important I think (already in Python and PHP libs) and I think there is a tiny advantage of matching the extension. |
|
Ruby and rails offer a lot of syntactic sugar, we could at least create an alias? |
|
I was thinking about it, but on the other hand I want us to be direct in docs and want to send people to one method everywhere. So creating an alias just on its own doesn't feel that good to me.
Do you think it's still worth it? |
|
@strzibny Thanks for the efforts. Functionality wise everything looks good to me 👍 One minor thing I observed (not due to code changes as part of this PR) is that when I pass I feel it would be good to mention some sample code in our Readme doc. Something like below where we are differentiating the three, # For JSON output
result_json = serpapi_client.search
# For HTML output
result_html = serpapi_client.html
# For Markdown output
result_md = serpapi_client.mdNote - I observe that our python library supports |
|
Thanks @pulkitchowdry, you are right, we should have already been choosing the right decoder for HTML. I included it in this change. |
| # | ||
| # @return [String] raw html search results directly from the search engine. | ||
| def html(params = {}) | ||
| get('/search', :html, params) |
There was a problem hiding this comment.
@strzibny Thanks for the changes. Output parameter is working now!
One issue I observed is that when we do not pass output = "html" and call serpapi_client.html(q: "coffee") then the response is json and not in html format. Should this be like below?
| get('/search', :html, params) | |
| get('/search.html', :html, params) |
Other tests I performed are mismatch between output parameter and the method called and it works properly. Its response is similar to SerpApi cURL response - for example if output parameter is json but we call .html then we respond with json which is fine as the output parameter is taking priority like the main SerpApi.
|
@pulkitchowdry Great find. I realized we need to fix it a bit better. If we only use I made a change to always return hash on |
|
I think the |
|
I think you are right @trusche it also overly complicates 'what should we do'. I still kept CONTENT_TYPE_DECODERS because with this change we can properly show errors that server returns as JSON. Wdyt? |
| response = execute_request(endpoint, params) | ||
| handle_response(response, decoder, endpoint, params) | ||
| # @return [String|Hash] raw text or decoded response as JSON / Hash | ||
| def get(endpoint, decoder = :json, params = {}, output: nil) |
There was a problem hiding this comment.
I still don't get why we need separate decoder and output arguments. Would it ever be used with different formats?
There was a problem hiding this comment.
Would you be more happy with something like this?
def response_decoder(response, default)
output = response.headers['Content-Type'].to_s.split(%r{[/;]})[1]
output = 'md' if output == 'markdown'
OUTPUT_DECODERS.fetch(output, default)
end
Not sure if I understand what you are asking but running client.md(...) will have to process JSON in case of errors.
There was a problem hiding this comment.
And in another case client.send(:get, '/invalid', :json, {}) is returning HTML with text/html content type. So the idea is to process responses by the content type we actually get from serpapi.com
This is WIP change for Markdown support.