Module: Lexdrill::HTTPClient
- Defined in:
- lib/lexdrill/http_client.rb
Overview
The only place lexdrill talks to Net::HTTP directly. Lexdrill::GoogleAuth (form-encoded OAuth endpoints) and Lexdrill::SheetsClient (JSON REST) both go through here, so every low-level connectivity failure (DNS, timeout, TLS, connection refused) is normalized to a single NetworkError for callers to handle, instead of each caller needing to know every possible Net::HTTP/OpenSSL/socket exception class.
Defined Under Namespace
Classes: NetworkError, Response
Constant Summary collapse
- OPEN_TIMEOUT =
10- READ_TIMEOUT =
15
Class Method Summary collapse
- .json_get(url, headers: {}) ⇒ Object
- .json_post(url, body:, headers: {}) ⇒ Object
- .json_put(url, body:, headers: {}) ⇒ Object
-
.post_form(url, params) ⇒ Object
Net::HTTP's 2-arg instance
#post(path, body)sends no Content-Type header at all — most token endpoints tolerate that (defaulting to form-urlencoded), but not all do, so this sets it explicitly.
Class Method Details
.json_get(url, headers: {}) ⇒ Object
42 43 44 45 46 |
# File 'lib/lexdrill/http_client.rb', line 42 def self.json_get(url, headers: {}) uri = URI(url) request = build_get_request(uri, headers) perform(uri) { |http| http.request(request) } end |
.json_post(url, body:, headers: {}) ⇒ Object
34 35 36 |
# File 'lib/lexdrill/http_client.rb', line 34 def self.json_post(url, body:, headers: {}) json_request(Net::HTTP::Post, url, body, headers) end |
.json_put(url, body:, headers: {}) ⇒ Object
38 39 40 |
# File 'lib/lexdrill/http_client.rb', line 38 def self.json_put(url, body:, headers: {}) json_request(Net::HTTP::Put, url, body, headers) end |
.post_form(url, params) ⇒ Object
Net::HTTP's 2-arg instance #post(path, body) sends no Content-Type
header at all — most token endpoints tolerate that (defaulting to
form-urlencoded), but not all do, so this sets it explicitly.
24 25 26 27 28 29 30 31 32 |
# File 'lib/lexdrill/http_client.rb', line 24 def self.post_form(url, params) uri = URI(url) perform(uri) do |http| request = Net::HTTP::Post.new(uri.request_uri) request["Content-Type"] = "application/x-www-form-urlencoded" request.body = URI.encode_www_form(params) http.request(request) end end |