Class: DurableHuggingfaceHub::Utils::HttpClient
- Inherits:
-
Object
- Object
- DurableHuggingfaceHub::Utils::HttpClient
- Defined in:
- lib/durable_huggingface_hub/utils/http.rb
Overview
HTTP client for making requests to the HuggingFace Hub API.
This class provides a configured Faraday client with retry logic, connection pooling, timeout settings, and proper error handling.
Instance Attribute Summary collapse
-
#connection ⇒ Faraday::Connection
readonly
The underlying Faraday connection.
-
#default_headers ⇒ Hash
readonly
Default headers for all requests.
-
#endpoint ⇒ String
readonly
Base URL for API requests.
-
#token ⇒ String?
readonly
Authentication token.
Instance Method Summary collapse
-
#delete(path, params: nil, headers: nil) ⇒ Faraday::Response
Performs a DELETE request.
-
#get(path, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a GET request.
-
#head(path, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a HEAD request.
-
#initialize(token: nil, endpoint: nil, headers: nil, timeout: nil, open_timeout: nil, proxy: nil, logger: nil) ⇒ HttpClient
constructor
Creates a new HTTP client.
-
#post(path, body: nil, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a POST request.
-
#put(path, body: nil, params: nil, headers: nil) ⇒ Faraday::Response
Performs a PUT request.
-
#request(method, path, body: nil, params: nil, headers: nil, timeout: nil) {|req| ... } ⇒ Faraday::Response
Performs an HTTP request with error handling.
Constructor Details
#initialize(token: nil, endpoint: nil, headers: nil, timeout: nil, open_timeout: nil, proxy: nil, logger: nil) ⇒ HttpClient
Creates a new HTTP client.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 47 def initialize( token: nil, endpoint: nil, headers: nil, timeout: nil, open_timeout: nil, proxy: nil, logger: nil ) @token = token || Configuration.instance.token @endpoint = endpoint || Configuration.instance.endpoint @default_headers = build_default_headers(headers) @logger = logger @connection = build_connection( timeout: timeout, open_timeout: open_timeout, proxy: proxy ) end |
Instance Attribute Details
#connection ⇒ Faraday::Connection (readonly)
Returns The underlying Faraday connection.
36 37 38 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 36 def connection @connection end |
#default_headers ⇒ Hash (readonly)
Returns Default headers for all requests.
33 34 35 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 33 def default_headers @default_headers end |
#endpoint ⇒ String (readonly)
Returns Base URL for API requests.
30 31 32 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 30 def endpoint @endpoint end |
#token ⇒ String? (readonly)
Returns Authentication token.
27 28 29 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 27 def token @token end |
Instance Method Details
#delete(path, params: nil, headers: nil) ⇒ Faraday::Response
Performs a DELETE request.
117 118 119 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 117 def delete(path, params: nil, headers: nil) request(:delete, path, params: params, headers: headers) end |
#get(path, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a GET request.
78 79 80 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 78 def get(path, params: nil, headers: nil, timeout: nil) request(:get, path, params: params, headers: headers, timeout: timeout) end |
#head(path, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a HEAD request.
129 130 131 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 129 def head(path, params: nil, headers: nil, timeout: nil) request(:head, path, params: params, headers: headers, timeout: timeout) end |
#post(path, body: nil, params: nil, headers: nil, timeout: nil) ⇒ Faraday::Response
Performs a POST request.
94 95 96 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 94 def post(path, body: nil, params: nil, headers: nil, timeout: nil) request(:post, path, body: body, params: params, headers: headers, timeout: timeout) end |
#put(path, body: nil, params: nil, headers: nil) ⇒ Faraday::Response
Performs a PUT request.
106 107 108 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 106 def put(path, body: nil, params: nil, headers: nil) request(:put, path, body: body, params: params, headers: headers) end |
#request(method, path, body: nil, params: nil, headers: nil, timeout: nil) {|req| ... } ⇒ Faraday::Response
Performs an HTTP request with error handling.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/durable_huggingface_hub/utils/http.rb', line 144 def request(method, path, body: nil, params: nil, headers: nil, timeout: nil, &block) url = build_url(path) merged_headers = @default_headers.merge(headers || {}) response = @connection.send(method) do |req| req.url(url) req.params.update(params) if params req.headers.update(merged_headers) req.body = prepare_body(body) if body && method != :get && method != :head req..timeout = timeout if timeout block&.call(req) end handle_response(response) rescue Faraday::Error => e handle_faraday_error(e) end |