Most elegant and terse way of adding wrappers around the library #13
-
Consider this simplified example for adding caching and error handling to HTTP GET requests:
What would be the most elegant and terse way to extend this to all HTTP methods and arguments?
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 6 replies
-
The recommended way is building a custom plugin. Intercepting a request can be done like this: module MyCachePlugin
module InstanceMethods
def send_requests(*requests)
cached_responses = get_cached_requests(requests)
# and for the remainder, call super, and guarantee ordered responses based on requests input
end
end
end You'll need to get familiar with
There's no documentation, nor wiki page, suggesting inheritance as a gateway to customize functionality. On the other hand, alternatives to do so are documented (as linked above). While I could go into detail about specifying why you shouldn't (but can) do option A or B (it may even work), not being a documented alternative should send a clear message. |
Beta Was this translation helpful? Give feedback.
-
This became a cakewalk when I found out how Below is a incomplete illustration of how I ended up. You're right that the best thing would be to study your plugin system and write something that works with that. That way other people could benefit from having a user-controlled cache/mirror. I wrapped the calls in classless methods because I didn't want to write
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your friendly advice and recommendations. Some comments/follow-up questions.
|
Beta Was this translation helpful? Give feedback.
-
A follow-up on error handling.
|
Beta Was this translation helpful? Give feedback.
-
Let's get practical. The goal is to create an error handler that, when something goes wrong, the user gets information that is 1) precise and 2) as detailed as necessary to understand and address the situation. Thus it would have be something like the pseudo-code below. How would this be written in actual code?
Notes:
|
Beta Was this translation helpful? Give feedback.
-
def x
url = 'http://127.0.0.1:4567/status'
response = HTTPX.get(url)
response.raise_for_status
rescue => e
case e
when HTTPX::HTTPError
puts 'HTTP 400-599 (Server Error)'
pp e.class
pp response.status # AKA e.status
pp response.headers
# All below this point are errors occuring before server response
when OpenSSL::SSL::SSLError
puts 'Could not establish secure connection (Certificate problem, etc.)'
pp e
# when OpenSSLErrorForCertificateFailure
# puts 'uninitialized constant OpenSSLErrorForCertificateFailure (NameError)'
when IPAddr::AddressFamilyError
puts 'Could not connect to the Internet'
pp e # #<IPAddr::AddressFamilyError: address family must be specified>
# when NoInternetError
# puts 'uninitialized constant NoInternetError (NameError)'
else
puts 'Other errors occuring before server response'
pp e
end
else
puts 'HTTP 100-399 (Server OK)'
puts response.status
puts response.body.to_s.length
end
class ConnectionError < Error
class GoawayError < Error
class HTTPError < Error
class HTTPProxyError < Error
class MisdirectedRequestError < HTTPError
class ResolveError < Error
class Socks4Error < Error
class Socks5Error < Error
class TimeoutError < Error
class UnsupportedSchemeError < Error |
Beta Was this translation helpful? Give feedback.
-
def x
response = HTTPX.get(url)
case response.status
when 400-599 # or possibly "when false"
puts "HTTP/#{response.version} #{response.status}"
pp response.headers
when 100-399 # or possibly "when true"
puts response.body
else # or possibly "when nil"
puts response.status.class # eg. 'IPAddr::AddressFamilyError'
puts response.details # eg. 'address family must be specified'
end
end |
Beta Was this translation helpful? Give feedback.
The recommended way is building a custom plugin.
Intercepting a request can be done like this:
You'll need to get familiar with
httpx
internal API, so I suggest reading the documentation, and studying other similar plugins, such as the response_cache plugin.There's no documentation, nor wiki page, suggesting inheritance as a gateway to customize functionality. On the oth…